-
Notifications
You must be signed in to change notification settings - Fork 1
/
sockets_cpp.cpp
433 lines (354 loc) · 9.87 KB
/
sockets_cpp.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
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
/*
*
* Copyright (C) 2009 Daniel J. Calandria Hernández &
* Antonio Cañas Vargas
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "sockets_cpp.h"
#include <algorithm>
#include <errno.h>
#include <iostream>
SocketServerNonBlock::SocketServerNonBlock ()
: listener (-1), max_descriptor (0), static_tbl(), dynamic_tbl(), sessions()
{
}
SocketServerNonBlock::~SocketServerNonBlock ()
{
Close();
}
SocketServerNonBlock::SocketServerNonBlock ( int port, unsigned max_connections, unsigned max_queue, time_t _timeout)
: listener (-1), max_descriptor (0), static_tbl(), dynamic_tbl(), sessions()
{
Open ( port, max_connections, max_queue, timeout);
}
SocketServerNonBlock::SocketServerNonBlock ( const SocketServerNonBlock &src )
{
listener = src.listener;
max_descriptor = src.max_descriptor;
static_tbl = src.static_tbl;
dynamic_tbl = src.dynamic_tbl;
sessions = src.sessions;
sessions.reserve (src.sessions.capacity());
}
SocketServerNonBlock& SocketServerNonBlock::operator= ( const SocketServerNonBlock &src)
{
if (&src != this)
{
listener = src.listener;
max_descriptor = src.max_descriptor;
static_tbl = src.static_tbl;
dynamic_tbl = src.dynamic_tbl;
sessions = src.sessions;
sessions.reserve (src.sessions.capacity());
}
return *this;
}
int SocketServerNonBlock::Open ( int port, unsigned max_connections, unsigned max_queue, time_t timeout_ )
{
Close ();
sockaddr_in address;
listener = socket ( AF_INET, SOCK_STREAM, 0);
if (listener == -1)
return E_FAIL;
int optval = 1;
if ( setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) == -1)
return E_FAIL;
address.sin_family = AF_INET;
address.sin_port = htons (port);
address.sin_addr.s_addr = INADDR_ANY;
if ( bind (listener, reinterpret_cast<sockaddr*>(&address), sizeof (address)) == -1)
return E_FAIL;
if ( listen (listener, max_queue) == -1)
return E_FAIL;
sessions.reserve (max_connections);
FD_ZERO ( &static_tbl );
FD_ZERO ( &dynamic_tbl );
FD_SET ( listener, &static_tbl );
max_descriptor = listener;
timeout = timeout_;
return listener;
}
int SocketServerNonBlock::Close ( )
{
while (!sessions.empty())
{
SocketSessionNonBlock *session = dynamic_cast<SocketSessionNonBlock*> (sessions.back ());
if (session->socket != -1)
{
if (session->socket >= 0)
close ( session->socket );
session->socket = -1;
delete session;
sessions.pop_back();
}
}
if (listener >= 0)
close ( listener );
listener = -1;
FD_ZERO ( &static_tbl );
FD_ZERO ( &dynamic_tbl );
return S_OK;
}
int SocketServerNonBlock::SetMaxConnections ( unsigned max_connections )
{
sessions.reserve (max_connections );
return S_OK;
}
int SocketServerNonBlock::Update ( unsigned usec )
{
timeval tv;
int res = 0;
tv.tv_sec = usec / 1000000;
tv.tv_usec = usec % 1000000;
FD_ZERO ( &dynamic_tbl );
dynamic_tbl = static_tbl;
res = select(max_descriptor + 1, &dynamic_tbl, 0, 0, &tv);
//Timeouts
std::vector < SocketSessionNonBlock* >::iterator it;
time_t cur_time = time(0);
for (it = sessions.begin(); it != sessions.end(); ++it)
{
if (timeout && ((*it)->GetTimeout() < cur_time) )
{
close ( (*it)->socket );
(*it)->is_timeout = true;
}
}
//
if (res < 0)
return E_FAIL;
return res;
}
SocketSession* SocketServerNonBlock::WaitConnection ( )
{
SocketSessionNonBlock *session = new SocketSessionNonBlock(timeout);
if ( FD_ISSET ( listener, &dynamic_tbl ) )
{
session->socket = accept ( listener, 0, 0);
if (session->socket < 0)
{
delete session;
return 0;
}
//session->socket_stream = fdopen ( session->socket, "r+");
if (sessions.size() < sessions.capacity())
{
session->server = this;
session->idx = sessions.size();
sessions.push_back (session);
FD_SET ( session->socket, &static_tbl ) ;
max_descriptor = std::max<int>( max_descriptor, session->socket ) ;
return session;
}
//Demasiadas conexiones
session->Close ();
}
delete session;
return 0;
}
size_t SocketServerNonBlock::Broadcast ( const char *msg, size_t size )
{
return 0;
}
int SocketServerNonBlock::CloseSession ( SocketSessionNonBlock* session )
{
if (!session)
return E_FAIL;
//SocketSessionNonBlock *session = reinterpret_cast<SocketSessionNonBlock*> (_session);
if (session->socket != -1 )
{
FD_CLR ( session->socket, &static_tbl );
FD_ZERO ( &dynamic_tbl );
//close ( session->socket );
/* if ( session->socket_stream > 0 )
fclose ( session->socket_stream ); */
if (session->socket >= 0 && !session->is_timeout)
close ( session->socket );
session->socket = -1;
//session->socket_stream = 0;
std::swap ( sessions[ session->idx ], sessions.back() );
//reinterpret_cast<SocketSessionNonBlock*> (sessions[session->idx])->idx = session->idx;
sessions[session->idx]->idx = session->idx;
sessions.pop_back();
if (max_descriptor == session->socket ) //calcular el nuevo maximo
{
max_descriptor = -1;
for (unsigned i = 0; i < sessions.size(); ++i)
{
//session = reinterpret_cast<SocketSessionNonBlock*> ( sessions[i] );
//max_descriptor = std::max<int> (max_descriptor, session->socket );
max_descriptor = std::max<int> (max_descriptor, sessions[i]->socket );
}
}
}
return S_OK;
}
ssize_t SocketSessionNonBlock::Read ( char *buf, size_t len )
{
if (socket == -1 )
return E_RW_ERROR;
else if ( is_timeout )
return E_TIMEOUT; //se habra cerrado la conexion
if (FD_ISSET ( socket, &server->dynamic_tbl ) )
{
ssize_t res = recv ( socket, buf, len, 0 );
if (res < 0)
return E_RW_ERROR; //error
else if (res == 0)
return S_CONNECTION_CLOSED; //conexion cerrada
else
{
next_timeout = time(0) + timeout;
return res;
}
}
return 0;
}
ssize_t SocketSessionNonBlock::Write (const char *buf, size_t len )
{
if ( socket < 0 )
return E_RW_ERROR;
const char *ptr = buf;
const char *end = buf + len;
while ( ptr < end )
{
ssize_t res = send (socket, ptr, len, MSG_NOSIGNAL | MSG_DONTWAIT );
if ((res < 0) && (res != EAGAIN))
return E_RW_ERROR;
else if (res == EAGAIN)
return E_RW_RETRY;
else if (res > 0)
{
ptr += res;
len -= res;
}
}
return 0;
}
int SocketSessionNonBlock::Close ()
{
if ( server )
{
server->CloseSession (this);
}
else //La sesion no se introdujo en la lista del servidor
{
if (socket>= 0)
close(socket);
socket = -1;
}
return S_OK;
}
SocketSessionNonBlock::SocketSessionNonBlock ( time_t timeout_)
: socket(-1), idx(-1), server(0), is_timeout(false)
{
timeout = timeout_;
next_timeout = time(0) + timeout;
}
SocketSessionNonBlock::~SocketSessionNonBlock ()
{
Close();
}
int SocketClient::Connect ( const char *server_name, int port )
{
sockaddr_in serv_addr;
hostent* host = gethostbyname (server_name);
if (host == NULL)
return E_FAIL;
sckt = socket (AF_INET, SOCK_STREAM, 0);
if (sckt == -1)
return E_FAIL;
memset ( &serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
serv_addr.sin_addr = *((struct in_addr *)host->h_addr);
if (connect(sckt, (sockaddr*) &serv_addr, sizeof(serv_addr) ) < 0)
return E_FAIL;
FD_ZERO(&select_flag);
FD_SET(sckt, &select_flag);
return S_OK;
}
int SocketClient::Close ()
{
if (sckt >= 0)
{
FD_CLR ( sckt, &select_flag);
close (sckt);
sckt = -1;
}
return S_OK;
}
ssize_t SocketClient::Read ( char *buf, size_t len, unsigned usec )
{
if (sckt == -1 )
return E_RW_ERROR;
timeval tv;
tv.tv_sec = usec / 1000000;
tv.tv_usec = usec % 1000000;
FD_SET(sckt, &select_flag);
if ( select(sckt + 1, &select_flag, 0, 0, &tv) < 0 )
return 0;
ssize_t res = 0;
if (FD_ISSET ( sckt, &select_flag ) )
{
res = recv(sckt, buf, len,0);
if (res < 0)
return E_RW_ERROR;
if (res == 0)
return S_CONNECTION_CLOSED;
}
return res;
}
ssize_t SocketClient::Write ( const char *buf, size_t len )
{
if ( sckt < 0 )
return E_RW_ERROR;
const char *ptr = buf;
const char *end = buf + len;
while ( ptr < end )
{
ssize_t res = send (sckt, ptr, len, MSG_NOSIGNAL | MSG_DONTWAIT );
if ((res < 0) && (res != EAGAIN))
return E_RW_ERROR;
else if (res == EAGAIN)
return E_RW_RETRY;
else if (res > 0)
{
ptr += res;
len -= res;
}
}
return 0;
}
SocketClient::SocketClient () : sckt(-1) {}
SocketClient::~SocketClient ()
{
Close();
}
//ssize_t SocketSessionNonBlock::printf ( const char *fmt, ... )
//{
// va_list list;
//va_start( list, fmt );
//ssize_t res = vfprintf ( socket_stream, fmt, list );
//va_end( list );
//fsync ( socket);
//return res;
//}
//ssize_t SocketSessionNonBlock::vprintf ( const char *fmt, va_list arg )
//{
//ssize_t res = vfprintf ( socket_stream, fmt, arg );
//fsync ( socket );
// return res;
//}