forked from n0-computer/iroh-c-ffi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle-thread-server.c
148 lines (128 loc) · 4.4 KB
/
single-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
#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "irohnet.h"
Connection_t *conn1;
Connection_t *conn2;
int main(int argc, char const *const argv[]) {
iroh_enable_tracing();
RecvStream_t *recv_stream = recv_stream_default();
RecvStream_t *recv_stream2 = recv_stream_default();
SendStream_t *send_stream = send_stream_default();
SendStream_t *send_stream2 = send_stream_default();
int accepted = 0;
char alpn_str1[] = "/cool/alpn/1";
char alpn_str2[] = "/cool/alpn/2";
// Assuming server and client specific parameters are initialized here...
// Server thread 1
slice_ref_uint8_t alpn1, alpn2;
alpn1.ptr = (uint8_t *)&alpn_str1[0];
alpn1.len = strlen(alpn_str1);
alpn2.ptr = (uint8_t *)&alpn_str2[0];
alpn2.len = strlen(alpn_str2);
EndpointConfig_t config = endpoint_config_default();
endpoint_config_add_alpn(&config, alpn1);
endpoint_config_add_alpn(&config, alpn2);
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);
printf("Node Address is \n%s\n", node_addr_as_str(&node_addr));
// 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);
conn1 = connection_default();
conn2 = connection_default();
while (accepted < 2) {
Vec_uint8_t alpn_slice_out = rust_buffer_alloc(0);
// int res = endpoint_accept(&ep, alpn_slice_control, &conn);
int res;
if (accepted == 0)
res = endpoint_accept_any(&ep, &alpn_slice_out, &conn1);
else
res = endpoint_accept_any(&ep, &alpn_slice_out, &conn2);
printf("Connection accepted with alpn %s \n", alpn_slice_out.ptr);
if (res != 0) {
printf("[Iroh] failed to accept connection");
return 1;
}
rust_buffer_free(alpn_slice_out);
accepted++;
}
printf("Accepting connection 1\n");
int err = connection_accept_bi(&conn1, &send_stream, &recv_stream);
if (err != 0) {
fprintf(stderr, "failed to accept streams");
return -1;
}
printf("reading to end\n");
Vec_uint8_t recvBuffer = rust_buffer_alloc(0);
err = recv_stream_read_to_end_timeout(&recv_stream, &recvBuffer, 1024, 5000);
if (err == ENDPOINT_RESULT_TIMEOUT) {
printf("Response timed out\n");
return 1;
} else if (err != 0) {
printf("Failed to wait for response: %d\n", err);
return 1;
}
char *data = "hello world from C - 1";
slice_ref_uint8_t buffer;
buffer.ptr = (uint8_t *)&data[0];
buffer.len = strlen(data);
err = send_stream_write(&send_stream, buffer);
if (err != 0) {
fprintf(stderr, "failed to write to stream");
return -1;
}
send_stream_finish(send_stream);
recv_stream_free(recv_stream);
printf("Accepting connection 2\n");
err = connection_accept_bi(&conn2, &send_stream2, &recv_stream2);
if (err != 0) {
fprintf(stderr, "failed to accept streams");
return -1;
}
printf("reading to end\n");
err = recv_stream_read_to_end_timeout(&recv_stream2, &recvBuffer, 1024, 5000);
if (err == ENDPOINT_RESULT_TIMEOUT) {
printf("Response timed out\n");
return 1;
} else if (err != 0) {
printf("Failed to wait for response: %d\n", err);
return 1;
}
char *data2 = "hello world from C - 2";
buffer.ptr = (uint8_t *)&data2[0];
buffer.len = strlen(data2);
err = send_stream_write(&send_stream2, buffer);
if (err != 0) {
fprintf(stderr, "failed to write to stream");
return -1;
}
send_stream_finish(send_stream2);
recv_stream_free(recv_stream2);
rust_buffer_free(recvBuffer);
return 0;
}