-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSocket.cpp
618 lines (513 loc) · 10.3 KB
/
Socket.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
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
#include "Socket.h"
#include "SocketLinux.h"
#include "SocketWin.h"
EASY_NS_BEGIN
#define MULTICAST_ADDR4 "234.5.6.7"
#define MULTICAST_ADDR6 "FF02::99"
SocketError::SocketError(int code, int inter /*= 0*/, const char *msg /*= 0*/, SocketBase* s)
: code(code), internalCode(inter), msg(msg), s(s)
{
formatError();
}
void SocketError::formatError()
{
if (code != 0) {
if (s) {
msg = s->formatError(code);
} else {
msg = "formatError failed: SocketBase invalid.";
}
} else {
switch (internalCode)
{
case InternalNone:
msg = "no internal error.";
break;
case SocketInvalid:
msg = "socket invalid.";
break;
default:
msg = "unknown internal error.";
break;
}
}
}
UdpData::UdpData(Buffer *b, SockAddr *a)
{
buffer = b;
addr = a;
buffer->retain();
addr->retain();
}
UdpData::~UdpData()
{
buffer->release();
addr->release();
}
const char* Socket::getMulticastAddr(int protocol)
{
return protocol == 4 ? MULTICAST_ADDR4 : MULTICAST_ADDR6;
}
Socket::Socket()
: state(0), maxConnections(10), connectTimeoutSecs(5),
addrInfo(0), checkIpv6Only(true), socket_type(SOCK_STREAM), addrUdp(0),
ioTimeoutSecs(-1), addrTemp(0)
{
#if EASY_LINUX
impl = new SocketLinux();
#else
impl = new SocketWin();
#endif
impl->setDelegate(this);
setState(sClose);
}
Socket::Socket(SocketBase *p)
: state(0), maxConnections(10), connectTimeoutSecs(-1),
addrInfo(0), checkIpv6Only(true), socket_type(SOCK_STREAM), addrUdp(0), impl(p),
ioTimeoutSecs(-1), addrTemp(0)
{
impl->setDelegate(this);
}
Socket::~Socket()
{
if (impl) {
delete impl;
impl = 0;
}
if (state) {
delete state;
state = 0;
}
if (addrInfo) {
freeaddrinfo(addrInfo);
addrInfo = 0;
}
if (addrUdp) {
addrUdp->release();
addrUdp = 0;
}
if (addrTemp) {
addrTemp->release();
addrTemp = 0;
}
SocketVec::iterator itr = connections.begin();
for (; itr != connections.end(); ++itr) {
Socket *p = *itr;
if (p) {
p->release();
}
}
connections.clear();
EventMap::iterator it = events.begin();
for (; it != events.end(); ++it) {
EventFuncVec *p = it->second;
if (p) {
delete p;
}
}
events.clear();
}
bool Socket::connect(int port, const char *ip)
{
int protocol = checkProtocol(port, ip);
if (isClosed() || protocol != impl->getProtocol()) {
if (!create(protocol)) {
EASY_LOG("Socket::connect error: create socket failed.");
return false;
}
}
if (state && state->getType() == sConnecting) {
EASY_LOG("Socket::connect warning: connecting.");
return true;
}
if (impl) {
setState(sConnecting);
if (addrInfo) {
return impl->connect(addrInfo);
} else {
return impl->connect(port, ip);
}
}
return false;
}
bool Socket::listen(int port, const char *ip)
{
int protocol = checkProtocol(port, ip, true);
if (isClosed() || protocol != impl->getProtocol()) {
if (!create(protocol)) {
EASY_LOG("Socket::listen error: create socket failed.");
return false;
}
}
if (impl) {
// Note: if ip zero, means any ip
if (addrInfo && ip) {
if (!impl->bind(addrInfo)) {
return false;
}
} else {
if (!impl->bind(port, ip)) {
return false;
}
}
// Note: udp not listen
if (isUdp()) {
onListening();
return true;
}
if (impl->listen()) {
return true;
}
}
return false;
}
bool Socket::send(const char *buf, int len)
{
if (!isConnected()) {
EASY_LOG("Socket::send error: not connected yet.");
return false;
}
if (len == 0) {
len = strlen(buf) + 1;
}
return impl ? impl->send(buf, len) : false;
}
void Socket::close()
{
if (impl) {
impl->close();
}
}
void Socket::shutdown()
{
if (impl) {
impl->shutdown();
impl->close();
}
}
bool Socket::create(int protocol /*= -1*/)
{
close();
if (impl->create(protocol)) {
setState(sDisconnected);
return true;
} else {
return false;
}
}
bool Socket::send(const char *buf, int len, int port, const char *ip)
{
if (!isUdp()) {
EASY_LOG("Socket::sendto error: you must call setSocketType(SOCK_DGRAM) first.");
return false;
}
int protocol = checkProtocol(port, ip);
if (isClosed() || protocol != impl->getProtocol()) {
if (!create(protocol)) {
EASY_LOG("Socket::sendto error: create socket failed.");
return false;
}
}
if (len == 0) {
len = strlen(buf) + 1;
}
return impl ? impl->sendto(buf, len, port, ip) : false;
}
bool Socket::addMembership(const char *addr, const char *interface_ /*= 0*/)
{
if (!isUdp()) {
EASY_LOG("Socket::addMembership error: you must call setSocketType(SOCK_DGRAM) first.");
return false;
}
int protocol = checkProtocol(0, 0);
if (isClosed() || protocol != impl->getProtocol()) {
if (!create(protocol)) {
EASY_LOG("Socket::addMembership error: create socket failed.");
return false;
}
}
if (!addr) {
addr = protocol == 4 ? MULTICAST_ADDR4 : MULTICAST_ADDR6;
}
return impl->addMembership(addr, interface_);
}
bool Socket::dropMembership(const char *addr, const char *interface_ /*= 0*/)
{
if (!isUdp()) {
EASY_LOG("Socket::dropMembership error: you must call setSocketType(SOCK_DGRAM) first.");
return false;
}
int protocol = checkProtocol(0, 0);
if (isClosed() || protocol != impl->getProtocol()) {
if (!create(protocol)) {
EASY_LOG("Socket::dropMembership error: create socket failed.");
return false;
}
}
if (!addr) {
addr = protocol == 4 ? MULTICAST_ADDR4 : MULTICAST_ADDR6;
}
return impl->dropMembership(addr, interface_);
}
void Socket::closeConnection(int i)
{
if (i >= 0 && i < connections.size()) {
Socket *p = connections[i];
if (p) {
p->close();
p->release();
connections.erase(connections.begin() + i);
}
}
}
void Socket::closeAllConnections()
{
SocketVec::iterator itr = connections.begin();
for (; itr != connections.end(); ++itr) {
Socket *p = (*itr);
if (p) {
p->close();
p->release();
}
}
connections.clear();
}
void Socket::update()
{
if (state) {
state->update();
}
// update connections
SocketVec::iterator itr = connections.begin();
for (; itr != connections.end(); ) {
(*itr)->update();
// remove closed
if ((*itr)->isClosed()) {
(*itr)->release();
itr = connections.erase(itr);
EASY_LOG("total connections: %d", connections.size());
} else {
++itr;
}
}
}
bool Socket::isConnected()
{
return state && state->getType() == sConnected;
}
bool Socket::isClosed()
{
return state && state->getType() == sClose;
}
int Socket::getState()
{
return state ? state->getType() : -1;
}
void Socket::onError(int error, int internalError)
{
SocketError se(error, internalError, "", impl);
emit(sError, &se);
}
void Socket::onClose(bool hasError /*= false*/)
{
setState(sClose);
emit(sClose, (void*)hasError);
}
void Socket::onListening()
{
setState(sListening);
emit(sListening);
}
void Socket::onConnection(SocketBase *s)
{
Socket *ps = new Socket(s);
ps->setState(sConnected);
connections.push_back(ps);
emit(sConnection, ps);
}
void Socket::onConnect()
{
setState(sConnected);
emit(sConnected);
}
void Socket::onData()
{
emit(sData);
}
int Socket::select(int ms)
{
return impl->select(ms);
}
bool Socket::canRead()
{
return impl ? impl->canRead() : false;
}
bool Socket::canWrite()
{
return impl ? impl->canWrite() : false;
}
void Socket::on(int name, EventFunc cb)
{
EventMap::iterator itr = events.find(name);
EventFuncVec *p = 0;
if (itr == events.end()) {
p = new EventFuncVec();
events[name] = p;
} else {
p = itr->second;
}
if (p) {
p->push_back(cb);
}
}
void Socket::setSocketType(int type)
{
socket_type = type;
if (impl) {
impl->setSocketType(type);
}
}
Socket* Socket::getConnection(int i)
{
if (i >= 0 && i < connections.size()) {
return connections[i];
} else {
return 0;
}
}
const char* Socket::getIp()
{
return impl ? impl->getSockAddr(0, 0)->getIp() : 0;
}
int Socket::getPort()
{
return impl ? impl->getSockAddr(0, 0)->getPort() : -1;
}
void Socket::setState(StateType type, void *param)
{
if (state && state->getType() == type) {
return;
}
if (state) {
delete state;
state = 0;
}
state = SocketState::create(this, type, param);
}
void Socket::recv()
{
if (impl) {
char p[65535];
int ret = impl->recv(p, sizeof(p));
if (ret > 0) {
Buffer* buffer = new Buffer(ret);
buffer->write(p, 0, ret);
emit(sData, buffer);
buffer->release();
} else if (ret == 0) {
impl->close();
} else if (ret == -1) {
emitError();
impl->close(true);
}
}
}
bool Socket::accept()
{
bool ret = true;
if (impl) {
if (!addrTemp) {
addrTemp = new SockAddr(impl->getProtocol());
}
ret = impl->accept(addrTemp);
if (addrTemp->ref() > 1) {
addrTemp->release();
addrTemp = 0;
}
}
return ret;
}
bool Socket::checkConnected()
{
return impl ? impl->checkConnected() : false;
}
void Socket::emitError()
{
if (impl) {
impl->emitError();
}
}
void Socket::recvfrom()
{
if (impl) {
char p[65535];
SockAddr *addr = getUdpAddr();
int ret = impl->recvfrom(p, sizeof(p), &addr);
if (ret > 0) {
Buffer *buffer = new Buffer(ret);
buffer->write(p, 0, ret);
UdpData *data = new UdpData(buffer, addr);
emit(sMessage, data);
data->release();
buffer->release();
} else if (ret == 0) {
impl->close();
} else if (ret == -1) {
emitError();
impl->close(true);
}
}
}
SockAddr* Socket::getUdpAddr()
{
if (!addrUdp) {
addrUdp = new SockAddr(impl->getProtocol());
}
return addrUdp;
}
void Socket::emit(int name, void *p)
{
EventMap::iterator itr = events.find(name);
if (itr != events.end()) {
EventFuncVec *arr = itr->second;
EventFuncVec::iterator it = arr->begin();
for (; it != arr->end(); ++it) {
(*it)(p);
}
}
}
int Socket::checkProtocol(int port, const char *ip, bool passive)
{
int protocol = 4;
if (!checkIpv6Only) {
return impl ? impl->getProtocol() : protocol;
}
struct addrinfo hints, *res;
char port_s[30];
sprintf(port_s, "%d", port);
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = socket_type;
if (ip == 0 || passive) {
if (!ip) {
ip = "127.0.0.1";
}
hints.ai_flags = AI_PASSIVE;
}
if (addrInfo) {
freeaddrinfo(addrInfo);
addrInfo = 0;
}
int ret = getaddrinfo(ip, port_s, &hints, &res);
if (ret == 0) {
addrInfo = res;
if (addrInfo->ai_family == AF_INET6) {
EASY_LOG("Socket::checkProtocol: using ipv6!");
protocol = 6;
}
} else {
EASY_LOG("Socket::checkProtocol error: getaddrinfo failed.");
}
return protocol;
}
EASY_NS_END