forked from UniversalRobots/Universal_Robots_Client_Library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcp_server.cpp
More file actions
469 lines (417 loc) · 12.6 KB
/
tcp_server.cpp
File metadata and controls
469 lines (417 loc) · 12.6 KB
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
// this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
// -- BEGIN LICENSE BLOCK ----------------------------------------------
// Copyright 2021 FZI Forschungszentrum Informatik
// Created on behalf of Universal Robots A/S
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// -- END LICENSE BLOCK ------------------------------------------------
//----------------------------------------------------------------------
/*!\file
*
* \author Felix Exner mauch@fzi.de
* \date 2021-03-13
*
*/
//----------------------------------------------------------------------
#include <ur_client_library/log.h>
#include <ur_client_library/comm/tcp_server.h>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include "ur_client_library/comm/socket_t.h"
#include <fcntl.h>
namespace urcl
{
namespace comm
{
TCPServer::TCPServer(const int port, const size_t max_num_tries, const std::chrono::milliseconds reconnection_time)
: port_(port), maxfd_(0), max_clients_allowed_(0)
{
#ifdef _WIN32
WSAData data;
::WSAStartup(MAKEWORD(1, 1), &data);
#endif // _WIN32
init();
bind(max_num_tries, reconnection_time);
startListen();
}
TCPServer::~TCPServer()
{
URCL_LOG_DEBUG("Destroying TCPServer object.");
shutdown();
}
void TCPServer::init()
{
listen_fd_ = socket(AF_INET, SOCK_STREAM, 0);
if (listen_fd_ == INVALID_SOCKET)
{
throw makeSocketError("Failed to create socket endpoint");
}
int flag = 1;
#ifndef _WIN32
ur_setsockopt(listen_fd_, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(int));
#endif
ur_setsockopt(listen_fd_, SOL_SOCKET, SO_KEEPALIVE, &flag, sizeof(int));
URCL_LOG_DEBUG("Created socket with FD %d", (int)listen_fd_);
FD_ZERO(&masterfds_);
FD_ZERO(&tempfds_);
}
void TCPServer::shutdown()
{
std::unique_lock<std::mutex> listen_lk(listen_fd_mutex_, std::try_to_lock);
if (listen_fd_ == INVALID_SOCKET)
{
URCL_LOG_INFO("Listen FD already closed by another thread. Nothing to do here.");
return;
}
if (!listen_lk.owns_lock())
{
URCL_LOG_WARN("Could not acquire lock for listen FD when shutting down. Is there another thread shutting the "
"server down already? Waiting for lock to be released.");
listen_lk.lock();
if (listen_fd_ == INVALID_SOCKET)
{
URCL_LOG_INFO("Listen FD already closed by another thread. Nothing to do here.");
return;
}
}
keep_running_ = false;
socket_t shutdown_socket = ::socket(AF_INET, SOCK_STREAM, 0);
if (shutdown_socket == INVALID_SOCKET)
{
throw makeSocketError("Unable to create shutdown socket.");
}
#ifdef _WIN32
unsigned long mode = 1;
::ioctlsocket(shutdown_socket, FIONBIO, &mode);
#else
int flags = ::fcntl(shutdown_socket, F_GETFL, 0);
if (flags >= 0)
{
::fcntl(shutdown_socket, F_SETFL, flags | O_NONBLOCK);
}
#endif
struct sockaddr_in address;
memset(&address, 0, sizeof(address));
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
address.sin_port = htons(static_cast<uint16_t>(port_));
::connect(shutdown_socket, reinterpret_cast<const sockaddr*>(&address), sizeof(address));
// After the event loop has finished the thread will be joinable.
if (worker_thread_.joinable())
{
worker_thread_.join();
URCL_LOG_DEBUG("Worker thread joined.");
}
std::lock_guard<std::mutex> lk(clients_mutex_);
for (const auto& client_fd : client_fds_)
{
ur_close(client_fd);
}
// This will effectively deactivate the disconnection handler.
client_fds_.clear();
ur_close(shutdown_socket);
ur_close(listen_fd_);
listen_fd_ = INVALID_SOCKET;
}
void TCPServer::bind(const size_t max_num_tries, const std::chrono::milliseconds reconnection_time)
{
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
// INADDR_ANY is a special constant that signalizes "ANY IFACE",
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(static_cast<uint16_t>(port_));
int err = -1;
size_t connection_counter = 0;
do
{
err = ::bind(listen_fd_, (struct sockaddr*)&server_addr, sizeof(server_addr));
if (err == -1)
{
auto error_code = getLastSocketErrorCode();
std::ostringstream ss;
ss << "Failed to bind socket for port " << port_ << " to address. Reason: " << error_code.message();
if (connection_counter++ < max_num_tries || max_num_tries == 0)
{
std::this_thread::sleep_for(reconnection_time);
ss << "Retrying in " << std::chrono::duration_cast<std::chrono::duration<float>>(reconnection_time).count()
<< " seconds";
URCL_LOG_WARN("%s", ss.str().c_str());
}
else
{
throw std::system_error(error_code, ss.str());
}
}
} while (err == -1 && (connection_counter <= max_num_tries || max_num_tries == 0));
URCL_LOG_DEBUG("Bound %d:%d to FD %d", server_addr.sin_addr.s_addr, port_, (int)listen_fd_);
FD_SET(listen_fd_, &masterfds_);
maxfd_ = listen_fd_;
}
void TCPServer::startListen()
{
int err = listen(listen_fd_, 1);
if (err == -1)
{
std::ostringstream ss;
ss << "Failed to start listen on port " << port_;
throw makeSocketError(ss.str());
}
struct sockaddr_in sin;
socklen_t len = sizeof(sin);
if (getsockname(listen_fd_, (struct sockaddr*)&sin, &len) == -1)
{
URCL_LOG_ERROR("getsockname() failed to get port number for listening socket: %s",
getLastSocketErrorCode().message().c_str());
}
else
{
port_ = ntohs(sin.sin_port);
}
URCL_LOG_DEBUG("Listening on port %d", port_);
}
void TCPServer::handleConnect()
{
struct sockaddr_storage client_addr;
socklen_t addrlen = sizeof(client_addr);
socket_t client_fd = accept(listen_fd_, (struct sockaddr*)&client_addr, &addrlen);
if (client_fd == INVALID_SOCKET)
{
URCL_LOG_ERROR("Failed to accept connection request on port %d. Reason: %s", port_,
getLastSocketErrorCode().message().c_str());
return;
}
#ifdef _WIN32
bool set_size_exceeded = client_fds_.size() >= FD_SETSIZE - 1; // -1 because listen_fd_ also occupies one
// slot in masterfds_
#else
bool set_size_exceeded = client_fd >= FD_SETSIZE; // On Unix-like systems, the client FD itself must be less than
// FD_SETSIZE, otherwise it cannot be added to the fd_set.
#endif
if (set_size_exceeded)
{
URCL_LOG_ERROR("Accepted client FD %d exceeds FD_SETSIZE (%d). Closing connection.", (int)client_fd, FD_SETSIZE);
ur_close(client_fd);
return;
}
bool accepted = false;
{
std::lock_guard<std::mutex> lk(clients_mutex_);
if (client_fds_.size() < max_clients_allowed_ || max_clients_allowed_ == 0)
{
client_fds_.push_back(client_fd);
FD_SET(client_fd, &masterfds_);
if (client_fd > maxfd_)
{
maxfd_ = client_fd;
}
accepted = true;
}
else
{
URCL_LOG_WARN("Connection attempt on port %d while maximum number of clients (%d) is already connected. Closing "
"connection.",
port_, max_clients_allowed_);
ur_close(client_fd);
}
}
{
std::lock_guard<std::mutex> lk(callback_mutex_);
if (new_connection_callback_ && accepted)
{
new_connection_callback_(client_fd);
}
}
}
void TCPServer::spin()
{
tempfds_ = masterfds_;
timeval timeout;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
// blocks until activity on any socket from tempfds
int sel = select(static_cast<int>(maxfd_ + 1), &tempfds_, NULL, NULL, &timeout);
if (sel < 0)
{
URCL_LOG_ERROR("select() failed. Shutting down socket event handler.");
keep_running_ = false;
return;
}
if (!keep_running_ || sel == 0)
{
return;
}
if (FD_ISSET(listen_fd_, &tempfds_))
{
URCL_LOG_DEBUG("Activity on listen FD %d", (int)listen_fd_);
handleConnect();
}
std::vector<socket_t> disconnected_clients;
std::vector<socket_t> client_fds_with_activity;
{
std::lock_guard<std::mutex> lk(clients_mutex_);
for (const auto& client_fd : client_fds_)
{
if (FD_ISSET(client_fd, &tempfds_))
{
URCL_LOG_DEBUG("Activity on client FD %d", (int)client_fd);
client_fds_with_activity.push_back(client_fd);
}
}
}
// We handle client activity outside the clients_mutex_ lock to avoid holding it during potentially slow I/O and
// message callbacks.
// The clients_mutex_ lock is only needed to protect the client_fds_ vector, but once we have copied the FDs with
// activity to a separate vector, we can safely handle them without holding the lock.
for (const auto& client_fd : client_fds_with_activity)
{
if (!readData(client_fd))
{
disconnected_clients.push_back(client_fd);
}
}
for (const auto& client_fd : disconnected_clients)
{
handleDisconnect(client_fd);
}
}
void TCPServer::handleDisconnect(const socket_t fd)
{
URCL_LOG_INFO("%d disconnected.", fd);
{
std::lock_guard<std::mutex> lk(clients_mutex_);
ur_close(fd);
FD_CLR(fd, &masterfds_);
for (size_t i = 0; i < client_fds_.size(); ++i)
{
if (client_fds_[i] == fd)
{
client_fds_.erase(client_fds_.begin() + i);
break;
}
}
maxfd_ = listen_fd_;
for (const auto& client_fd : client_fds_)
{
if (client_fd > maxfd_)
{
maxfd_ = client_fd;
}
}
}
{
std::lock_guard<std::mutex> lk(callback_mutex_);
if (disconnect_callback_ && keep_running_)
{
disconnect_callback_(fd);
}
}
}
bool TCPServer::readData(const socket_t fd)
{
memset(input_buffer_, 0, INPUT_BUFFER_SIZE); // clear input buffer
int nbytesrecv = recv(fd, input_buffer_, INPUT_BUFFER_SIZE, 0);
if (nbytesrecv > 0)
{
std::lock_guard<std::mutex> lk(message_mutex_);
if (message_callback_)
{
message_callback_(fd, input_buffer_, nbytesrecv);
}
}
else
{
if (nbytesrecv < 0)
{
auto check_err = []() {
#ifdef _WIN32
return WSAGetLastError() == WSAECONNRESET;
#else
return errno == ECONNRESET;
#endif
};
if (check_err()) // if connection gets reset by client, we want to suppress this output
{
URCL_LOG_DEBUG("client from FD %d sent a connection reset package.", fd);
}
else
{
URCL_LOG_ERROR("recv() on FD %d failed.", fd);
}
}
else
{
// normal disconnect
}
return false;
}
return true;
}
void TCPServer::worker()
{
while (keep_running_)
{
spin();
}
URCL_LOG_DEBUG("Finished worker thread of TCPServer");
}
void TCPServer::start()
{
URCL_LOG_DEBUG("Starting worker thread");
keep_running_ = true;
worker_thread_ = std::thread(&TCPServer::worker, this);
}
bool TCPServer::write(const socket_t fd, const uint8_t* buf, const size_t buf_len, size_t& written)
{
written = 0;
if (fd == INVALID_SOCKET)
{
URCL_LOG_ERROR("Invalid socket provided for writing.");
return false;
}
{
std::lock_guard<std::mutex> lk(clients_mutex_);
if (std::find(client_fds_.begin(), client_fds_.end(), fd) == client_fds_.end())
{
URCL_LOG_ERROR("Trying to write to FD %d, but this client is not connected.", fd);
return false;
}
}
// We don't use a lock around the send call here, since writing on a closed socket would raise
// an error anyway, and the client FD is only removed from client_fds_ after the socket is
// closed. Thus, even if the client gets disconnected right after the check, the send call will
// just fail and return false, which is the expected behavior.
return writeUnchecked(fd, buf, buf_len, written);
}
bool TCPServer::writeUnchecked(const socket_t fd, const uint8_t* buf, const size_t buf_len, size_t& written)
{
size_t remaining = buf_len;
// handle partial sends
while (written < buf_len)
{
ssize_t sent =
::send(fd, reinterpret_cast<const char*>(buf + written), static_cast<socklen_t>(remaining), MSG_NOSIGNAL);
if (sent <= 0)
{
URCL_LOG_ERROR("Sending data through socket failed.");
return false;
}
written += sent;
remaining -= sent;
}
return true;
}
} // namespace comm
} // namespace urcl