-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtcp_server.cpp
More file actions
295 lines (252 loc) · 9.2 KB
/
Copy pathtcp_server.cpp
File metadata and controls
295 lines (252 loc) · 9.2 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
#include <functional>
#include <thread>
#include <algorithm>
#include "../include/tcp_server.h"
#include "../include/common.h"
TcpServer::TcpServer() {
_subscribers.reserve(10);
_clients.reserve(10);
_stopRemoveClientsTask = false;
}
TcpServer::~TcpServer() {
close();
}
void TcpServer::subscribe(const server_observer_t & observer) {
std::lock_guard<std::mutex> lock(_subscribersMtx);
_subscribers.push_back(observer);
}
void TcpServer::printClients() {
std::lock_guard<std::mutex> lock(_clientsMtx);
if (_clients.empty()) {
std::cout << "no connected clients\n";
}
for (const Client *client : _clients) {
client->print();
}
}
/**
* Remove dead clients (disconnected) from clients vector periodically
*/
void TcpServer::removeDeadClients() {
std::vector<Client*>::const_iterator clientToRemove;
while (!_stopRemoveClientsTask) {
{
std::lock_guard<std::mutex> lock(_clientsMtx);
do {
clientToRemove = std::find_if(_clients.begin(), _clients.end(),
[](Client *client) { return !client->isConnected(); });
if (clientToRemove != _clients.end()) {
(*clientToRemove)->close();
delete *clientToRemove;
_clients.erase(clientToRemove);
}
} while (clientToRemove != _clients.end());
}
sleep(2);
}
}
void TcpServer::terminateDeadClientsRemover() {
if (_clientsRemoverThread) {
_stopRemoveClientsTask = true;
_clientsRemoverThread->join();
delete _clientsRemoverThread;
_clientsRemoverThread = nullptr;
}
}
/**
* Handle different client events. Subscriber callbacks should be short and fast, and must not
* call other server functions to avoid deadlock
*/
void TcpServer::clientEventHandler(const Client &client, ClientEvent event, const std::string &msg) {
switch (event) {
case ClientEvent::DISCONNECTED: {
publishClientDisconnected(client.getIp(), msg);
break;
}
case ClientEvent::INCOMING_MSG: {
publishClientMsg(client, msg.c_str(), msg.size());
break;
}
}
}
/*
* Publish incomingPacketHandler client message to observer.
* Observers get only messages that originated
* from clients with IP address identical to
* the specific observer requested IP
*/
void TcpServer::publishClientMsg(const Client & client, const char * msg, size_t msgSize) {
std::lock_guard<std::mutex> lock(_subscribersMtx);
for (const server_observer_t& subscriber : _subscribers) {
if (subscriber.wantedIP == client.getIp() || subscriber.wantedIP.empty()) {
if (subscriber.incomingPacketHandler) {
subscriber.incomingPacketHandler(client.getIp(), msg, msgSize);
}
}
}
}
/*
* Publish client disconnection to observer.
* Observers get only notify about clients
* with IP address identical to the specific
* observer requested IP
*/
void TcpServer::publishClientDisconnected(const std::string &clientIP, const std::string &clientMsg) {
std::lock_guard<std::mutex> lock(_subscribersMtx);
for (const server_observer_t& subscriber : _subscribers) {
if (subscriber.wantedIP == clientIP) {
if (subscriber.disconnectionHandler) {
subscriber.disconnectionHandler(clientIP, clientMsg);
}
}
}
}
/*
* Bind port and start listening
* Return tcp_ret_t
*/
pipe_ret_t TcpServer::start(int port, int maxNumOfClients, bool removeDeadClientsAutomatically) {
if (removeDeadClientsAutomatically) {
_clientsRemoverThread = new std::thread(&TcpServer::removeDeadClients, this);
}
try {
initializeSocket();
bindAddress(port);
listenToClients(maxNumOfClients);
} catch (const std::runtime_error &error) {
return pipe_ret_t::failure(error.what());
}
return pipe_ret_t::success();
}
void TcpServer::initializeSocket() {
_sockfd.set(socket(AF_INET, SOCK_STREAM, 0));
const bool socketFailed = (_sockfd.get() == -1);
if (socketFailed) {
throw std::runtime_error(strerror(errno));
}
// set socket for reuse (otherwise might have to wait 4 minutes every time socket is closed)
const int option = 1;
setsockopt(_sockfd.get(), SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
}
void TcpServer::bindAddress(int port) {
memset(&_serverAddress, 0, sizeof(_serverAddress));
_serverAddress.sin_family = AF_INET;
_serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
_serverAddress.sin_port = htons(port);
const int bindResult = bind(_sockfd.get(), (struct sockaddr *)&_serverAddress, sizeof(_serverAddress));
const bool bindFailed = (bindResult == -1);
if (bindFailed) {
throw std::runtime_error(strerror(errno));
}
}
void TcpServer::listenToClients(int maxNumOfClients) {
const int clientsQueueSize = maxNumOfClients;
const bool listenFailed = (listen(_sockfd.get(), clientsQueueSize) == -1);
if (listenFailed) {
throw std::runtime_error(strerror(errno));
}
}
/*
* Accept and handle new client socket. To handle multiple clients, user must
* call this function in a loop to enable the acceptance of more than one.
* If timeout argument equal 0, this function is executed in blocking mode.
* If timeout argument is > 0 then this function is executed in non-blocking
* mode (async) and will quit after timeout seconds if no client tried to connect.
* Return accepted client IP, or throw error if failed
*/
std::string TcpServer::acceptClient(uint timeout) {
const pipe_ret_t waitingForClient = waitForClient(timeout);
if (!waitingForClient.isSuccessful()) {
throw std::runtime_error(waitingForClient.message());
}
socklen_t socketSize = sizeof(_clientAddress);
const int fileDescriptor = accept(_sockfd.get(), (struct sockaddr*)&_clientAddress, &socketSize);
const bool acceptFailed = (fileDescriptor == -1);
if (acceptFailed) {
throw std::runtime_error(strerror(errno));
}
auto newClient = new Client(fileDescriptor);
newClient->setIp(inet_ntoa(_clientAddress.sin_addr));
using namespace std::placeholders;
newClient->setEventsHandler(std::bind(&TcpServer::clientEventHandler, this, _1, _2, _3));
newClient->startListen();
std::lock_guard<std::mutex> lock(_clientsMtx);
_clients.push_back(newClient);
return newClient->getIp();
}
pipe_ret_t TcpServer::waitForClient(uint32_t timeout) {
if (timeout > 0) {
const fd_wait::Result waitResult = fd_wait::waitFor(_sockfd, timeout);
const bool noIncomingClient = (!FD_ISSET(_sockfd.get(), &_fds));
if (waitResult == fd_wait::Result::FAILURE) {
return pipe_ret_t::failure(strerror(errno));
} else if (waitResult == fd_wait::Result::TIMEOUT) {
return pipe_ret_t::failure("Timeout waiting for client");
} else if (noIncomingClient) {
return pipe_ret_t::failure("File descriptor is not set");
}
}
return pipe_ret_t::success();
}
/*
* Send message to all connected clients.
* Return true if message was sent successfully to all clients
*/
pipe_ret_t TcpServer::sendToAllClients(const char * msg, size_t size) {
std::lock_guard<std::mutex> lock(_clientsMtx);
for (const Client *client : _clients) {
pipe_ret_t sendingResult = sendToClient(*client, msg, size);
if (!sendingResult.isSuccessful()) {
return sendingResult;
}
}
return pipe_ret_t::success();
}
/*
* Send message to specific client (determined by client IP address).
* Return true if message was sent successfully
*/
pipe_ret_t TcpServer::sendToClient(const Client & client, const char * msg, size_t size){
try{
client.send(msg, size);
} catch (const std::runtime_error &error) {
return pipe_ret_t::failure(error.what());
}
return pipe_ret_t::success();
}
pipe_ret_t TcpServer::sendToClient(const std::string & clientIP, const char * msg, size_t size) {
std::lock_guard<std::mutex> lock(_clientsMtx);
const auto clientIter = std::find_if(_clients.begin(), _clients.end(),
[&clientIP](Client *client) { return client->getIp() == clientIP; });
if (clientIter == _clients.end()) {
return pipe_ret_t::failure("client not found");
}
const Client &client = *(*clientIter);
return sendToClient(client, msg, size);
}
/*
* Close server and clients resources.
* Return true is successFlag, false otherwise
*/
pipe_ret_t TcpServer::close() {
terminateDeadClientsRemover();
{ // close clients
std::lock_guard<std::mutex> lock(_clientsMtx);
for (Client * client : _clients) {
try {
client->close();
} catch (const std::runtime_error& error) {
return pipe_ret_t::failure(error.what());
}
}
_clients.clear();
}
{ // close server
const int closeServerResult = ::close(_sockfd.get());
const bool closeServerFailed = (closeServerResult == -1);
if (closeServerFailed) {
return pipe_ret_t::failure(strerror(errno));
}
}
return pipe_ret_t::success();
}