forked from n0-computer/iroh-c-ffi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti-thread-server.c
248 lines (212 loc) · 7.22 KB
/
multi-thread-server.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include "irohnet.h"
int run_server(Endpoint_t *ep, slice_ref_uint8_t alpn_slice, bool json_output)
{
if (json_output)
{
printf("{ \"type\": \"server\", \"status\": \"starting\" }\n");
}
else
{
printf("Starting server...\n");
}
// Bind
// Accept connections
Connection_t *conn = connection_default();
int res = endpoint_accept(&ep, alpn_slice, &conn);
if (res != 0)
{
fprintf(stderr, "failed to accept connection");
return -1;
}
// Accept uni directional connection
RecvStream_t *recv_stream = recv_stream_default();
res = connection_accept_uni(&conn, &recv_stream);
if (res != 0)
{
fprintf(stderr, "failed to accept stream");
return -1;
}
uint8_t *recv_buffer = malloc(512);
slice_mut_uint8_t recv_buffer_slice;
recv_buffer_slice.ptr = recv_buffer;
recv_buffer_slice.len = 512;
int read = recv_stream_read(&recv_stream, recv_buffer_slice);
if (read == -1)
{
fprintf(stderr, "failed to read data");
return -1;
}
// assume they sent us a nice string
char *recv_str = malloc(read + 1);
memcpy(recv_str, recv_buffer, read);
recv_str[read] = '\0';
if (json_output)
{
printf("{ \"type\": \"server\", \"status\": \"received\", \"data\": \"%s\" }\n", recv_str);
}
else
{
printf("received: '%s'\n", recv_str);
// print rtt
uint64_t rtt = connection_rtt(&conn);
printf("Estimated RTT: %llu ms\n", rtt);
// print packet loss, multiple by 100 to get the percentage
float packet_loss = connection_packet_loss(&conn) * 100;
printf("Estimated packet loss: %0.2f%%\n", packet_loss);
}
fflush(stdout);
// Cleanup
free(recv_str);
recv_stream_free(recv_stream);
// Accept bi directional connection
printf("accepting bi\n");
recv_stream = recv_stream_default();
SendStream_t *send_stream = send_stream_default();
res = connection_accept_bi(&conn, &send_stream, &recv_stream);
if (res != 0)
{
fprintf(stderr, "failed to accept stream");
return -1;
}
printf("receving data\n");
read = recv_stream_read(&recv_stream, recv_buffer_slice);
if (read == -1)
{
fprintf(stderr, "failed to read data");
return -1;
}
// assume they sent us a nice string
recv_str = malloc(read + 1);
memcpy(recv_str, recv_buffer, read);
recv_str[read] = '\0';
if (json_output)
{
// ..
}
else
{
printf("received: '%s'\n", recv_str);
}
// send response
slice_ref_uint8_t buffer;
buffer.ptr = (uint8_t *)&recv_str[0];
buffer.len = strlen(recv_str);
printf("sending data\n");
int ret = send_stream_write(&send_stream, buffer);
if (ret != 0)
{
fprintf(stderr, "failed to send data\n");
return -1;
}
// finish
ret = send_stream_finish(send_stream);
if (ret != 0)
{
fprintf(stderr, "failed to finish sending\n");
return -1;
}
// Cleanup
free(recv_str);
free(recv_buffer);
recv_stream_free(recv_stream);
connection_free(conn);
endpoint_free(ep);
return 0;
}
// Define a structure to pass multiple parameters to the pthread functions
typedef struct
{
EndpointConfig_t *config;
slice_ref_uint8_t alpn_slice;
Endpoint_t *ep;
bool json_output; // For server
const char *node_id; // For client
const char *relay_url; // For client
const char **addrs; // For client
int addrs_len; // For client
} ThreadParam;
// Wrapper function for the server
void *server_thread_func(void *arg)
{
ThreadParam *params = (ThreadParam *)arg;
run_server(params->ep, params->alpn_slice, params->json_output);
pthread_exit(NULL);
}
int main(int argc, char const *const argv[])
{
iroh_enable_tracing();
pthread_t server_threads[2];
// pthread_t client_threads[2];
ThreadParam server_params[2];
// ThreadParam client_params[2];
// Initialize parameters for each thread, including different ALPNs
char alpn1[] = "/cool/alpn/1";
char alpn2[] = "/cool/alpn/2";
// Assuming server and client specific parameters are initialized here...
// Server thread 1
server_params[0].alpn_slice.ptr = (uint8_t *)&alpn1[0];
server_params[0].alpn_slice.len = strlen(alpn1);
server_params[0].json_output = false; // Or true, based on your requirement
EndpointConfig_t config = endpoint_config_default();
endpoint_config_add_alpn(&config, server_params[0].alpn_slice);
server_params[0].config = &config;
// Server thread 2 with different ALPN
server_params[1].alpn_slice.ptr = (uint8_t *)&alpn2[0];
server_params[1].alpn_slice.len = strlen(alpn2);
server_params[1].json_output = false; // Or true
endpoint_config_add_alpn(&config, server_params[1].alpn_slice);
server_params[1].config = &config;
Endpoint_t *ep = endpoint_default();
int bind_res = endpoint_bind(&config, NULL, NULL, &ep);
if (bind_res != 0)
{
fprintf(stderr, "failed to bind server\n");
return -1;
}
// Print details
NodeAddr_t node_addr = node_addr_default();
int addr_res = endpoint_node_addr(&ep, &node_addr);
if (addr_res != 0)
{
fprintf(stderr, "faile to get my address");
return -1;
}
char *node_id_str = public_key_as_base32(&node_addr.node_id);
char *relay_url_str = url_as_str(node_addr.relay_url);
printf("Listening on:\nNode Id: %s\nRelay: %s\nAddrs:\n", node_id_str, relay_url_str);
// iterate over the direct addresses
for (int i = 0; i < node_addr.direct_addresses.len; i++)
{
SocketAddr_t const *addr = node_addr_direct_addresses_nth(&node_addr, i);
char *socket_str = socket_addr_as_str(addr);
printf(" - %s\n", socket_str);
rust_free_string(socket_str);
}
printf("\n");
fflush(stdout);
server_params[0].ep = ep;
server_params[1].ep = ep;
pthread_create(&server_threads[0], NULL, server_thread_func, (void *)&server_params[0]);
//pthread_create(&server_threads[1], NULL, server_thread_func, (void *)&server_params[1]);
// Client threads would be similar, using client_thread_func and client_params
// Initialize client_params with necessary parameters
// pthread_create(&client_threads[0], NULL, client_thread_func, (void *)&client_params[0]);
// pthread_create(&client_threads[1], NULL, client_thread_func, (void *)&client_params[1]);
// Wait for server threads to complete
pthread_join(server_threads[0], NULL);
pthread_join(server_threads[1], NULL);
rust_free_string(relay_url_str);
rust_free_string(node_id_str);
node_addr_free(node_addr);
// Wait for client threads to complete
// pthread_join(client_threads[0], NULL);
// pthread_join(client_threads[1], NULL);
return 0;
}