| 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 |
|
| 20 |
#include <stdio.h> |
| 21 |
#include <stdlib.h> |
| 22 |
#ifdef _WIN32 |
| 23 |
#define usleep(x) Sleep(x / 1000) |
| 24 |
#else |
| 25 |
#include <unistd.h> |
| 26 |
#endif |
| 27 |
|
| 28 |
#include "srt.h" |
| 29 |
|
| 30 |
struct |
| 31 |
{ |
| 32 |
const char* name; |
| 33 |
int gtype; |
| 34 |
} group_types [] = { |
| 35 |
{ |
| 36 |
"broadcast", SRT_GTYPE_BROADCAST |
| 37 |
} |
| 38 |
// Others will follow |
| 39 |
}; |
| 40 |
|
| 41 |
#define SIZE(array) (sizeof array/sizeof(array[0])) |
| 42 |
|
| 43 |
// Note that in this example application there's a socket |
| 44 |
// used first to connect to the service and then it will be |
| 45 |
// used for writing. Therefore the same function will be used |
| 46 |
// for waiting for the socket to be connected and then to wait |
| 47 |
// for write-ready on the socket used for transmission. For a |
| 48 |
// model of waiting for read-ready see test-c-server-bonding.c file. |
| 49 |
int WaitForWriteReady(int eid, SRTSOCKET ss) |
| 50 |
{ |
| 51 |
int ready_err[2]; |
| 52 |
int ready_err_len = 2; |
| 53 |
int ready_out[2]; |
| 54 |
int ready_out_len = 2; |
| 55 |
|
| 56 |
int st = srt_epoll_wait(eid, ready_err, &ready_err_len, ready_out, &ready_out_len, -1, |
| 57 |
0, 0, 0, 0); |
| 58 |
|
| 59 |
// Note: with indefinite wait time we can either have a connection reported |
| 60 |
// or possibly error. Also srt_epoll_wait never returns 0 - at least the number |
| 61 |
// of ready connections is reported or -1 is returned for error, including timeout. |
| 62 |
if (st < 1) |
| 63 |
{ |
| 64 |
fprintf(stderr, "srt_epoll_wait: %s\n", srt_getlasterror_str()); |
| 65 |
return 0; |
| 66 |
} |
| 67 |
|
| 68 |
// Check if this was reported as error-ready, in which case it doesn't |
| 69 |
// matter if read-ready. |
| 70 |
if (ready_err[0] == ss) |
| 71 |
{ |
| 72 |
int reason = srt_getrejectreason(ss); |
| 73 |
fprintf(stderr, "srt_epoll_wait: socket @%d reported error reason=%d: %s\n", ss, reason, srt_rejectreason_str(reason)); |
| 74 |
return 0; |
| 75 |
} |
| 76 |
|
| 77 |
return 1; |
| 78 |
} |
| 79 |
|
| 80 |
int main(int argc, char** argv) |
| 81 |
{ |
| 82 |
int ss, st; |
| 83 |
struct sockaddr_in sa; |
| 84 |
//int yes = 1; // for options, none needed so far |
| 85 |
const char message [] = "This message should be sent to the other side"; |
| 86 |
|
| 87 |
if (argc < 3) |
| 88 |
{ |
| 89 |
fprintf(stderr, "Usage: %s <type> {<host> <port>}... \n", argv[0]); |
| 90 |
return 1; |
| 91 |
} |
| 92 |
|
| 93 |
int gtype = SRT_GTYPE_BROADCAST; |
| 94 |
size_t i; |
| 95 |
for (i = 0; i < SIZE(group_types); ++i) |
| 96 |
if (0 == strcmp(group_types[i].name, argv[1])) |
| 97 |
{ |
| 98 |
gtype = group_types[i].gtype; |
| 99 |
break; |
| 100 |
} |
| 101 |
|
| 102 |
int is_nonblocking = 0; |
| 103 |
size_t nmemb = argc - 2; |
| 104 |
if (nmemb < 2) |
| 105 |
{ |
| 106 |
fprintf(stderr, "Usage error: no members specified\n"); |
| 107 |
return 1; |
| 108 |
} |
| 109 |
|
| 110 |
if (nmemb % 2) |
| 111 |
{ |
| 112 |
// Last argument is then optionset |
| 113 |
--nmemb; |
| 114 |
const char* opt = argv[argc-1]; |
| 115 |
if (strchr(opt, 'n')) |
| 116 |
is_nonblocking = 1; |
| 117 |
} |
| 118 |
|
| 119 |
nmemb /= 2; |
| 120 |
|
| 121 |
printf("srt startup\n"); |
| 122 |
srt_startup(); |
| 123 |
|
| 124 |
// Declare all variables before any destructive goto. |
| 125 |
// In C++ such a code that jumps over initialization would be illegal, |
| 126 |
// in C it causes an uninitialized value to be used. |
| 127 |
int eid = -1; |
| 128 |
int write_modes = SRT_EPOLL_OUT | SRT_EPOLL_ERR; |
| 129 |
SRT_SOCKGROUPDATA* grpdata = NULL; |
| 130 |
SRT_SOCKGROUPCONFIG* grpconfig = calloc(nmemb, sizeof (SRT_SOCKGROUPCONFIG)); |
| 131 |
|
| 132 |
printf("srt group\n"); |
| 133 |
ss = srt_create_group(gtype); |
| 134 |
if (ss == SRT_ERROR) |
| 135 |
{ |
| 136 |
fprintf(stderr, "srt_create_group: %s\n", srt_getlasterror_str()); |
| 137 |
goto end; |
| 138 |
} |
| 139 |
|
| 140 |
const int B = 2; |
| 141 |
|
| 142 |
for (i = 0; i < nmemb; ++i) |
| 143 |
{ |
| 144 |
printf("srt remote address #%zi\n", i); |
| 145 |
|
| 146 |
sa.sin_family = AF_INET; |
| 147 |
sa.sin_port = htons(atoi(argv[B + 2*i + 1])); |
| 148 |
if (inet_pton(AF_INET, argv[B + 2*i], &sa.sin_addr) != 1) |
| 149 |
{ |
| 150 |
fprintf(stderr, "inet_pton: can't resolve address: %s\n", argv[B + 2*i]); |
| 151 |
goto end; |
| 152 |
} |
| 153 |
|
| 154 |
grpconfig[i] = srt_prepare_endpoint(NULL, (struct sockaddr*)&sa, sizeof sa); |
| 155 |
} |
| 156 |
|
| 157 |
if (is_nonblocking) |
| 158 |
{ |
| 159 |
int blockingmode = 0; |
| 160 |
srt_setsockflag(ss, SRTO_RCVSYN, &blockingmode, sizeof (blockingmode)); |
| 161 |
eid = srt_epoll_create(); |
| 162 |
srt_epoll_add_usock(eid, ss, &write_modes); |
| 163 |
} |
| 164 |
|
| 165 |
printf("srt connect (group)\n"); |
| 166 |
|
| 167 |
// Note: this function unblocks at the moment when at least one connection |
| 168 |
// from the array is established (no matter which one); the others will |
| 169 |
// continue in background. |
| 170 |
st = srt_connect_group(ss, grpconfig, nmemb); |
| 171 |
if (st == SRT_ERROR) |
| 172 |
{ |
| 173 |
fprintf(stderr, "srt_connect: %s\n", srt_getlasterror_str()); |
| 174 |
goto end; |
| 175 |
} |
| 176 |
|
| 177 |
// In non-blocking mode the srt_connect function returns immediately |
| 178 |
// and displays only errors of the initial usage, not runtime errors. |
| 179 |
// These could be reported by epoll. |
| 180 |
if (is_nonblocking) |
| 181 |
{ |
| 182 |
// WRITE-ready means connected |
| 183 |
|
| 184 |
printf("srt wait for socket reporting connection success\n"); |
| 185 |
if (!WaitForWriteReady(eid, ss)) |
| 186 |
goto end; |
| 187 |
} |
| 188 |
|
| 189 |
// In non-blocking mode now is the time to possibly change the epoll. |
| 190 |
// As this socket will be used for writing, it is in the right mode already. |
| 191 |
// Just set the right flag, as for non-blocking connect it needs RCVSYN. |
| 192 |
if (is_nonblocking) |
| 193 |
{ |
| 194 |
int blockingmode = 0; |
| 195 |
srt_setsockflag(ss, SRTO_SNDSYN, &blockingmode, sizeof (blockingmode)); |
| 196 |
} |
| 197 |
|
| 198 |
// Important: Normally you need that at least one link is ready for |
| 199 |
// the group link to be ready. All but first are done actually in |
| 200 |
// background, so this sleep only makes it more probable. If you'd like |
| 201 |
// to make sure that ALL links are established - by some reason - then |
| 202 |
// you'd have to subscribe for epoll event SRT_EPOLL_UPDATE and after the |
| 203 |
// connect function exits do checks by srt_group_data to see if all links |
| 204 |
// are established, and if not, repeat it after srt_epoll_wait for the |
| 205 |
// SRT_EPOLL_UPDATE signal. |
| 206 |
printf("sleeping 1s to make it probable all links are established\n"); |
| 207 |
sleep(1); |
| 208 |
|
| 209 |
grpdata = calloc(nmemb, sizeof (SRT_SOCKGROUPDATA)); |
| 210 |
|
| 211 |
for (i = 0; i < 100; i++) |
| 212 |
{ |
| 213 |
printf("srt sendmsg2 #%zd >> %s\n",i,message); |
| 214 |
|
| 215 |
SRT_MSGCTRL mc = srt_msgctrl_default; |
| 216 |
mc.grpdata = grpdata; |
| 217 |
mc.grpdata_size = nmemb; // Set maximum known |
| 218 |
|
| 219 |
if (is_nonblocking) |
| 220 |
{ |
| 221 |
// Block in epoll as srt_recvmsg2 will not block. |
| 222 |
if (!WaitForWriteReady(eid, ss)) |
| 223 |
goto end; |
| 224 |
} |
| 225 |
|
| 226 |
st = srt_sendmsg2(ss, message, sizeof message, &mc); |
| 227 |
if (st == SRT_ERROR) |
| 228 |
{ |
| 229 |
fprintf(stderr, "srt_sendmsg: %s\n", srt_getlasterror_str()); |
| 230 |
goto end; |
| 231 |
} |
| 232 |
|
| 233 |
// Perform the group check. This can be used to recognize broken connections |
| 234 |
// and probably reestablish them by calling `srt_connect` for them. Here they |
| 235 |
// are only shown. |
| 236 |
printf(" ++ Group status [%zi]:", mc.grpdata_size); |
| 237 |
if (!mc.grpdata) |
| 238 |
{ |
| 239 |
printf(" (ERROR: array too small!)\n"); |
| 240 |
} |
| 241 |
else |
| 242 |
{ |
| 243 |
for (i = 0; i < mc.grpdata_size; ++i) |
| 244 |
{ |
| 245 |
printf( "[%zd] result=%d state=%s ", i, mc.grpdata[i].result, |
| 246 |
mc.grpdata[i].sockstate <= SRTS_CONNECTING ? "pending" : |
| 247 |
mc.grpdata[i].sockstate == SRTS_CONNECTED ? "connected" : "broken"); |
| 248 |
} |
| 249 |
printf("\n"); |
| 250 |
} |
| 251 |
|
| 252 |
usleep(1000); // 1 ms |
| 253 |
} |
| 254 |
|
| 255 |
end: |
| 256 |
if (eid != -1) |
| 257 |
{ |
| 258 |
srt_epoll_release(eid); |
| 259 |
} |
| 260 |
printf("srt close\n"); |
| 261 |
st = srt_close(ss); |
| 262 |
if (st == SRT_ERROR) |
| 263 |
{ |
| 264 |
fprintf(stderr, "srt_close: %s\n", srt_getlasterror_str()); |
| 265 |
return 1; |
| 266 |
} |
| 267 |
|
| 268 |
free(grpdata); |
| 269 |
free(grpconfig); |
| 270 |
|
| 271 |
//cleanup: |
| 272 |
printf("srt cleanup\n"); |
| 273 |
srt_cleanup(); |
| 274 |
return 0; |
| 275 |
} |