-
Notifications
You must be signed in to change notification settings - Fork 15
/
socket.cpp
812 lines (638 loc) · 18.4 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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
/*
ent-ghost
Copyright [2011-2013] [Jack Lu]
This file is part of the ent-ghost source code.
ent-ghost 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.
ent-ghost source code 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 ent-ghost source code. If not, see <http://www.gnu.org/licenses/>.
ent-ghost is modified from GHost++ (http://ghostplusplus.googlecode.com/)
GHost++ is Copyright [2008] [Trevor Hogan]
*/
#include "ghost.h"
#include "util.h"
#include "socket.h"
#include <string.h>
#ifndef WIN32
int GetLastError( ) { return errno; }
#endif
//
// CSocket
//
CSocket :: CSocket( ) : m_Socket( INVALID_SOCKET ), m_HasError( false ), m_Error( 0 )
{
memset( &m_SIN, 0, sizeof( m_SIN ) );
}
CSocket :: CSocket( SOCKET nSocket, struct sockaddr_in nSIN ) : m_Socket( nSocket ), m_SIN( nSIN ), m_HasError( false ), m_Error( 0 )
{
}
CSocket :: ~CSocket( )
{
if( m_Socket != INVALID_SOCKET )
closesocket( m_Socket );
}
BYTEARRAY CSocket :: GetPort( )
{
return UTIL_CreateByteArray( m_SIN.sin_port, false );
}
BYTEARRAY CSocket :: GetIP( )
{
return UTIL_CreateByteArray( (uint32_t)m_SIN.sin_addr.s_addr, false );
}
string CSocket :: GetIPString( )
{
return inet_ntoa( m_SIN.sin_addr );
}
string CSocket :: GetErrorString( )
{
if( !m_HasError )
return "NO ERROR";
switch( m_Error )
{
case EWOULDBLOCK: return "EWOULDBLOCK";
case EINPROGRESS: return "EINPROGRESS";
case EALREADY: return "EALREADY";
case ENOTSOCK: return "ENOTSOCK";
case EDESTADDRREQ: return "EDESTADDRREQ";
case EMSGSIZE: return "EMSGSIZE";
case EPROTOTYPE: return "EPROTOTYPE";
case ENOPROTOOPT: return "ENOPROTOOPT";
case EPROTONOSUPPORT: return "EPROTONOSUPPORT";
case ESOCKTNOSUPPORT: return "ESOCKTNOSUPPORT";
case EOPNOTSUPP: return "EOPNOTSUPP";
case EPFNOSUPPORT: return "EPFNOSUPPORT";
case EAFNOSUPPORT: return "EAFNOSUPPORT";
case EADDRINUSE: return "EADDRINUSE";
case EADDRNOTAVAIL: return "EADDRNOTAVAIL";
case ENETDOWN: return "ENETDOWN";
case ENETUNREACH: return "ENETUNREACH";
case ENETRESET: return "ENETRESET";
case ECONNABORTED: return "ECONNABORTED";
case ECONNRESET: return "ECONNRESET";
case ENOBUFS: return "ENOBUFS";
case EISCONN: return "EISCONN";
case ENOTCONN: return "ENOTCONN";
case ESHUTDOWN: return "ESHUTDOWN";
case ETOOMANYREFS: return "ETOOMANYREFS";
case ETIMEDOUT: return "ETIMEDOUT";
case ECONNREFUSED: return "ECONNREFUSED";
case ELOOP: return "ELOOP";
case ENAMETOOLONG: return "ENAMETOOLONG";
case EHOSTDOWN: return "EHOSTDOWN";
case EHOSTUNREACH: return "EHOSTUNREACH";
case ENOTEMPTY: return "ENOTEMPTY";
case EUSERS: return "EUSERS";
case EDQUOT: return "EDQUOT";
case ESTALE: return "ESTALE";
case EREMOTE: return "EREMOTE";
}
return "UNKNOWN ERROR (" + UTIL_ToString( m_Error ) + ")";
}
void CSocket :: SetFD( fd_set *fd, fd_set *send_fd, int *nfds )
{
if( m_Socket == INVALID_SOCKET )
return;
FD_SET( m_Socket, fd );
FD_SET( m_Socket, send_fd );
#ifndef WIN32
if( m_Socket > *nfds )
*nfds = m_Socket;
#endif
}
void CSocket :: Allocate( int type )
{
m_Socket = socket( AF_INET, type, 0 );
if( m_Socket == INVALID_SOCKET )
{
m_HasError = true;
m_Error = GetLastError( );
CONSOLE_Print( "[SOCKET] error (socket) - " + GetErrorString( ) );
return;
}
}
void CSocket :: Reset( )
{
if( m_Socket != INVALID_SOCKET )
closesocket( m_Socket );
m_Socket = INVALID_SOCKET;
memset( &m_SIN, 0, sizeof( m_SIN ) );
m_HasError = false;
m_Error = 0;
}
string CSocket :: GetHostName( )
{
if( m_CachedHostName.empty( ) )
{
char host[NI_MAXHOST], service[NI_MAXSERV];
int s = getnameinfo( ( struct sockaddr * ) &m_SIN, sizeof( m_SIN ), host, NI_MAXHOST, service, NI_MAXSERV, NI_NUMERICSERV );
string str( host );
m_CachedHostName = str;
if( m_CachedHostName.empty( ) )
m_CachedHostName = "Unknown";
return str;
}
else
return m_CachedHostName;
}
//
// CTCPSocket
//
CTCPSocket :: CTCPSocket( ) : CSocket( ), m_Connected( false ), m_LastRecv( GetTime( ) ), m_LastSend( GetTime( ) )
{
Allocate( SOCK_STREAM );
// make socket non blocking
#ifdef WIN32
int iMode = 1;
ioctlsocket( m_Socket, FIONBIO, (u_long FAR *)&iMode );
#else
fcntl( m_Socket, F_SETFL, fcntl( m_Socket, F_GETFL ) | O_NONBLOCK );
#endif
}
CTCPSocket :: CTCPSocket( SOCKET nSocket, struct sockaddr_in nSIN ) : CSocket( nSocket, nSIN )
{
m_Connected = true;
m_LastRecv = GetTime( );
m_LastSend = GetTime( );
// make socket non blocking
#ifdef WIN32
int iMode = 1;
ioctlsocket( m_Socket, FIONBIO, (u_long FAR *)&iMode );
#else
fcntl( m_Socket, F_SETFL, fcntl( m_Socket, F_GETFL ) | O_NONBLOCK );
#endif
}
CTCPSocket :: ~CTCPSocket( )
{
}
void CTCPSocket :: Reset( )
{
CSocket :: Reset( );
Allocate( SOCK_STREAM );
m_Connected = false;
m_RecvBuffer.clear( );
m_SendBuffer.clear( );
m_LastRecv = GetTime( );
m_LastSend = GetTime( );
// make socket non blocking
#ifdef WIN32
int iMode = 1;
ioctlsocket( m_Socket, FIONBIO, (u_long FAR *)&iMode );
#else
fcntl( m_Socket, F_SETFL, fcntl( m_Socket, F_GETFL ) | O_NONBLOCK );
#endif
if( !m_LogFile.empty( ) )
{
ofstream Log;
Log.open( m_LogFile.c_str( ), ios :: app );
if( !Log.fail( ) )
{
Log << "----------RESET----------" << endl;
Log.close( );
}
}
}
void CTCPSocket :: PutBytes( string bytes )
{
m_SendBuffer += bytes;
}
void CTCPSocket :: PutBytes( BYTEARRAY bytes )
{
m_SendBuffer += string( bytes.begin( ), bytes.end( ) );
}
void CTCPSocket :: DoRecv( fd_set *fd )
{
if( m_Socket == INVALID_SOCKET || m_HasError || !m_Connected )
return;
if( FD_ISSET( m_Socket, fd ) )
{
// data is waiting, receive it
char buffer[1024];
int c = recv( m_Socket, buffer, 1024, 0 );
if( c > 0 )
{
// success! add the received data to the buffer
if( !m_LogFile.empty( ) )
{
ofstream Log;
Log.open( m_LogFile.c_str( ), ios :: app );
if( !Log.fail( ) )
{
Log << " RECEIVE <<< " << UTIL_ByteArrayToHexString( UTIL_CreateByteArray( (unsigned char *)buffer, c ) ) << endl;
Log.close( );
}
}
m_RecvBuffer += string( buffer, c );
m_LastRecv = GetTime( );
}
else if( c == SOCKET_ERROR && GetLastError( ) != EWOULDBLOCK )
{
// receive error
m_HasError = true;
m_Error = GetLastError( );
CONSOLE_Print( "[TCPSOCKET] error (recv) - " + GetErrorString( ) );
return;
}
else if( c == 0 )
{
// the other end closed the connection
CONSOLE_Print( "[TCPSOCKET] closed by remote host" );
m_Connected = false;
}
}
}
void CTCPSocket :: DoSend( fd_set *send_fd )
{
if( m_Socket == INVALID_SOCKET || m_HasError || !m_Connected || m_SendBuffer.empty( ) )
return;
if( FD_ISSET( m_Socket, send_fd ) )
{
// socket is ready, send it
int s = send( m_Socket, m_SendBuffer.c_str( ), (int)m_SendBuffer.size( ), MSG_NOSIGNAL );
if( s > 0 )
{
// success! only some of the data may have been sent, remove it from the buffer
if( !m_LogFile.empty( ) )
{
ofstream Log;
Log.open( m_LogFile.c_str( ), ios :: app );
if( !Log.fail( ) )
{
Log << "SEND >>> " << UTIL_ByteArrayToHexString( BYTEARRAY( m_SendBuffer.begin( ), m_SendBuffer.begin( ) + s ) ) << endl;
Log.close( );
}
}
m_SendBuffer = m_SendBuffer.substr( s );
m_LastSend = GetTime( );
}
else if( s == SOCKET_ERROR && GetLastError( ) != EWOULDBLOCK )
{
// send error
m_HasError = true;
m_Error = GetLastError( );
CONSOLE_Print( "[TCPSOCKET] error (send) - " + GetErrorString( ) );
return;
}
}
}
void CTCPSocket :: Disconnect( )
{
if( m_Socket != INVALID_SOCKET )
shutdown( m_Socket, SHUT_RDWR );
m_Connected = false;
}
void CTCPSocket :: SetNoDelay( bool noDelay )
{
int OptVal = 0;
if( noDelay )
OptVal = 1;
setsockopt( m_Socket, IPPROTO_TCP, TCP_NODELAY, (const char *)&OptVal, sizeof( int ) );
}
//
// CTCPClient
//
CTCPClient :: CTCPClient( ) : CTCPSocket( ), m_Connecting( false )
{
}
CTCPClient :: ~CTCPClient( )
{
}
void CTCPClient :: Reset( )
{
CTCPSocket :: Reset( );
m_Connecting = false;
}
void CTCPClient :: Disconnect( )
{
CTCPSocket :: Disconnect( );
m_Connecting = false;
}
void CTCPClient :: Connect( string localaddress, string address, uint16_t port )
{
if( m_Socket == INVALID_SOCKET || m_HasError || m_Connecting || m_Connected )
return;
if( !localaddress.empty( ) )
{
struct sockaddr_in LocalSIN;
memset( &LocalSIN, 0, sizeof( LocalSIN ) );
LocalSIN.sin_family = AF_INET;
if( ( LocalSIN.sin_addr.s_addr = inet_addr( localaddress.c_str( ) ) ) == INADDR_NONE )
LocalSIN.sin_addr.s_addr = INADDR_ANY;
LocalSIN.sin_port = htons( 0 );
if( bind( m_Socket, (struct sockaddr *)&LocalSIN, sizeof( LocalSIN ) ) == SOCKET_ERROR )
{
m_HasError = true;
m_Error = GetLastError( );
CONSOLE_Print( "[TCPCLIENT] error (bind) - " + GetErrorString( ) );
return;
}
}
// get IP address
struct hostent *HostInfo;
uint32_t HostAddress;
HostInfo = gethostbyname( address.c_str( ) );
if( !HostInfo )
{
m_HasError = true;
// m_Error = h_error;
CONSOLE_Print( "[TCPCLIENT] error (gethostbyname)" );
return;
}
memcpy( &HostAddress, HostInfo->h_addr, HostInfo->h_length );
// connect
m_SIN.sin_family = AF_INET;
m_SIN.sin_addr.s_addr = HostAddress;
m_SIN.sin_port = htons( port );
if( connect( m_Socket, (struct sockaddr *)&m_SIN, sizeof( m_SIN ) ) == SOCKET_ERROR )
{
if( GetLastError( ) != EINPROGRESS && GetLastError( ) != EWOULDBLOCK )
{
// connect error
m_HasError = true;
m_Error = GetLastError( );
CONSOLE_Print( "[TCPCLIENT] error (connect) - " + GetErrorString( ) );
return;
}
}
m_Connecting = true;
}
bool CTCPClient :: CheckConnect( )
{
if( m_Socket == INVALID_SOCKET || m_HasError || !m_Connecting )
return false;
fd_set fd;
FD_ZERO( &fd );
FD_SET( m_Socket, &fd );
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
// check if the socket is connected
#ifdef WIN32
if( select( 1, NULL, &fd, NULL, &tv ) == SOCKET_ERROR )
#else
if( select( m_Socket + 1, NULL, &fd, NULL, &tv ) == SOCKET_ERROR )
#endif
{
m_HasError = true;
m_Error = GetLastError( );
return false;
}
if( FD_ISSET( m_Socket, &fd ) )
{
m_Connecting = false;
m_Connected = true;
return true;
}
return false;
}
//
// CTCPServer
//
CTCPServer :: CTCPServer( ) : CTCPSocket( )
{
// set the socket to reuse the address in case it hasn't been released yet
int optval = 1;
#ifdef WIN32
setsockopt( m_Socket, SOL_SOCKET, SO_REUSEADDR, (const char *)&optval, sizeof( int ) );
#else
setsockopt( m_Socket, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval, sizeof( int ) );
#endif
}
CTCPServer :: ~CTCPServer( )
{
}
bool CTCPServer :: Listen( string address, uint16_t port )
{
if( m_Socket == INVALID_SOCKET || m_HasError )
return false;
m_SIN.sin_family = AF_INET;
if( !address.empty( ) )
{
if( ( m_SIN.sin_addr.s_addr = inet_addr( address.c_str( ) ) ) == INADDR_NONE )
m_SIN.sin_addr.s_addr = INADDR_ANY;
}
else
m_SIN.sin_addr.s_addr = INADDR_ANY;
m_SIN.sin_port = htons( port );
if( bind( m_Socket, (struct sockaddr *)&m_SIN, sizeof( m_SIN ) ) == SOCKET_ERROR )
{
m_HasError = true;
m_Error = GetLastError( );
CONSOLE_Print( "[TCPSERVER] error (bind) - " + GetErrorString( ) );
return false;
}
// listen, queue length 8
if( listen( m_Socket, 8 ) == SOCKET_ERROR )
{
m_HasError = true;
m_Error = GetLastError( );
CONSOLE_Print( "[TCPSERVER] error (listen) - " + GetErrorString( ) );
return false;
}
return true;
}
CTCPSocket *CTCPServer :: Accept( fd_set *fd )
{
if( m_Socket == INVALID_SOCKET || m_HasError )
return NULL;
if( FD_ISSET( m_Socket, fd ) )
{
// a connection is waiting, accept it
struct sockaddr_in Addr;
int AddrLen = sizeof( Addr );
SOCKET NewSocket;
#ifdef WIN32
if( ( NewSocket = accept( m_Socket, (struct sockaddr *)&Addr, &AddrLen ) ) == INVALID_SOCKET )
#else
if( ( NewSocket = accept( m_Socket, (struct sockaddr *)&Addr, (socklen_t *)&AddrLen ) ) == INVALID_SOCKET )
#endif
{
// accept error, ignore it
}
else
{
// success! return the new socket
return new CTCPSocket( NewSocket, Addr );
}
}
return NULL;
}
//
// CUDPSocket
//
CUDPSocket :: CUDPSocket( ) : CSocket( )
{
Allocate( SOCK_DGRAM );
// enable broadcast support
int OptVal = 1;
setsockopt( m_Socket, SOL_SOCKET, SO_BROADCAST, (const char *)&OptVal, sizeof( int ) );
// set default broadcast target
m_BroadcastTarget.s_addr = INADDR_BROADCAST;
}
CUDPSocket :: ~CUDPSocket( )
{
}
bool CUDPSocket :: SendTo( struct sockaddr_in sin, BYTEARRAY message )
{
if( m_Socket == INVALID_SOCKET || m_HasError )
return false;
string MessageString = string( message.begin( ), message.end( ) );
if( sendto( m_Socket, MessageString.c_str( ), MessageString.size( ), 0, (struct sockaddr *)&sin, sizeof( sin ) ) == -1 )
return false;
return true;
}
bool CUDPSocket :: SendTo( string address, uint16_t port, BYTEARRAY message )
{
if( m_Socket == INVALID_SOCKET || m_HasError )
return false;
// get IP address
struct hostent *HostInfo;
uint32_t HostAddress;
HostInfo = gethostbyname( address.c_str( ) );
if( !HostInfo )
{
m_HasError = true;
// m_Error = h_error;
CONSOLE_Print( "[UDPSOCKET] error (gethostbyname)" );
return false;
}
memcpy( &HostAddress, HostInfo->h_addr, HostInfo->h_length );
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = HostAddress;
sin.sin_port = htons( port );
return SendTo( sin, message );
}
bool CUDPSocket :: Broadcast( uint16_t port, BYTEARRAY message )
{
if( m_Socket == INVALID_SOCKET || m_HasError )
return false;
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = m_BroadcastTarget.s_addr;
sin.sin_port = htons( port );
string MessageString = string( message.begin( ), message.end( ) );
if( sendto( m_Socket, MessageString.c_str( ), MessageString.size( ), 0, (struct sockaddr *)&sin, sizeof( sin ) ) == -1 )
{
CONSOLE_Print( "[UDPSOCKET] failed to broadcast packet (port " + UTIL_ToString( port ) + ", size " + UTIL_ToString( MessageString.size( ) ) + " bytes)" );
return false;
}
return true;
}
void CUDPSocket :: SetBroadcastTarget( string subnet )
{
if( subnet.empty( ) )
{
CONSOLE_Print( "[UDPSOCKET] using default broadcast target" );
m_BroadcastTarget.s_addr = INADDR_BROADCAST;
}
else
{
// this function does not check whether the given subnet is a valid subnet the user is on
// convert string representation of ip/subnet to in_addr
CONSOLE_Print( "[UDPSOCKET] using broadcast target [" + subnet + "]" );
m_BroadcastTarget.s_addr = inet_addr( subnet.c_str( ) );
// if conversion fails, inet_addr( ) returns INADDR_NONE
if( m_BroadcastTarget.s_addr == INADDR_NONE )
{
CONSOLE_Print( "[UDPSOCKET] invalid broadcast target, using default broadcast target" );
m_BroadcastTarget.s_addr = INADDR_BROADCAST;
}
}
}
void CUDPSocket :: SetDontRoute( bool dontRoute )
{
int OptVal = 0;
if( dontRoute )
OptVal = 1;
// don't route packets; make them ignore routes set by routing table and send them to the interface
// belonging to the target address directly
setsockopt( m_Socket, SOL_SOCKET, SO_DONTROUTE, (const char *)&OptVal, sizeof( int ) );
}
//
// CUDPServer
//
CUDPServer :: CUDPServer( ) : CUDPSocket( )
{
// make socket non blocking
#ifdef WIN32
int iMode = 1;
ioctlsocket( m_Socket, FIONBIO, (u_long FAR *)&iMode );
#else
fcntl( m_Socket, F_SETFL, fcntl( m_Socket, F_GETFL ) | O_NONBLOCK );
#endif
// set the socket to reuse the address
// with UDP sockets this allows more than one program to listen on the same port
int optval = 1;
#ifdef WIN32
setsockopt( m_Socket, SOL_SOCKET, SO_REUSEADDR, (const char *)&optval, sizeof( int ) );
#else
setsockopt( m_Socket, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval, sizeof( int ) );
#endif
}
CUDPServer :: ~CUDPServer( )
{
}
bool CUDPServer :: Bind( struct sockaddr_in sin )
{
if( m_Socket == INVALID_SOCKET || m_HasError )
return false;
m_SIN = sin;
if( bind( m_Socket, (struct sockaddr *)&m_SIN, sizeof( m_SIN ) ) == SOCKET_ERROR )
{
m_HasError = true;
m_Error = GetLastError( );
CONSOLE_Print( "[UDPSERVER] error (bind) - " + GetErrorString( ) );
return false;
}
return true;
}
bool CUDPServer :: Bind( string address, uint16_t port )
{
if( m_Socket == INVALID_SOCKET || m_HasError )
return false;
struct sockaddr_in sin;
sin.sin_family = AF_INET;
if( !address.empty( ) )
{
if( ( sin.sin_addr.s_addr = inet_addr( address.c_str( ) ) ) == INADDR_NONE )
sin.sin_addr.s_addr = INADDR_ANY;
}
else
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons( port );
return Bind( sin );
}
void CUDPServer :: RecvFrom( fd_set *fd, struct sockaddr_in *sin, string *message )
{
if( m_Socket == INVALID_SOCKET || m_HasError || !sin || !message )
return;
int AddrLen = sizeof( *sin );
if( FD_ISSET( m_Socket, fd ) )
{
// data is waiting, receive it
char buffer[1024];
#ifdef WIN32
int c = recvfrom( m_Socket, buffer, 1024, 0, (struct sockaddr *)sin, &AddrLen );
#else
int c = recvfrom( m_Socket, buffer, 1024, 0, (struct sockaddr *)sin, (socklen_t *)&AddrLen );
#endif
if( c > 0 )
{
// success!
*message = string( buffer, c );
}
else if( c == SOCKET_ERROR && GetLastError( ) != EWOULDBLOCK )
{
// receive error
m_HasError = true;
m_Error = GetLastError( );
CONSOLE_Print( "[UDPSERVER] error (recvfrom) - " + GetErrorString( ) );
}
}
}