Debian Codegraph @

examples/recvmsg.cpp

raw
1 #ifndef WIN32
2 #include <cstdlib>
3 #include <netdb.h>
4 #else
5 #include <winsock2.h>
6 #include <ws2tcpip.h>
7 #endif
8 #include <fstream>
9 #include <iostream>
10 #include <sstream>
11 #include <string>
12 #include <cstring>
13 #include <cassert>
14
15 #include <srt.h>
16 #include <netinet_any.h>
17
18 using namespace std;
19
20 string ShowChar(char in)
21 {
22 if (in >= 32 && in < 127)
23 return string(1, in);
24
25 ostringstream os;
26 os << "<" << hex << uppercase << int(in) << ">";
27 return os.str();
28 }
29
30 string CreateFilename(string fmt, int ord)
31 {
32 ostringstream os;
33
34 size_t pos = fmt.find('%');
35 if (pos == string::npos)
36 os << fmt << ord << ".out";
37 else
38 {
39 os << fmt.substr(0, pos) << ord << fmt.substr(pos+1);
40 }
41 return os.str();
42 }
43
44 int main(int argc, char* argv[])
45 {
46 string service("9000");
47 if (argc > 1)
48 service = argv[1];
49
50 if (service == "--help")
51 {
52 cout << "usage: recvmsg [server_port] [filepattern]" << endl;
53 return 0;
54 }
55
56 addrinfo hints;
57 addrinfo* res;
58
59 memset(&hints, 0, sizeof(struct addrinfo));
60 hints.ai_flags = AI_PASSIVE;
61 hints.ai_family = AF_INET;
62 hints.ai_socktype = SOCK_DGRAM;
63
64 if (0 != getaddrinfo(NULL, service.c_str(), &hints, &res))
65 {
66 cout << "illegal port number or port is busy.\n" << endl;
67 return 1;
68 }
69
70 string outfileform;
71 if (argc > 2)
72 {
73 outfileform = argv[2];
74 }
75
76 // use this function to initialize the UDT library
77 srt_startup();
78
79 srt_setloglevel(srt_logging::LogLevel::debug);
80
81 SRTSOCKET sfd = srt_create_socket();
82 if (SRT_INVALID_SOCK == sfd)
83 {
84 cout << "srt_socket: " << srt_getlasterror_str() << endl;
85 return 1;
86 }
87
88 int file_mode = SRTT_FILE;
89 if (SRT_ERROR == srt_setsockflag(sfd, SRTO_TRANSTYPE, &file_mode, sizeof file_mode))
90 {
91 cout << "srt_setsockopt: " << srt_getlasterror_str() << endl;
92 return 1;
93 }
94
95 bool message_mode = true;
96 if (SRT_ERROR == srt_setsockflag(sfd, SRTO_MESSAGEAPI, &message_mode, sizeof message_mode))
97 {
98 cout << "srt_setsockopt: " << srt_getlasterror_str() << endl;
99 return 1;
100 }
101
102 if (SRT_ERROR == srt_bind(sfd, res->ai_addr, res->ai_addrlen))
103 {
104 cout << "srt_bind: " << srt_getlasterror_str() << endl;
105 return 0;
106 }
107
108 freeaddrinfo(res);
109
110 cout << "server is ready at port: " << service << endl;
111
112 if (SRT_ERROR == srt_listen(sfd, 10))
113 {
114 cout << "srt_listen: " << srt_getlasterror_str() << endl;
115 return 1;
116 }
117
118 char data[4096];
119
120 srt::sockaddr_any remote;
121
122 int afd = srt_accept(sfd, remote.get(), &remote.len);
123
124 if (afd == SRT_INVALID_SOCK)
125 {
126 cout << "srt_accept: " << srt_getlasterror_str() << endl;
127 return 1;
128 }
129
130 cout << "Connection from " << remote.str() << " established\n";
131
132 bool save_to_files = true;
133
134 if (outfileform != "")
135 save_to_files = true;
136
137 int ordinal = 1;
138
139 // the event loop
140 while (true)
141 {
142 SRT_SOCKSTATUS status = srt_getsockstate(afd);
143 if ((status == SRTS_BROKEN) ||
144 (status == SRTS_NONEXIST) ||
145 (status == SRTS_CLOSED))
146 {
147 cout << "source disconnected. status=" << status << endl;
148 srt_close(afd);
149 break;
150 }
151
152 int ret = srt_recvmsg(afd, data, sizeof(data));
153 if (ret == SRT_ERROR)
154 {
155 cout << "srt_recvmsg: " << srt_getlasterror_str() << endl;
156 break;
157 }
158 if (ret == 0)
159 {
160 cout << "EOT\n";
161 break;
162 }
163
164 if (ret < 5)
165 {
166 cout << "WRONG MESSAGE SYNTAX\n";
167 break;
168 }
169
170 if (save_to_files)
171 {
172 string fname = CreateFilename(outfileform, ordinal++);
173 ofstream ofile(fname);
174
175 if (!ofile.good())
176 {
177 cout << "ERROR: can't create file: " << fname << " - skipping message\n";
178 continue;
179 }
180
181 ofile.write(data, ret);
182 ofile.close();
183 cout << "Written " << ret << " bytes of message to " << fname << endl;
184 }
185 else
186 {
187 union
188 {
189 char chars[4];
190 int32_t intval;
191 } first4;
192
193 copy(data, data + 4, first4.chars);
194
195 cout << "[" << ret << "B " << ntohl(first4.intval) << "] ";
196 for (int i = 4; i < ret; ++i)
197 cout << ShowChar(data[i]);
198 cout << endl;
199 }
200 }
201
202 srt_close(afd);
203 srt_close(sfd);
204
205 // use this function to release the UDT library
206 srt_cleanup();
207
208 return 0;
209 }
210
211 // Local Variables:
212 // c-file-style: "ellemtel"
213 // c-basic-offset: 3
214 // compile-command: "g++ -Wall -O2 -std=c++11 -I.. -I../srtcore -o recvlive recvlive.cpp -L.. -lsrt -lpthread -L/usr/local/opt/openssl/lib -lssl -lcrypto"
215 // End: