Debian Codegraph @

examples/sendmsg.cpp

raw
1 #ifndef _WIN32
2 #include <arpa/inet.h>
3 #include <netdb.h>
4 #else
5 #include <winsock2.h>
6 #include <ws2tcpip.h>
7 #endif
8 #include <fstream>
9 #include <iostream>
10 #include <cstdlib>
11 #include <string>
12 #include <sstream>
13 #include <vector>
14 #include <iterator>
15 #include <algorithm>
16 #include <srt.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 int main(int argc, char* argv[])
31 {
32 if ((argc != 4) || (0 == atoi(argv[2])))
33 {
34 cout << "usage: sendmsg server_ip server_port source_filename" << endl;
35 return -1;
36 }
37
38 // Use this function to initialize the UDT library
39 srt_startup();
40
41 srt_setloglevel(srt_logging::LogLevel::debug);
42
43 struct addrinfo hints, *peer;
44
45 memset(&hints, 0, sizeof(struct addrinfo));
46 hints.ai_flags = AI_PASSIVE;
47 hints.ai_family = AF_INET;
48 hints.ai_socktype = SOCK_DGRAM;
49
50 SRTSOCKET fhandle = srt_create_socket();
51 // SRT requires that third argument is always SOCK_DGRAM. The Stream API is set by an option,
52 // although there's also lots of other options to be set, for which there's a convenience option,
53 // SRTO_TRANSTYPE.
54 SRT_TRANSTYPE tt = SRTT_FILE;
55 srt_setsockopt(fhandle, 0, SRTO_TRANSTYPE, &tt, sizeof tt);
56
57 bool message_mode = true;
58 srt_setsockopt(fhandle, 0, SRTO_MESSAGEAPI, &message_mode, sizeof message_mode);
59
60 if (0 != getaddrinfo(argv[1], argv[2], &hints, &peer))
61 {
62 cout << "incorrect server/peer address. " << argv[1] << ":" << argv[2] << endl;
63 return -1;
64 }
65
66 // Connect to the server, implicit bind.
67 if (SRT_ERROR == srt_connect(fhandle, peer->ai_addr, peer->ai_addrlen))
68 {
69 int rej = srt_getrejectreason(fhandle);
70 cout << "connect: " << srt_getlasterror_str() << ":" << srt_rejectreason_str(rej) << endl;
71 return -1;
72 }
73
74 freeaddrinfo(peer);
75
76 string source_fname = argv[3];
77
78 bool use_filelist = false;
79 if (source_fname[0] == '+')
80 {
81 use_filelist = true;
82 source_fname = source_fname.substr(1);
83 }
84
85 istream* pin;
86 ifstream fin;
87 if (source_fname == "-")
88 {
89 pin = &cin;
90 }
91 else
92 {
93 fin.open(source_fname.c_str());
94 pin = &fin;
95 }
96
97 // The syntax is:
98 //
99 // 1. If the first character is +, it's followed by TTL in milliseconds.
100 // 2. Otherwise the first number is the ID, followed by a space, to be filled in first 4 bytes.
101 // 3. Rest of the characters, up to the end of line, should be put into a solid block and sent at once.
102
103 int status = 0;
104
105 int ordinal = 1;
106 int lpos = 0;
107
108 for (;;)
109 {
110 string line;
111 int32_t id = 0;
112 int ttl = -1;
113
114 getline(*pin, (line));
115 if (pin->eof())
116 break;
117
118 // Interpret
119 if (line.size() < 2)
120 continue;
121
122 if (use_filelist)
123 {
124 // The line should contain [+TTL] FILENAME
125 char fname[1024] = "";
126 if (line[0] == '+')
127 {
128 int nparsed = sscanf(line.c_str(), "+%d %n%1000s", &ttl, &lpos, fname);
129 if (nparsed != 2)
130 {
131 cout << "ERROR: syntax error in input (" << nparsed << " parsed pos=" << lpos << ")\n";
132 status = SRT_ERROR;
133 break;
134 }
135 line = fname;
136 }
137
138 ifstream ifile (line);
139
140 id = ordinal;
141 ++ordinal;
142
143 if (!ifile.good())
144 {
145 cout << "ERROR: file '" << line << "' cannot be read, skipping\n";
146 continue;
147 }
148
149 line = string(istreambuf_iterator<char>(ifile), istreambuf_iterator<char>());
150 }
151 else
152 {
153 int lpos2 = 0;
154
155 int nparsed = 0;
156 if (line[0] == '+')
157 {
158 nparsed = sscanf(line.c_str(), "+%d %d %n%*s", &ttl, &id, &lpos2);
159 if (nparsed != 2)
160 {
161 cout << "ERROR: syntax error in input (" << nparsed << " parsed pos=" << lpos2 << ")\n";
162 status = SRT_ERROR;
163 break;
164 }
165 }
166 else
167 {
168 nparsed = sscanf(line.c_str(), "%d %n%*s", &id, &lpos2);
169 if (nparsed != 1)
170 {
171 cout << "ERROR: syntax error in input (" << nparsed << " parsed pos=" << lpos2 << ")\n";
172 status = SRT_ERROR;
173 break;
174 }
175 }
176 }
177
178 union
179 {
180 char chars[4];
181 int32_t intval;
182 } first4;
183 first4.intval = htonl(id);
184
185 vector<char> input;
186 copy(first4.chars, first4.chars+4, back_inserter(input));
187 copy(line.begin() + lpos, line.end(), back_inserter(input));
188
189 // CHECK CODE
190 // cout << "WILL SEND: TTL=" << ttl << " ";
191 // transform(input.begin(), input.end(),
192 // ostream_iterator<string>(cout), ShowChar);
193 // cout << endl;
194
195 int nsnd = srt_sendmsg(fhandle, &input[0], input.size(), ttl, false);
196 if (nsnd == SRT_ERROR)
197 {
198 cout << "SRT ERROR: " << srt_getlasterror_str() << endl;
199 status = SRT_ERROR;
200 break;
201 }
202 }
203
204 srt_close(fhandle);
205
206 // Signal to the SRT library to clean up all allocated sockets and resources.
207 srt_cleanup();
208
209 return status;
210 }