Debian Codegraph @

examples/example-client-nonblock.c

raw
1 /*
2 * SRT - Secure, Reliable, Transport
3 * Copyright (c) 2021 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 int main(int argc, char** argv)
31 {
32 int ss, st;
33 struct sockaddr_in sa;
34 const int no = 0;
35 const char message [] = "This message should be sent to the other side";
36
37 if (argc != 3) {
38 fprintf(stderr, "Usage: %s <host> <port>\n", argv[0]);
39 return 1;
40 }
41
42 printf("SRT startup\n");
43 srt_startup();
44
45 printf("Creating SRT socket\n");
46 ss = srt_create_socket();
47 if (ss == SRT_ERROR)
48 {
49 fprintf(stderr, "srt_socket: %s\n", srt_getlasterror_str());
50 return 1;
51 }
52
53 printf("Creating remote address\n");
54 sa.sin_family = AF_INET;
55 sa.sin_port = htons(atoi(argv[2]));
56 if (inet_pton(AF_INET, argv[1], &sa.sin_addr) != 1)
57 {
58 return 1;
59 }
60
61 int epollid = srt_epoll_create();
62 if (epollid == -1)
63 {
64 fprintf(stderr, "srt_epoll_create: %s\n", srt_getlasterror_str());
65 return 1;
66 }
67
68 printf("srt setsockflag\n");
69 if (SRT_ERROR == srt_setsockflag(ss, SRTO_RCVSYN, &no, sizeof no)
70 || SRT_ERROR == srt_setsockflag(ss, SRTO_SNDSYN, &no, sizeof no))
71 {
72 fprintf(stderr, "SRTO_SNDSYN or SRTO_RCVSYN: %s\n", srt_getlasterror_str());
73 return 1;
74 }
75
76 // When a caller is connected, a write-readiness event is triggered.
77 int modes = SRT_EPOLL_OUT | SRT_EPOLL_ERR;
78 if (SRT_ERROR == srt_epoll_add_usock(epollid, ss, &modes))
79 {
80 fprintf(stderr, "srt_epoll_add_usock: %s\n", srt_getlasterror_str());
81 return 1;
82 }
83
84 printf("srt connect\n");
85 st = srt_connect(ss, (struct sockaddr*)&sa, sizeof sa);
86 if (st == SRT_ERROR)
87 {
88 fprintf(stderr, "srt_connect: %s\n", srt_getlasterror_str());
89 return 1;
90 }
91
92 // We had subscribed for write-readiness or error.
93 // Write readiness comes in wready array,
94 // error is notified via rready in this case.
95 int rlen = 1;
96 SRTSOCKET rready;
97 int wlen = 1;
98 SRTSOCKET wready;
99 if (srt_epoll_wait(epollid, &rready, &rlen, &wready, &wlen, -1, 0, 0, 0, 0) != -1)
100 {
101 SRT_SOCKSTATUS state = srt_getsockstate(ss);
102 if (state != SRTS_CONNECTED || rlen > 0) // rlen > 0 - an error notification
103 {
104 fprintf(stderr, "srt_epoll_wait: reject reason %s\n", srt_rejectreason_str(srt_getrejectreason(rready)));
105 return 1;
106 }
107
108 if (wlen != 1 || wready != ss)
109 {
110 fprintf(stderr, "srt_epoll_wait: wlen %d, wready %d, socket %d\n", wlen, wready, ss);
111 return 1;
112 }
113 }
114 else
115 {
116 fprintf(stderr, "srt_connect: %s\n", srt_getlasterror_str());
117 return 1;
118 }
119
120 int i;
121 for (i = 0; i < 100; i++)
122 {
123 rready = SRT_INVALID_SOCK;
124 rlen = 1;
125 wready = SRT_INVALID_SOCK;
126 wlen = 1;
127
128 // As we have subscribed only for write-readiness or error events,
129 // but have not subscribed for read-readiness,
130 // through readfds we are notified about an error.
131 int timeout_ms = 5000; // ms
132 int res = srt_epoll_wait(epollid, &rready, &rlen, &wready, &wlen, timeout_ms, 0, 0, 0, 0);
133 if (res == SRT_ERROR || rlen > 0)
134 {
135 fprintf(stderr, "srt_epoll_wait: %s\n", srt_getlasterror_str());
136 return 1;
137 }
138
139 printf("srt sendmsg2 #%d >> %s\n", i, message);
140 st = srt_sendmsg2(ss, message, sizeof message, NULL);
141 if (st == SRT_ERROR)
142 {
143 fprintf(stderr, "srt_sendmsg: %s\n", srt_getlasterror_str());
144 return 1;
145 }
146
147 usleep(1000); // 1 ms
148 }
149
150 // Let's wait a bit so that all packets reach destination
151 usleep(100000); // 100 ms
152
153 // In live mode the connection will be closed even if some packets were not yet acknowledged.
154 printf("srt close\n");
155 st = srt_close(ss);
156 if (st == SRT_ERROR)
157 {
158 fprintf(stderr, "srt_close: %s\n", srt_getlasterror_str());
159 return 1;
160 }
161
162 printf("srt cleanup\n");
163 srt_cleanup();
164 return 0;
165 }