-
Notifications
You must be signed in to change notification settings - Fork 16
/
datacastServer.cpp
343 lines (294 loc) · 13.5 KB
/
datacastServer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/* Provide a TCP server for injecting datacast packet streams */
#include "datacastServer.h"
using namespace ttx;
DatacastServer::DatacastServer(ttx::Configure *configure, vbit::Debug *debug) :
_debug(debug),
_portNumber(configure->GetDatacastServerPort()),
_isActive(false)
{
/* initialise sockets */
_serverSock = -1;
for (int i=0; i < MAXCLIENTS; i++)
{
_clientSocks[i] = -1;
_clientChannel[i] = -1; // no channel set
}
_datachannel[0]=nullptr; // can't use datachannel 0
for (int dc=1; dc<16; dc++)
{
_datachannel[dc] = new vbit::PacketDatacast(dc, configure); // create 15 datacast channels
}
}
DatacastServer::~DatacastServer()
{
}
void DatacastServer::SocketError(std::string errorMessage)
{
if (_serverSock >= 0)
{
#ifdef WIN32
closesocket(_serverSock);
#else
close(_serverSock);
#endif
}
for (int i = 0; i < MAXCLIENTS; i++)
{
if (_clientSocks[i] >= 0)
#ifdef WIN32
closesocket(_clientSocks[i]);
#else
close(_clientSocks[i]);
#endif
_clientChannel[i] = -1; // no channel set
}
std::cerr << errorMessage;
}
void DatacastServer::run()
{
_debug->Log(vbit::Debug::LogLevels::logDEBUG,"[DatacastServer::run] Datacast server thread started");
int newSock;
int sock;
struct sockaddr_in address;
char readBuffer[256];
fd_set readfds;
#ifdef WIN32
int addrlen;
WSADATA wsaData;
int iResult;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0)
{
SocketError("[DatacastServer::run] WSAStartup failed\n");
return;
}
#else
unsigned int addrlen;
#endif
/* Create socket */
if ((_serverSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
{
SocketError("[DatacastServer::run] socket() failed\n");
return;
}
int reuse = true;
/* Allow multiple connnections */
if(setsockopt(_serverSock, SOL_SOCKET, SO_REUSEADDR, (const char *)&reuse, sizeof(reuse)) < 0)
{
SocketError("[DatacastServer::run] setsockopt() SO_REUSEADDR failed\n");
return;
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_ANY);
address.sin_port = htons(_portNumber);
/* bind socked */
if (bind(_serverSock, (struct sockaddr *) &address, sizeof(address)) < 0)
{
SocketError("[DatacastServer::run] bind() failed\n");
return;
}
/* Listen for incoming connections */
if (listen(_serverSock, MAXPENDING) < 0)
{
SocketError("[DatacastServer::run] listen() failed\n");
return;
}
addrlen = sizeof(address);
while(true)
{
FD_ZERO(&readfds);
FD_SET(_serverSock, &readfds);
bool active = false;
for (int i = 0; i < MAXCLIENTS; i++)
{
sock = _clientSocks[i];
if(sock >= 0){
FD_SET(sock , &readfds);
active = true; /* packet server has connections */
}
}
_isActive = active;
/* wait for activity on any socket */
if ((select(FD_SETSIZE, &readfds, NULL, NULL, NULL) < 0) && (errno!=EINTR))
SocketError("[DatacastServer::run] select() failed");
if (FD_ISSET(_serverSock, &readfds))
{
/* incoming connection to server */
if ((newSock = accept(_serverSock, (struct sockaddr *)&address, &addrlen))<0)
{
SocketError("[DatacastServer::run] accept() failed");
return;
}
#ifdef WIN32
u_long ul = 1;
if (ioctlsocket(newSock, FIONBIO, &ul) < 0)
{
SocketError("[DatacastServer::run] ioctlsocket() failed");
return;
}
#else
if (fcntl(newSock, F_SETFL, fcntl(newSock, F_GETFL, 0) | O_NONBLOCK) < 0)
{
SocketError("[DatacastServer::run] fcntl() failed");
return;
}
#endif
for (int i = 0; i <= MAXCLIENTS; i++)
{
if (i == MAXCLIENTS)
{
/* no more client slots so reject */
#ifdef WIN32
closesocket(newSock);
#else
close(newSock);
#endif
_debug->Log(vbit::Debug::LogLevels::logWARN,"[DatacastServer::run] reject new connection from " + std::string(inet_ntoa(address.sin_addr)) + " (too many connections)");
break;
}
/* find unused slot */
if( _clientSocks[i] < 0 )
{
/* add to active sockets */
_clientSocks[i] = newSock;
_clientChannel[i] = -1; // no channel set
_debug->Log(vbit::Debug::LogLevels::logINFO,"[DatacastServer::run] new connection from " + std::string(inet_ntoa(address.sin_addr)) + ":" + std::to_string(ntohs(address.sin_port)) + " as socket " + std::to_string(newSock));
break;
}
}
}
else
{
/* a client socket has activity */
for (int i = 0; i < MAXCLIENTS; i++)
{
sock = _clientSocks[i];
if (sock >= 0 && FD_ISSET(sock , &readfds))
{
/* socket has activity */
int n = recv(sock, readBuffer, 1, MSG_PEEK); // peek at first byte of message
if (n == 1)
{
// byte 0 of message is message length
int len = (uint8_t)readBuffer[0];
n = recv(sock, readBuffer, len, 0); // try to read whole message
if (n == len)
{
std::vector<uint8_t> res = {DCOK};
// byte 1 of message is command number
switch (readBuffer[1]){
case DCSET: // set datacast channel
{
int ch = readBuffer[2];
if (n == 3 && ch >= 0 && ch <= 15)
{
_clientChannel[i] = -1; // release a current datachannel
// check if another client is using desired datachannel
for (int j=0; j<MAXCLIENTS; j++){
if (_clientChannel[j] == ch)
goto DCSETError; // jump out to return error
}
_clientChannel[i] = ch; // use requested channel
std::cerr << "set datachannel " << std::hex << ch << std::endl;
break;
}
DCSETError:
res[0] = DCERR;
break;
}
case DCRAW: // push raw datacast packet to buffer
{
if (_clientChannel[i] > 0 && n == 42) // 40 bytes of packet data
{
std::vector<uint8_t> data(readBuffer+2, readBuffer+n);
if(_datachannel[_clientChannel[i]]->PushRaw(&data))
{
res[0] = DCFULL; // buffer full
}
}
else
{
res[0] = DCERR;
}
break;
}
case DCFORMATA: // encode and push a format A datacast payload to buffer
{
/* Does _not_ automate repeats, continuity indicator, etc.
Format is:
byte 2: bits 4-7 IAL, bits 1-3 flags RI,CI,DL
byte 3-5: 24 bit Service Packet Address (little endian)
byte 6: Repeat indicator
byte 7: Continuity indicator
byte 8+: payload data
*/
if (_clientChannel[i] > 0 && n > 8)
{
uint8_t flags = readBuffer[2] & 0xe;
uint8_t ial = readBuffer[2] >> 4;
uint32_t spa = (uint8_t)readBuffer[3] | ((uint8_t)readBuffer[4] << 8) | ((uint8_t)readBuffer[5] << 16);
uint8_t ri = readBuffer[6];
uint8_t ci = readBuffer[7];
std::vector<uint8_t> data(readBuffer+8, readBuffer+n);
int bytes = _datachannel[_clientChannel[i]]->PushIDLA(flags, ial, spa, ri, ci, &data);
if (bytes == 0) // buffer full
res[0] = DCFULL;
else if (bytes < n-8) // payload didn't fit
res[0] = DCTRUNC; // warn of truncation
res.push_back(bytes); // return number of bytes written
break;
}
else
{
res[0] = DCERR;
res.push_back(0); // no bytes written
}
break;
}
default: // unknown command
{
res[0] = DCERR;
break;
}
}
if (res.size() > 254)
{
_debug->Log(vbit::Debug::LogLevels::logERROR,"[DatacastServer::run] Response too long");
res.resize(254); // truncate!
}
res.insert(res.begin(), res.size()+1); // prepend message size
int n = send(sock, (char*)res.data(), res.size(), 0); // send response
if (n > 0)
continue; // next socket in loop
}
// else fall through to error handling
}
// couldn't read/write all bytes
getpeername(sock, (struct sockaddr*)&address, &addrlen);
if (n == 0)
{
/* client disconnected */
_debug->Log(vbit::Debug::LogLevels::logINFO,"[DatacastServer::run] closing connection from " + std::string(inet_ntoa(address.sin_addr)) + ":" + std::to_string(ntohs(address.sin_port)) + " on socket " + std::to_string(sock));
}
else
{
#ifdef WIN32
int e = WSAGetLastError();
#else
int e = errno;
#endif
_debug->Log(vbit::Debug::LogLevels::logWARN,"[DatacastServer::run] closing connection from " + std::string(inet_ntoa(address.sin_addr)) + ":" + std::to_string(ntohs(address.sin_port)) + " recv error " + std::to_string(e) + " on socket " + std::to_string(sock));
}
/* close the socket when any error occurs */
_clientSocks[i] = -1; /* free slot */
#ifdef WIN32
closesocket(sock);
#else
close(sock);
#endif
}
}
}
}
}