Skip to content

Commit aba73de

Browse files
authored
[apps] Added examples for message mode transmission (Haivision#2592).
Fixed example build issues.
1 parent a7e4204 commit aba73de

File tree

7 files changed

+432
-13
lines changed

7 files changed

+432
-13
lines changed

.github/workflows/cxx11-ubuntu.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- name: configure
1717
run: |
1818
mkdir _build && cd _build
19-
cmake ../ -DENABLE_STDCXX_SYNC=ON -DENABLE_UNITTESTS=ON -DENABLE_BONDING=ON
19+
cmake ../ -DENABLE_STDCXX_SYNC=ON -DENABLE_UNITTESTS=ON -DENABLE_BONDING=ON -DENABLE_TESTING=ON -DENABLE_EXAMPLES=ON
2020
- name: build
2121
run: cd _build && cmake --build ./
2222
- name: test

CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -1339,6 +1339,10 @@ if (ENABLE_EXAMPLES)
13391339

13401340
srt_add_example(recvfile.cpp)
13411341

1342+
srt_add_example(sendmsg.cpp)
1343+
1344+
srt_add_example(recvmsg.cpp)
1345+
13421346
srt_add_example(test-c-client.c)
13431347

13441348
srt_add_example(example-client-nonblock.c)

examples/recvlive.cpp

-10
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,6 @@ int main(int argc, char* argv[])
5353
return 0;
5454
}
5555

56-
// SRT requires that third argument is always SOCK_DGRAM. The Stream API is set by an option,
57-
// although there's also lots of other options to be set, for which there's a convenience option,
58-
// SRTO_TRANSTYPE.
59-
// SRT_TRANSTYPE tt = SRTT_LIVE;
60-
// if (SRT_ERROR == srt_setsockopt(sfd, 0, SRTO_TRANSTYPE, &tt, sizeof tt))
61-
// {
62-
// cout << "srt_setsockopt: " << srt_getlasterror_str() << endl;
63-
// return 0;
64-
// }
65-
6656
bool no = false;
6757
if (SRT_ERROR == srt_setsockopt(sfd, 0, SRTO_RCVSYN, &no, sizeof no))
6858
{

examples/recvmsg.cpp

+215
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
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:

0 commit comments

Comments
 (0)