-
Notifications
You must be signed in to change notification settings - Fork 16
/
packetServer.cpp
288 lines (238 loc) · 9.87 KB
/
packetServer.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
/* Provide a TCP packet server which sends a whole frame of t42 data at a time to all connected clients */
#include "packetServer.h"
using namespace ttx;
PacketServer::PacketServer(ttx::Configure *configure, vbit::Debug *debug) :
_debug(debug),
_portNumber(configure->GetPacketServerPort()),
_isActive(false)
{
/* initialise sockets */
_serverSock = -1;
for (int i = 0; i < MAXCLIENTS; i++)
_clientSocks[i] = -1;
}
PacketServer::~PacketServer()
{
}
void PacketServer::DieWithError(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
}
perror(errorMessage.c_str());
exit(1);
}
void PacketServer::SendField(std::vector<std::vector<uint8_t>> FrameBuffer)
{
std::array<uint8_t, 42*32> RawFrameBuffer;
RawFrameBuffer.fill(0x00); // clear two frames
unsigned int i, j;
for (i = 0; i < FrameBuffer.size(); i++)
{
int field = FrameBuffer[i][0]&1;
int line = (FrameBuffer[i][1]<<8) + FrameBuffer[i][2];
if (line < 16) // full field lines not supported in this output
{
for (j = 0; j < 42; j++)
{
RawFrameBuffer[(field*42*16)+(line*42)+j] = FrameBuffer[i][j+3];
}
}
}
int sock;
int ret;
for (i = 0; i < MAXCLIENTS; i++)
{
if (_mtx[i].try_lock()) // skip this socket if unable to lock mutex as it's in the process of being closed
{
sock = _clientSocks[i];
if (sock >= 0)
{
ret = send(sock, (char*)RawFrameBuffer.data(), RawFrameBuffer.size(), 0);
if (ret != RawFrameBuffer.size())
{
/*
We were unable to send a whole frame to the client. We either sent a partial frame or the send failed entirely.
This probably means that either there are network issues, or the client is not consuming data fast enough.
Either way trying to handle this adds a lot of complexity and risks getting the client desynchronised or blocking the thread trying to sort it out, so the best thing to do is probably just boot the client off and let it reconnect.
*/
#ifdef WIN32
int e = WSAGetLastError();
#else
int e = errno;
#endif
_debug->Log(vbit::Debug::LogLevels::logWARN,"[PacketServer::SendField] send() failed. Closing socket " + std::to_string(sock) + " send error " + std::to_string(e));
_clientSocks[i] = -1; /* free slot */
#ifdef WIN32
closesocket(sock);
#else
close(sock);
#endif
}
}
_mtx[i].unlock();
}
}
}
void PacketServer::run()
{
_debug->Log(vbit::Debug::LogLevels::logDEBUG,"[PacketServer::run] TCP packet server thread started");
int newSock;
int sock;
struct sockaddr_in address;
unsigned short servPort;
char readBuffer[BUFFLEN];
fd_set readfds;
#ifdef WIN32
int addrlen;
WSADATA wsaData;
int iResult;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0)
{
DieWithError("[PacketServer::run] WSAStartup failed");
}
#else
unsigned int addrlen;
#endif
servPort = _portNumber;
/* Create socket */
if ((_serverSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
DieWithError("[PacketServer::run] socket() failed\n");
int reuse = true;
/* Allow multiple connnections */
if(setsockopt(_serverSock, SOL_SOCKET, SO_REUSEADDR, (const char *)&reuse, sizeof(reuse)) < 0)
DieWithError("[PacketServer::run] setsockopt() SO_REUSEADDR failed");
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_ANY);
address.sin_port = htons(servPort);
/* bind socked */
if (bind(_serverSock, (struct sockaddr *) &address, sizeof(address)) < 0)
DieWithError("[PacketServer::run] bind() failed");
/* Listen for incoming connections */
if (listen(_serverSock, MAXPENDING) < 0)
DieWithError("[PacketServer::run] listen() failed");
addrlen = sizeof(address);
int iopt = 42*32*25; /* 25 frames worth of t42 */
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))
DieWithError("[PacketServer::run] select() failed");
if (FD_ISSET(_serverSock, &readfds))
{
/* incoming connection to server */
if ((newSock = accept(_serverSock, (struct sockaddr *)&address, &addrlen))<0)
DieWithError("[PacketServer::run] accept() failed");
#ifdef WIN32
u_long ul = 1;
if (ioctlsocket(newSock, FIONBIO, &ul) < 0)
DieWithError("[PacketServer::run] ioctlsocket() failed");
#else
if (fcntl(newSock, F_SETFL, fcntl(newSock, F_GETFL, 0) | O_NONBLOCK) < 0)
DieWithError("[PacketServer::run] fcntl() failed");
#endif
if (setsockopt(newSock, SOL_SOCKET, SO_SNDBUF, (char *) &iopt, sizeof(iopt)) < 0 )
DieWithError("[PacketServer::run] setsockopt() failed");
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,"[PacketServer::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;
_debug->Log(vbit::Debug::LogLevels::logINFO,"[PacketServer::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, BUFFLEN, 0);
if (n == 0)
{
/* client disconnected */
getpeername(sock, (struct sockaddr*)&address, &addrlen);
_debug->Log(vbit::Debug::LogLevels::logINFO,"[PacketServer::run] closing connection from " + std::string(inet_ntoa(address.sin_addr)) + ":" + std::to_string(ntohs(address.sin_port)) + " on socket " + std::to_string(sock));
_mtx[i].lock();
_clientSocks[i] = -1; /* free slot */
#ifdef WIN32
closesocket(sock);
#else
close(sock);
#endif
_mtx[i].unlock();
}
else if (n > 0)
{
// don't care what client sent right now
}
else /* n < 0 */
{
#ifdef WIN32
int e = WSAGetLastError();
#else
int e = errno;
#endif
getpeername(sock, (struct sockaddr*)&address, &addrlen);
_debug->Log(vbit::Debug::LogLevels::logWARN,"[PacketServer::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 */
_mtx[i].lock();
_clientSocks[i] = -1; /* free slot */
#ifdef WIN32
closesocket(sock);
#else
close(sock);
#endif
_mtx[i].unlock();
}
}
}
}
}
}