[go: up one dir, main page]

Menu

[r269]: / gizmod3 / libH / Socket.cpp  Maximize  Restore  History

Download this file

566 lines (493 with data), 13.6 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
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
/**
*********************************************************************
*************************************************************************
***
*** \file Socket.cpp
*** \brief Socket class body
***
*****************************************
*****************************************
**/
/*
Copyright (c) 2007, Tim Burrell
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "Socket.hpp"
#include "SocketException.hpp"
#include "Debug.hpp"
#include "UtilTime.hpp"
#include <boost/lexical_cast.hpp>
#include <boost/thread/thread.hpp>
#include <errno.h>
#include <signal.h>
#include <poll.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
using namespace H;
using namespace boost;
using namespace std;
////////////////////////////////////////////////////////////////////////////
// Defines / Type Defs
///////////////////////////////////////
/**
* \def DEFAULT_BACKLOG
* \brief Default backlog setting for listen sockets
**/
#define DEFAULT_BACKLOG 64
/**
* \def PACKET_SIZE
* \brief Size of the buffer when reading from a socket
**/
#define PACKET_SIZE 4096
/**
* \def POLL_TIMEOUT
* \brief Poll timeout in milliseconds
**/
#define POLL_TIMEOUT 1000
/**
* \def STOP_CODON
* \brief Message stop signififer
**/
#define STOP_CODON "\255"
/**
* \def STOP_CODON_CHAR
* \brief Message stop signififer
**/
#define STOP_CODON_CHAR '\255'
////////////////////////////////////////////////////////////////////////////
// Construction / Deconstruction
///////////////////////////////////////
/**
* \brief Default Constructor
**/
Socket::Socket() : mThreadProcRead(this) {
mpEventWatcher = NULL;
init();
}
/**
* \brief Default Constructor
**/
Socket::Socket(Socket const & InitFrom) : mThreadProcRead(this) {
mpEventWatcher = NULL;
init();
setTo(InitFrom);
}
/**
* \brief Destructor
**/
Socket::~Socket() {
shutdown();
}
////////////////////////////////////////////////////////////////////////////
// Class Body
///////////////////////////////////////
/**
* \brief Accept connections
* \return The new socket
*
* Note: blocking
**/
boost::shared_ptr<Socket> Socket::accept() {
// error checking
if (mSocket == SOCKET_ERROR)
throw SocketException("Accept Attempted on Invalid Socket!" + lexical_cast<string>(mPort), __FILE__, __FUNCTION__, __LINE__);
// set up the poll structure
struct pollfd PollFD;
PollFD.fd = mSocket;
PollFD.events = POLLIN | POLLOUT;
PollFD.revents = 0;
// create the new socket
shared_ptr<Socket> pSocket = shared_ptr<Socket>(new Socket(*this));
// wait until there's a connection on the socket
int ret;
do {
if ((ret = poll(&PollFD, 1, POLL_TIMEOUT)) < 0) {
// error
cdbg1 << "Poll error: " << strerror(errno) << endl;
return pSocket;
}
} while (mProcessing && (ret <= 0));
// don't try to accept if we're quitting
if (!mProcessing)
return pSocket;
// accept the connection
pSocket->mSocket = ::accept(mSocket, (struct sockaddr *) &pSocket->mSockAddr, &pSocket->mSockAddrLen);
pSocket->setAddress();
pSocket->mOldSocket = pSocket->mSocket;
return pSocket;
}
/**
* \brief Add to the message buffer
**/
void Socket::addToMessageBuffer(char * Data, int BufLen) {
if (!mMessageMode)
return;
// search for the stop codon
int Index = -1;
for (int lp = 0; lp < BufLen; lp ++) {
if (Data[lp] == STOP_CODON_CHAR) {
Index = lp;
break;
}
}
if (Index > -1) {
// message found!
string Message;
if (mMessageBuffer.length())
Message += mMessageBuffer.getBuffer();
Message += string(Data, Index);
// fire the event
if (mpEventWatcher)
mpEventWatcher->onSocketMessage(*this, Message);
// Check if there's another event
mMessageBuffer.clear();
if (BufLen - Index > 1)
addToMessageBuffer(Data + Index + 1, BufLen - Index - 1);
} else {
// message not found
mMessageBuffer.addToBuffer(Data, BufLen);
}
}
/**
* \brief Bind a socket to a port
* \param Port The port to bind to
**/
void Socket::bind(int Port) {
mPort = Port;
memset(&mSockAddr, 0, sizeof(mSockAddr));
mSockAddr.sin_family = mType;
mSockAddr.sin_port = htons(mPort);
mSockAddr.sin_addr.s_addr = INADDR_ANY;
/* bind the socket to the newly formed address**/
if (::bind(mSocket, (struct sockaddr *) &mSockAddr, sizeof(mSockAddr)))
throw SocketException("Failed to Bind to Port [" + lexical_cast<string>(mPort) + "]", __FILE__, __FUNCTION__, __LINE__);
}
/**
* \brief Close the socket
**/
void Socket::closeSocket() {
if (mSocket != SOCKET_ERROR) {
#ifndef WIN32
if (::close(mSocket) == SOCKET_ERROR)
#else
if (::closesocket(mSocket) == SOCKET_ERROR)
#endif
throw SocketException("Failed to Close Socket [" + lexical_cast<string>(mSocket) + "]", __FILE__, __FUNCTION__, __LINE__);
}
#ifdef HAVE_OPENSSL
if (mSSL) {
SSL_free(mSSL);
mSSL = NULL;
}
#endif
mOldSocket = mSocket;
init();
}
/**
* \brief Connect to a server on Port at Host
* \param Host The server to connect to
* \param Port The port to connect to
**/
void Socket::connect(std::string Host, int Port) {
// error checking
if (mSocket == SOCKET_ERROR)
throw SocketException("Connect Attempted on Invalid Socket!", __FILE__, __FUNCTION__, __LINE__);
// get hostname
struct hostent * hp = gethostbyname(Host.c_str());
if (!hp)
throw SocketException("Connect Failed to Resolve Host [" + Host + "]", __FILE__, __FUNCTION__, __LINE__);
// Set up the data structures
mPort = Port;
#ifndef WIN32
struct in_addr address;
memcpy(&address, *(hp->h_addr_list), sizeof(struct in_addr));
mSockAddr.sin_addr = address;
#else
memset(&mSockAddr, 0, sizeof(mSockAddr));
mSockAddr.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
#endif
mSockAddr.sin_family = AF_INET;
mSockAddr.sin_port = htons(mPort);
cdbg4 << "Initiating connection to [" << Host << ":" << mPort << "]" << endl;
#ifdef WIN32
return tryConnectingWindows();
#endif
if (::connect(mSocket, (struct sockaddr *) &mSockAddr, sizeof(mSockAddr)) == -1) {
closeSocket();
throw SocketException("Connect Attempted to [" + Host + ":" + lexical_cast<string>(Port) + "] Failed -- " + strerror(errno), __FILE__, __FUNCTION__, __LINE__);
}
setAddress();
// connection!
if (mpEventWatcher)
mpEventWatcher->onSocketConnect(*this);
}
/**
* \brief Create a socket
**/
void Socket::createSocket(SocketDomain Domain, SocketType Type) {
mDomain = Domain;
mType = Type;
#ifdef HAVE_OPENSSL
if ( (mSSLMode) && (!initializeCTX()) )
throw SocketException("Failed to Initialize OpenSSL", __FILE__, __FUNCTION__, __LINE__);
#endif
if ((mSocket = socket(mDomain, mType, mProtocol)) == -1)
throw SocketException(string("Failed to Create Socket -- ") + strerror(errno), __FILE__, __FUNCTION__, __LINE__);
mOldSocket = mSocket;
}
/**
* \brief Get the socket's address / hostname
* \return The address / hostname
**/
std::string Socket::getAddress() const {
return mAddress;
}
/**
* \brief Get the old socket (what it was before disconnect)
* \return The old socket
**/
int Socket::getOldSocket() const {
return mOldSocket;
}
/**
* \brief Get the socket
* \return The socket
**/
int Socket::getSocket() const {
return mSocket;
}
/**
* \brief Handle a socket disconnect
**/
void Socket::handleSocketDisconnect() {
closeSocket();
if (mpEventWatcher)
mpEventWatcher->onSocketDisconnect(*this);
}
/**
* \brief Handle a socket read
**/
void Socket::handleSocketRead(DynamicBuffer<char> & ReadBuffer) {
if (mpEventWatcher)
mpEventWatcher->onSocketRead(*this, ReadBuffer);
}
/**
* \brief Init the socket
**/
void Socket::init() {
mBacklog = DEFAULT_BACKLOG;
mDomain = SOCKET_INTERNET;
mMessageMode = false;
mPort = 0;
mProcessing = false;
mProtocol = SOCKET_PROTO_TCP;
mSockAddrLen = sizeof(struct sockaddr);
mSocket = SOCKET_ERROR;
mType = SOCKET_STREAM;
}
/**
* \brief Test if a socket is valid or not
* \return True if valid
**/
bool Socket::isSocketValid() const {
return (mSocket != SOCKET_ERROR);
}
/**
* \brief Listen on a socket
**/
void Socket::listen() {
if (::listen(mSocket, mBacklog) == -1)
throw SocketException(string("Failed to Listen on Socket -- ") + strerror(errno), __FILE__, __FUNCTION__, __LINE__);
}
/**
* \brief Process events on the socket
**/
void Socket::processEvents() {
// initialize the read thread
thread thrd(mThreadProcRead);
}
/**
* \brief Read from a socket
* \param Buffer The buffer to read into
* \param BufLen The length of the buffer
**/
int Socket::read(char * Buffer, int BufLen) {
// do the receiving
int ret;
#ifdef HAVE_OPENSSL
if (m_SSLMode) {
SSL_set_bio(mSSL, mSSLbio, mSSLbio);
ret = SSL_read(mSSL, Buffer, BufLen);
} else
ret = ::recv(mSocket, Buffer, sizeof(char) * BufLen, 0);
#else
ret = ::recv(mSocket, Buffer, sizeof(char) * BufLen, 0);
#endif
// value is 0 if socket has disconnected
if (ret == 0)
handleSocketDisconnect();
return ret;
}
/**
* \brief Read from the socket, info a buffer
* \param Buffer The buffer to read into
* \return Number of bytes read from socket
**/
int Socket::readIntoBuffer(DynamicBuffer<char> & Buffer) {
char Packet[PACKET_SIZE];
int TotalBytesRead = 0;
int BytesRead;
do {
if ((BytesRead = read(Packet, PACKET_SIZE)) == -1) {
#ifndef WIN32
switch (errno) {
case EINPROGRESS:
case EALREADY:
case EAGAIN:
#else
switch (WSAGetLastError()) {
case WSAEINPROGRESS:
case WSAEALREADY:
case WSAEWOULDBLOCK:
#endif
// this is just fine
return TotalBytesRead;
default:
cdbg << "Socket Read Error -- " << strerror(errno) << endl;
handleSocketDisconnect();
return TotalBytesRead;
}
}
if (BytesRead > 0) {
Buffer.addToBuffer(Packet, BytesRead);
addToMessageBuffer(Packet, BytesRead);
TotalBytesRead += BytesRead;
}
} while ( (BytesRead == PACKET_SIZE) && (BytesRead > 0) );
return TotalBytesRead;
}
/**
* \brief Set the socket's address from internal structures
**/
void Socket::setAddress() {
mAddress = inet_ntoa(mSockAddr.sin_addr);
}
/**
* \brief Set the event watcher
* \param pWatcher The event watcher
**/
void Socket::setEventWatcher(SocketEventWatcher * pWatcher) {
mpEventWatcher = pWatcher;
}
/**
* \brief Enable automatic message handling mode?
* \param Enabled True if message mode should be enabled
**/
void Socket::setMessageMode(bool Enabled) {
mMessageMode = Enabled;
}
/**
* \brief Set the current socket to another socket
* \param SocketToBecome The socket to copy from
**/
void Socket::setTo(Socket const & SocketToBecome) {
mProtocol = SocketToBecome.mProtocol;
mDomain = SocketToBecome.mDomain;
mType = SocketToBecome.mType;
mPort = SocketToBecome.mPort;
mBacklog = SocketToBecome.mBacklog;
#ifdef HAVE_OPENSSL
mSSL = SocketToBecome.mSSL;
mSSLMode = SocketToBecome.mSSLMode;
#endif
}
/**
* \brief Shutdown all socket processing
**/
void Socket::shutdown() {
mProcessing = false;
}
/**
* \brief Thread procedure for reading from the socket
**/
void Socket::threadProcRead() {
// set up the poll structure
struct pollfd PollFD;
PollFD.fd = mSocket;
PollFD.events = POLLIN | POLLOUT;
PollFD.revents = 0;
// loop
mProcessing = true;
while ( mProcessing && isSocketValid() ) {
// wait until there's data on the socket
int ret;
do {
if ((ret = poll(&PollFD, 1, POLL_TIMEOUT)) < 0) {
// error
handleSocketDisconnect();
return;
}
} while (mProcessing && (ret <= 0));
DynamicBuffer<char> ReadBuffer;
if (readIntoBuffer(ReadBuffer) > 0)
handleSocketRead(ReadBuffer);
}
}
/**
* \brief Write data to the socket
* \param Buffer The data to send
* \param BufLen Length of data to send
* \return Bytes sent
**/
int Socket::write(const char * Buffer, int BufLen) {
#ifdef HAVE_OPENSSL
if (mSSLMode) {
SSL_set_bio(mSSL, mSSLbio, mSSLbio);
return SSL_write(mSSL, Buffer, BufLen);
} else
#ifndef WIN32
return ::write(mSocket, Buffer, sizeof(char) * BufLen);
#else
return ::send(mSocket, Buffer, sizeof(char) * BufLen, 0);
#endif
#else
#ifndef WIN32
return ::write(mSocket, Buffer, sizeof(char) * BufLen);
#else
return ::send(mSocket, Buffer, sizeof(char) * BufLen, 0);
#endif
#endif
}
/**
* \brief Write message to socket
* \param Message The message to write
* \param FormatMessage Format the massage for easy receiving
*
* This function send a message, and makes sure it gets fully sent
* or else it throws an exception
*
* It also formats the message for easy decomposition by SocketServer
**/
void Socket::writeMessage(std::string const & Message, bool FormatMessage) {
// format the message
string OutMessage = Message;
if (FormatMessage)
OutMessage += STOP_CODON;
size_t CurPos = 0;
int BytesWritten;
do {
if ((BytesWritten = write(OutMessage.c_str() + CurPos, OutMessage.length() - CurPos)) == -1)
throw SocketException(string("Failed to Write Message to Socket -- ") + strerror(errno), __FILE__, __FUNCTION__, __LINE__);
CurPos += BytesWritten;
} while (CurPos < OutMessage.length());
}