Debian Codegraph @

examples/test-c-server-bonding.c

raw
1 /*
2 * SRT - Secure, Reliable, Transport
3 * Copyright (c) 2017 Haivision Systems Inc.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; If not, see <http://www.gnu.org/licenses/>
17 */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21
22
23 #include "srt.h"
24
25 // Note that in this example application there's a listening
26 // socket, off which then a transmission socket is accepted,
27 // then this socket will be used for reading. Therefore the same
28 // function will be used for waiting for the listener to get the
29 // accepted socket ready and then to wait for read-readiness on
30 // the transmission socket. For a model of waiting for write-ready
31 // see test-c-client-bonding.c file.
32 int WaitForReadReady(int eid, SRTSOCKET ss)
33 {
34 int ready_in[2];
35 int ready_in_len = 2;
36 int ready_err[2];
37 int ready_err_len = 2;
38
39 int st = srt_epoll_wait(eid, ready_in, &ready_in_len, ready_err, &ready_err_len, -1,
40 0, 0, 0, 0);
41
42 // Note: with indefinite wait time we can either have a connection reported
43 // or possibly error. Also srt_epoll_wait never returns 0 - at least the number
44 // of ready connections is reported or -1 is returned for error, including timeout.
45 if (st < 1)
46 {
47 fprintf(stderr, "srt_epoll_wait: %s\n", srt_getlasterror_str());
48 return 0;
49 }
50
51 // Check if this was reported as error-ready, in which case it doesn't
52 // matter if read-ready.
53 if (ready_err[0] == ss)
54 {
55 fprintf(stderr, "srt_epoll_wait: socket @%d reported error\n", ss);
56 return 0;
57 }
58
59 if (ready_in[0] != ss)
60 {
61 fprintf(stderr, "srt_epoll_wait: socket @%d not reported ready\n", ss);
62 return 0;
63 }
64
65 return 1;
66 }
67
68 int main(int argc, char** argv)
69 {
70 int globstatus = 0;
71
72 int ss, st;
73 struct sockaddr_in sa;
74 int yes = 1;
75 struct sockaddr_storage their_addr;
76 SRT_SOCKGROUPDATA* grpdata = NULL;
77
78 if (argc < 3 || argc > 4)
79 {
80 fprintf(stderr, "Usage: %s <host> <port> [options]\n", argv[0]);
81 return 1;
82 }
83
84 printf("srt startup\n");
85 srt_startup();
86 // Since now, srt_cleanup() must be done before exiting.
87
88 printf("srt socket\n");
89 ss = srt_create_socket();
90 if (ss == SRT_ERROR)
91 {
92 fprintf(stderr, "srt_socket: %s\n", srt_getlasterror_str());
93 globstatus = 1;
94 goto cleanup;
95 }
96 // Now that the socket is created, jump to 'end' on error.
97
98 // Check options
99 int is_nonblocking = 0;
100 SRTSOCKET their_fd = SRT_INVALID_SOCK; // declared early because of gotos
101 int eid = -1;
102 int lsn_modes = SRT_EPOLL_IN | SRT_EPOLL_ERR;
103 int read_modes = lsn_modes;
104 if (argc > 3)
105 {
106 const char* opt = argv[3];
107 if (strchr(opt, 'n'))
108 is_nonblocking = 1;
109 }
110
111 printf("srt bind address\n");
112 if (0 == strcmp(argv[1], "0"))
113 {
114 memset(&sa, 0, sizeof sa);
115 }
116 else if (inet_pton(AF_INET, argv[1], &sa.sin_addr) != 1)
117 {
118 fprintf(stderr, "srt_bind: Can't resolve address: %s\n", argv[1]);
119 globstatus = 1;
120 goto end;
121 }
122 sa.sin_family = AF_INET;
123 sa.sin_port = htons(atoi(argv[2]));
124
125 printf("srt setsockflag: groupconnect\n");
126 srt_setsockflag(ss, SRTO_GROUPCONNECT, &yes, sizeof yes);
127
128 printf("srt bind\n");
129 st = srt_bind(ss, (struct sockaddr*)&sa, sizeof sa);
130 if (st == SRT_ERROR)
131 {
132 fprintf(stderr, "srt_bind: %s\n", srt_getlasterror_str());
133 globstatus = 1;
134 goto end;
135 }
136
137 if (is_nonblocking)
138 {
139 int blockingmode = 0;
140 srt_setsockflag(ss, SRTO_RCVSYN, &blockingmode, sizeof (blockingmode));
141 eid = srt_epoll_create();
142 srt_epoll_add_usock(eid, ss, &lsn_modes);
143 }
144
145 printf("srt listen\n");
146
147 // We set here 10, just for a case. Every unit in this number
148 // defines a maximum number of connections that can be pending
149 // simultaneously - it doesn't matter here if particular connection
150 // will belong to a bonding group or will be a single-socket connection.
151 st = srt_listen(ss, 10);
152 if (st == SRT_ERROR)
153 {
154 fprintf(stderr, "srt_listen: %s\n", srt_getlasterror_str());
155 globstatus = 1;
156 goto end;
157 }
158
159
160 // In this example, there will be prepared an array of 10 items.
161 // The listener, however, doesn't know how many member connections
162 // one bonded connection will contain, so a real application should be
163 // prepared for dynamically adjusting the array size.
164 const size_t N = 10;
165 grpdata = calloc(N, sizeof (SRT_SOCKGROUPDATA));
166
167 // In non-blocking mode you can't call srt_accept immediately.
168 // You must first wait for readiness on the listener socket.
169 if (is_nonblocking)
170 {
171 printf("srt wait for listener socket reporting in a new connection\n");
172 if (!WaitForReadReady(eid, ss))
173 goto end;
174 }
175
176 printf("srt accept\n");
177 int addr_size = sizeof their_addr;
178 their_fd = srt_accept(ss, (struct sockaddr *)&their_addr, &addr_size);
179
180 if (their_fd == -1)
181 {
182 fprintf(stderr, "srt_accept: %s\n", srt_getlasterror_str());
183 goto end;
184 }
185
186 printf("accepted socket: @%d\n", their_fd);
187
188 // You never know if `srt_accept` is going to give you a socket or a group.
189 // You have to check it on your own. The SRTO_GROUPCONNECT flag doesn't disallow
190 // single socket connections.
191 int isgroup = their_fd & SRTGROUP_MASK;
192
193 if (!isgroup)
194 {
195 fprintf(stderr, "srt_accept: Accepted @%d is not a group???\n", their_fd);
196 goto end;
197 }
198
199 if (is_nonblocking)
200 {
201 // NOTE: The SRTO_RCVSYN = false flag will be derived from
202 // the listener socket and we are going to read, so it matches
203 // the need. In case when you'd like to write to the accepted
204 // socket, you'd have to set also SRTO_SNDSYN = false.
205 srt_epoll_add_usock(eid, their_fd, &read_modes);
206
207 // The listener socket is no longer important.
208 srt_epoll_remove_usock(eid, ss);
209 }
210
211 // Still, use the same procedure for receiving, no matter if
212 // this is a bonded or single connection.
213 int i;
214 for (i = 0; i < 100; i++)
215 {
216 printf("srt recvmsg #%d... ",i);
217 char msg[2048];
218 SRT_MSGCTRL mc = srt_msgctrl_default;
219 mc.grpdata = grpdata;
220 mc.grpdata_size = N;
221
222 if (is_nonblocking)
223 {
224 // Block in epoll as srt_recvmsg2 will not block.
225 if (!WaitForReadReady(eid, their_fd))
226 goto end;
227 }
228
229 st = srt_recvmsg2(their_fd, msg, sizeof msg, &mc);
230 if (st == SRT_ERROR)
231 {
232 fprintf(stderr, "srt_recvmsg: %s\n", srt_getlasterror_str());
233 goto end;
234 }
235
236 printf("Got msg of len %d << %s (%s)\n", st, msg, isgroup ? "group" : "single");
237
238 if (!isgroup)
239 continue;
240
241 if (!mc.grpdata)
242 printf("Group status: [%zi] members > %zi, can't handle.\n", mc.grpdata_size, N);
243 else
244 {
245 printf(" ++ Group status [%zi]: ", mc.grpdata_size);
246 size_t z;
247 for (z = 0; z < mc.grpdata_size; ++z)
248 {
249 printf( "[%zd] result=%d state=%s ", z, mc.grpdata[z].result,
250 mc.grpdata[z].sockstate <= SRTS_CONNECTING ? "pending" :
251 mc.grpdata[z].sockstate == SRTS_CONNECTED ? "connected" : "broken");
252 }
253 printf("\n");
254 }
255 }
256
257 end:
258 if (eid != -1)
259 {
260 srt_epoll_release(eid);
261 }
262 free(grpdata);
263 printf("srt close\n");
264 st = srt_close(their_fd); // just for a case; broken socket should be wiped out anyway
265 if (st == SRT_ERROR)
266 {
267 fprintf(stderr, "srt_close: %s\n", srt_getlasterror_str());
268 // But not matter, we're finishing here.
269 }
270 st = srt_close(ss);
271 if (st == SRT_ERROR)
272 {
273 fprintf(stderr, "srt_close: %s\n", srt_getlasterror_str());
274 // But not matter, we're finishing here.
275 }
276
277 cleanup:
278 printf("srt cleanup\n");
279 srt_cleanup();
280 return globstatus;
281 }