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
|
/*
* Copyright (C) 2007-2019 Xagasoft, All rights reserved.
*
* This file is part of the libbu++ library and is released under the
* terms of the license contained in the file LICENSE.
*/
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <errno.h>
#include <fcntl.h>
#include "bu/sockettcp.h"
#include "bu/config.h"
#ifndef WIN32
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#else
#include <Winsock2.h>
#endif
#define RBS (1024*2)
namespace Bu { subExceptionDef( SocketTcpException ) }
Bu::SocketTcp::SocketTcp( handle nSocketTcp ) :
nSocketTcp( nSocketTcp ),
bActive( true ),
bBlocking( true )
{
#ifdef WIN32
Bu::Winsock2::getInstance();
#endif
setAddress();
}
Bu::SocketTcp::SocketTcp( const Bu::String &sAddr, int nPort, int nTimeout,
bool bBlocking ) :
nSocketTcp( 0 ),
bActive( false ),
bBlocking( true )
{
#ifdef WIN32
Bu::Winsock2::getInstance();
#endif
/* Create the socket. */
nSocketTcp = bu_socket( PF_INET, SOCK_STREAM, 0 );
#ifdef WIN32
if( nSocketTcp == INVALID_SOCKET )
#else
if( nSocketTcp < 0 )
#endif
{
throw ExceptionBase("Couldn't create socket.\n");
}
setBlocking( false );
/* Connect to the server. */
//printf("Resolving hostname (%s)...\n", sAddr );
{
struct addrinfo *pAddr = NULL;
struct addrinfo aiHints;
memset( &aiHints, 0, sizeof(addrinfo) );
aiHints.ai_flags = AI_CANONNAME;
aiHints.ai_family = AF_INET;
aiHints.ai_socktype = SOCK_STREAM;
char ibuf[10];
sprintf( ibuf, "%d", nPort );
int ret;
if( (ret = bu_getaddrinfo(
sAddr.getStr(), ibuf, &aiHints, &pAddr )) != 0 )
{
close();
throw Bu::SocketTcpException("Couldn't resolve hostname %s (%s).\n",
sAddr.getStr(), bu_gai_strerror(ret));
}
bu_connect(
nSocketTcp,
pAddr->ai_addr,
pAddr->ai_addrlen
);
sAddress = pAddr->ai_canonname;
bu_freeaddrinfo( pAddr );
}
bActive = true;
if( nTimeout > 0 )
{
fd_set rfds, wfds, efds;
int retval;
FD_ZERO(&rfds);
FD_SET(nSocketTcp, &rfds);
FD_ZERO(&wfds);
FD_SET(nSocketTcp, &wfds);
FD_ZERO(&efds);
FD_SET(nSocketTcp, &efds);
struct timeval tv;
tv.tv_sec = nTimeout;
tv.tv_usec = 0;
retval = bu_select( nSocketTcp+1, &rfds, &wfds, &efds, &tv );
if( retval == 0 )
{
close();
throw ExceptionBase("Connection timeout.\n");
}
read( NULL, 0 ); // See if we can get any errors out of the way early.
}
if( bBlocking )
setBlocking( bBlocking );
}
Bu::SocketTcp::~SocketTcp()
{
close();
}
void Bu::SocketTcp::close()
{
if( bActive )
{
#ifndef WIN32
fsync( nSocketTcp );
#endif
#ifdef WIN32
#ifndef SHUT_RDWR
#define SHUT_RDWR (SD_BOTH)
#endif
#endif
bu_shutdown( nSocketTcp, SHUT_RDWR );
::close( nSocketTcp );
}
bActive = false;
}
Bu::size Bu::SocketTcp::read( void *pBuf, Bu::size nBytes )
{
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(nSocketTcp, &rfds);
struct timeval tv = {0, 0};
if( bu_select( nSocketTcp+1, &rfds, NULL, NULL, &tv ) < 0 )
{
int iErr = errno;
close();
throw SocketTcpException( SocketTcpException::cRead, strerror(iErr) );
}
if( FD_ISSET( nSocketTcp, &rfds ) || bBlocking )
{
int nRead = TEMP_FAILURE_RETRY(
bu_recv( nSocketTcp, (char *) pBuf, nBytes, 0 ) );
if( nRead == 0 && nBytes > 0 )
{
close();
throw SocketTcpException( SocketTcpException::cClosed, "SocketTcp closed.");
}
if( nRead < 0 )
{
#ifdef WIN32
int iWSAError = bu_WSAGetLastError();
if( iWSAError == WSAEWOULDBLOCK )
return 0;
#else
if( errno == ENETRESET || errno == ECONNRESET )
{
close();
throw SocketTcpException( SocketTcpException::cClosed,
strerror(errno) );
}
if( errno == EAGAIN )
return 0;
int iErr = errno;
close();
throw SocketTcpException( SocketTcpException::cRead, strerror(iErr) );
#endif
}
return nRead;
}
return 0;
}
Bu::size Bu::SocketTcp::read( void *pBuf, Bu::size nBytes,
uint32_t nSec, uint32_t nUSec )
{
struct timeval tv;
Bu::size nRead = 0;
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(nSocketTcp, &rfds);
#ifdef WIN32
DWORD dwStart = GetTickCount();
uint64_t uOver = dwStart + ((nUSec / 1000) * (nSec * 1000));
DWORD dwEnd = uOver>4294967295U?uOver-4294967295U:uOver;
#else
struct timeval nt, ct;
gettimeofday( &nt, NULL );
nt.tv_sec += nSec;
nt.tv_usec += nUSec;
#endif
for(;;)
{
tv.tv_sec = nSec;
tv.tv_usec = nUSec;
bu_select( nSocketTcp+1, &rfds, NULL, NULL, &tv );
nRead += read( ((char *)pBuf)+nRead, nBytes-nRead );
if( nRead >= nBytes )
break;
#ifdef WIN32
DWORD dwNow = GetTickCount();
if( dwNow > dwEnd )
break;
#else
gettimeofday( &ct, NULL );
if( (ct.tv_sec > nt.tv_sec) ||
(ct.tv_sec == nt.tv_sec &&
ct.tv_usec >= nt.tv_usec) )
break;
#endif
}
return nRead;
}
Bu::size Bu::SocketTcp::write( const void *pBuf, Bu::size nBytes )
{
//#ifdef WIN32
int nWrote = TEMP_FAILURE_RETRY(
bu_send( nSocketTcp, (const char *) pBuf, nBytes, 0 ) );
//#else
// int nWrote = TEMP_FAILURE_RETRY( ::write( nSocketTcp, pBuf, nBytes ) );
//#endif
if( nWrote < 0 )
{
#ifdef WIN32
int iWSAError = bu_WSAGetLastError();
if( iWSAError == WSAEWOULDBLOCK )
return 0;
#else
if( errno == EAGAIN ) return 0;
#endif
throw SocketTcpException( SocketTcpException::cWrite, strerror(errno) );
}
return nWrote;
}
Bu::size Bu::SocketTcp::write( const void *pBuf, Bu::size nBytes, uint32_t nSec, uint32_t nUSec )
{
struct timeval tv;
Bu::size nWrote = 0;
fd_set wfds;
FD_ZERO(&wfds);
FD_SET(nSocketTcp, &wfds);
#ifdef WIN32
DWORD dwStart = GetTickCount();
uint64_t uOver = dwStart + ((nUSec / 1000) * (nSec * 1000));
DWORD dwEnd = uOver>4294967295U?uOver-4294967295U:uOver;
#else
struct timeval nt, ct;
gettimeofday( &nt, NULL );
nt.tv_sec += nSec;
nt.tv_usec += nUSec;
#endif
for(;;)
{
tv.tv_sec = nSec;
tv.tv_usec = nUSec;
bu_select( nSocketTcp+1, NULL, &wfds, NULL, &tv );
nWrote += write( ((char *)pBuf)+nWrote, nBytes-nWrote );
if( nWrote >= nBytes )
break;
#ifdef WIN32
DWORD dwNow = GetTickCount();
if( dwNow > dwEnd )
break;
#else
gettimeofday( &ct, NULL );
if( (ct.tv_sec > nt.tv_sec) ||
(ct.tv_sec == nt.tv_sec &&
ct.tv_usec >= nt.tv_usec) )
break;
#endif
}
return nWrote;
}
Bu::size Bu::SocketTcp::tell()
{
throw UnsupportedException();
}
void Bu::SocketTcp::seek( Bu::size )
{
throw UnsupportedException();
}
void Bu::SocketTcp::setPos( Bu::size )
{
throw UnsupportedException();
}
void Bu::SocketTcp::setPosEnd( Bu::size )
{
throw UnsupportedException();
}
bool Bu::SocketTcp::isEos()
{
return !bActive;
}
bool Bu::SocketTcp::canRead()
{
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(nSocketTcp, &rfds);
struct timeval tv = { 0, 0 };
int retval = bu_select( nSocketTcp+1, &rfds, NULL, NULL, &tv );
if( retval == -1 )
throw SocketTcpException(
SocketTcpException::cBadRead,
"Bad Read error"
);
if( !FD_ISSET( nSocketTcp, &rfds ) )
return false;
return true;
}
bool Bu::SocketTcp::canWrite()
{
fd_set wfds;
FD_ZERO(&wfds);
FD_SET(nSocketTcp, &wfds);
struct timeval tv = { 0, 0 };
int retval = bu_select( nSocketTcp+1, NULL, &wfds, NULL, &tv );
if( retval == -1 )
throw SocketTcpException(
SocketTcpException::cBadRead,
"Bad Read error"
);
if( !FD_ISSET( nSocketTcp, &wfds ) )
return false;
return true;
}
bool Bu::SocketTcp::isReadable()
{
return true;
}
bool Bu::SocketTcp::isWritable()
{
return true;
}
bool Bu::SocketTcp::isSeekable()
{
return false;
}
bool Bu::SocketTcp::isBlocking()
{
#ifndef WIN32
return ((fcntl( nSocketTcp, F_GETFL, 0 ) & O_NONBLOCK) != O_NONBLOCK);
#else
return false;
#endif
}
void Bu::SocketTcp::setBlocking( bool bBlocking )
{
this->bBlocking = bBlocking;
#ifndef WIN32
if( bBlocking )
{
fcntl( nSocketTcp, F_SETFL, fcntl( nSocketTcp, F_GETFL, 0 ) & (~O_NONBLOCK) );
}
else
{
fcntl( nSocketTcp, F_SETFL, fcntl( nSocketTcp, F_GETFL, 0 ) | O_NONBLOCK );
}
#else
u_long iMode;
if( bBlocking )
iMode = 0;
else
iMode = 1;
//-------------------------
// Set the socket I/O mode: In this case FIONBIO
// enables or disables the blocking mode for the
// socket based on the numerical value of iMode.
// If iMode = 0, blocking is enabled;
// If iMode != 0, non-blocking mode is enabled.
bu_ioctlsocket(nSocketTcp, FIONBIO, &iMode);
#endif
}
void Bu::SocketTcp::setSize( Bu::size )
{
}
void Bu::SocketTcp::flush()
{
}
bool Bu::SocketTcp::isOpen()
{
return bActive;
}
void Bu::SocketTcp::setAddress()
{
struct sockaddr_in addr;
socklen_t len = sizeof(addr);
addr.sin_family = AF_INET;
bu_getpeername( nSocketTcp, (sockaddr *)(&addr), &len );
sAddress = bu_inet_ntoa( addr.sin_addr );
}
Bu::String Bu::SocketTcp::getAddress() const
{
return sAddress;
}
Bu::SocketTcp::operator Bu::SocketTcp::handle() const
{
return nSocketTcp;
}
Bu::SocketTcp::handle Bu::SocketTcp::getHandle() const
{
return nSocketTcp;
}
Bu::SocketTcp::handle Bu::SocketTcp::takeHandle()
{
handle nRet = nSocketTcp;
bActive = false;
nSocketTcp = 0;
return nRet;
}
Bu::size Bu::SocketTcp::getSize() const
{
throw UnsupportedException();
}
Bu::size Bu::SocketTcp::getBlockSize() const
{
return 1500; //TODO: Fix this, it's stupid.
}
Bu::String Bu::SocketTcp::getLocation() const
{
return getAddress();
}
bool Bu::SocketTcp::getFd( int &rFdOut ) const
{
rFdOut = nSocketTcp;
return true;
}
|