| 1 |
/*---------------------------------------------------------------------------*/ |
| 2 |
/* */ |
| 3 |
/* SimpleSocket.h - Simple Socket base class decleration. */ |
| 4 |
/* */ |
| 5 |
/* Author : Mark Carrier ([email protected]) */ |
| 6 |
/* */ |
| 7 |
/*---------------------------------------------------------------------------*/ |
| 8 |
/* Copyright (c) 2007-2009 CarrierLabs, LLC. All rights reserved. |
| 9 |
* |
| 10 |
* Redistribution and use in source and binary forms, with or without |
| 11 |
* modification, are permitted provided that the following conditions |
| 12 |
* are met: |
| 13 |
* |
| 14 |
* 1. Redistributions of source code must retain the above copyright |
| 15 |
* notice, this list of conditions and the following disclaimer. |
| 16 |
* |
| 17 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 18 |
* notice, this list of conditions and the following disclaimer in |
| 19 |
* the documentation and/or other materials provided with the |
| 20 |
* distribution. |
| 21 |
* |
| 22 |
* 3. The name of the author may not be used to endorse or promote products |
| 23 |
* derived from this software without specific prior written permission. |
| 24 |
* |
| 25 |
* 4. The name "CarrierLabs" must not be used to |
| 26 |
* endorse or promote products derived from this software without |
| 27 |
* prior written permission. For written permission, please contact |
| 28 |
* [email protected]. |
| 29 |
* |
| 30 |
* THIS SOFTWARE IS PROVIDED BY MARK CARRIER ``AS IS'' AND ANY |
| 31 |
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 32 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 33 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MARK CARRIER OR |
| 34 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 35 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 36 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 37 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 38 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 39 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 40 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 41 |
* OF THE POSSIBILITY OF SUCH DAMAGE. |
| 42 |
*----------------------------------------------------------------------------*/ |
| 43 |
#ifndef __SOCKET_H__ |
| 44 |
#define __SOCKET_H__ |
| 45 |
|
| 46 |
#include <sys/stat.h> |
| 47 |
#include <stdlib.h> |
| 48 |
#include <stdio.h> |
| 49 |
#include <stdarg.h> |
| 50 |
#include <errno.h> |
| 51 |
|
| 52 |
#if defined(__linux__) || defined (_DARWIN) |
| 53 |
#include <sys/socket.h> |
| 54 |
#include <netinet/in.h> |
| 55 |
#include <arpa/inet.h> |
| 56 |
#include <netinet/tcp.h> |
| 57 |
#include <netinet/ip.h> |
| 58 |
#include <netdb.h> |
| 59 |
#endif |
| 60 |
#ifdef __linux__ |
| 61 |
#include <linux/if_packet.h> |
| 62 |
#include <linux/if_ether.h> |
| 63 |
#include <linux/if.h> |
| 64 |
#include <sys/sendfile.h> |
| 65 |
#endif |
| 66 |
#ifdef _DARWIN |
| 67 |
#include <net/if.h> |
| 68 |
#endif |
| 69 |
#if defined(__linux__) || defined (_DARWIN) |
| 70 |
#include <sys/time.h> |
| 71 |
#include <sys/uio.h> |
| 72 |
#include <unistd.h> |
| 73 |
#include <fcntl.h> |
| 74 |
#endif |
| 75 |
|
| 76 |
#ifdef _WIN32 |
| 77 |
#include <io.h> |
| 78 |
#include <winsock2.h> |
| 79 |
#include <Ws2tcpip.h> |
| 80 |
|
| 81 |
#define IPTOS_LOWDELAY 0x10 |
| 82 |
|
| 83 |
#endif |
| 84 |
#include "Host.h" |
| 85 |
#include "StatTimer.h" |
| 86 |
|
| 87 |
//----------------------------------------------------------------------------- |
| 88 |
// General class macro definitions and typedefs |
| 89 |
//----------------------------------------------------------------------------- |
| 90 |
#ifndef INVALID_SOCKET |
| 91 |
#define INVALID_SOCKET ~(0) |
| 92 |
#endif |
| 93 |
|
| 94 |
#define SOCKET_SENDFILE_BLOCKSIZE 8192 |
| 95 |
|
| 96 |
/// Provides a platform independent class to for socket development. |
| 97 |
/// This class is designed to abstract socket communication development in a |
| 98 |
/// platform independent manner. |
| 99 |
/// - Socket types |
| 100 |
/// -# CActiveSocket Class |
| 101 |
/// -# CPassiveSocket Class |
| 102 |
class CSimpleSocket { |
| 103 |
public: |
| 104 |
/// Defines the three possible states for shuting down a socket. |
| 105 |
typedef enum |
| 106 |
{ |
| 107 |
Receives = SHUT_RD, ///< Shutdown passive socket. |
| 108 |
Sends = SHUT_WR, ///< Shutdown active socket. |
| 109 |
Both = SHUT_RDWR ///< Shutdown both active and passive sockets. |
| 110 |
} CShutdownMode; |
| 111 |
|
| 112 |
/// Defines the socket types defined by CSimpleSocket class. |
| 113 |
typedef enum |
| 114 |
{ |
| 115 |
SocketTypeInvalid, ///< Invalid socket type. |
| 116 |
SocketTypeTcp, ///< Defines socket as TCP socket. |
| 117 |
SocketTypeUdp, ///< Defines socket as UDP socket. |
| 118 |
SocketTypeTcp6, ///< Defines socket as IPv6 TCP socket. |
| 119 |
SocketTypeUdp6, ///< Defines socket as IPv6 UDP socket. |
| 120 |
SocketTypeRaw ///< Provides raw network protocol access. |
| 121 |
} CSocketType; |
| 122 |
|
| 123 |
/// Defines all error codes handled by the CSimpleSocket class. |
| 124 |
typedef enum |
| 125 |
{ |
| 126 |
SocketError = -1, ///< Generic socket error translates to error below. |
| 127 |
SocketSuccess = 0, ///< No socket error. |
| 128 |
SocketInvalidSocket, ///< Invalid socket handle. |
| 129 |
SocketInvalidAddress, ///< Invalid destination address specified. |
| 130 |
SocketInvalidPort, ///< Invalid destination port specified. |
| 131 |
SocketConnectionRefused, ///< No server is listening at remote address. |
| 132 |
SocketTimedout, ///< Timed out while attempting operation. |
| 133 |
SocketEwouldblock, ///< Operation would block if socket were blocking. |
| 134 |
SocketNotconnected, ///< Currently not connected. |
| 135 |
SocketEinprogress, ///< Socket is non-blocking and the connection cannot be completed immediately |
| 136 |
SocketInterrupted, ///< Call was interrupted by a signal that was caught before a valid connection arrived. |
| 137 |
SocketConnectionAborted, ///< The connection has been aborted. |
| 138 |
SocketProtocolError, ///< Invalid protocol for operation. |
| 139 |
SocketFirewallError, ///< Firewall rules forbid connection. |
| 140 |
SocketInvalidSocketBuffer, ///< The receive buffer point outside the process's address space. |
| 141 |
SocketConnectionReset, ///< Connection was forcibly closed by the remote host. |
| 142 |
SocketAddressInUse, ///< Address already in use. |
| 143 |
SocketInvalidPointer, ///< Pointer type supplied as argument is invalid. |
| 144 |
SocketEunknown ///< Unknown error please report to [email protected] |
| 145 |
} CSocketError; |
| 146 |
|
| 147 |
public: |
| 148 |
CSimpleSocket(CSocketType type = SocketTypeTcp); |
| 149 |
CSimpleSocket(CSimpleSocket &socket); |
| 150 |
|
| 151 |
virtual ~CSimpleSocket() |
| 152 |
{ |
| 153 |
if (m_pBuffer != NULL) |
| 154 |
{ |
| 155 |
delete [] m_pBuffer; |
| 156 |
m_pBuffer = NULL; |
| 157 |
} |
| 158 |
}; |
| 159 |
|
| 160 |
/// Initialize instance of CSocket. This method MUST be called before an |
| 161 |
/// object can be used. Errors : CSocket::SocketProtocolError, |
| 162 |
/// CSocket::SocketInvalidSocket, |
| 163 |
/// @return true if properly initialized. |
| 164 |
virtual bool Initialize(void); |
| 165 |
|
| 166 |
/// Close socket |
| 167 |
/// @return true if successfully closed otherwise returns false. |
| 168 |
virtual bool Close(void); |
| 169 |
|
| 170 |
/// Shutdown shut down socket send and receive operations |
| 171 |
/// CShutdownMode::Receives - Disables further receive operations. |
| 172 |
/// CShutdownMode::Sends - Disables further send operations. |
| 173 |
/// CShutdownBoth:: - Disables further send and receive operations. |
| 174 |
/// @param nShutdown specifies the type of shutdown. |
| 175 |
/// @return true if successfully shutdown otherwise returns false. |
| 176 |
virtual bool Shutdown(CShutdownMode nShutdown); |
| 177 |
|
| 178 |
/// Examine the socket descriptor sets currently owned by the instance of |
| 179 |
/// the socket class (the readfds, writefds, and errorfds parameters) to |
| 180 |
/// see whether some of their descriptors are ready for reading, are ready |
| 181 |
/// for writing, or have an exceptional condition pending, respectively. |
| 182 |
/// Block until an event happens on the specified file descriptors. |
| 183 |
/// @return true if socket has data ready, or false if not ready or timed out. |
| 184 |
virtual bool Select(void) { |
| 185 |
return Select(0,0); |
| 186 |
}; |
| 187 |
|
| 188 |
/// Examine the socket descriptor sets currently owned by the instance of |
| 189 |
/// the socket class (the readfds, writefds, and errorfds parameters) to |
| 190 |
/// see whether some of their descriptors are ready for reading, are ready |
| 191 |
/// for writing, or have an exceptional condition pending, respectively. |
| 192 |
/// @param nTimeoutSec timeout in seconds for select. |
| 193 |
/// @param nTimeoutUSec timeout in micro seconds for select. |
| 194 |
/// @return true if socket has data ready, or false if not ready or timed out. |
| 195 |
virtual bool Select(int32 nTimeoutSec, int32 nTimeoutUSec); |
| 196 |
|
| 197 |
/// Does the current instance of the socket object contain a valid socket |
| 198 |
/// descriptor. |
| 199 |
/// @return true if the socket object contains a valid socket descriptor. |
| 200 |
virtual bool IsSocketValid(void) { |
| 201 |
return (m_socket != SocketError); |
| 202 |
}; |
| 203 |
|
| 204 |
/// Provides a standard error code for cross platform development by |
| 205 |
/// mapping the operating system error to an error defined by the CSocket |
| 206 |
/// class. |
| 207 |
void TranslateSocketError(void); |
| 208 |
|
| 209 |
/// Returns a human-readable description of the given error code |
| 210 |
/// or the last error code of a socket |
| 211 |
static const char *DescribeError(CSocketError err); |
| 212 |
inline const char *DescribeError() { |
| 213 |
return DescribeError(m_socketErrno); |
| 214 |
}; |
| 215 |
|
| 216 |
/// Attempts to receive a block of data on an established connection. |
| 217 |
/// @param nMaxBytes maximum number of bytes to receive. |
| 218 |
/// @param pBuffer, memory where to receive the data, |
| 219 |
/// NULL receives to internal buffer returned with GetData() |
| 220 |
/// Non-NULL receives directly there, but GetData() will return WRONG ptr! |
| 221 |
/// @return number of bytes actually received. |
| 222 |
/// @return of zero means the connection has been shutdown on the other side. |
| 223 |
/// @return of -1 means that an error has occurred. |
| 224 |
virtual int32 Receive(int32 nMaxBytes = 1, uint8 * pBuffer = 0); |
| 225 |
|
| 226 |
/// Attempts to send a block of data on an established connection. |
| 227 |
/// @param pBuf block of data to be sent. |
| 228 |
/// @param bytesToSend size of data block to be sent. |
| 229 |
/// @return number of bytes actually sent. |
| 230 |
/// @return of zero means the connection has been shutdown on the other side. |
| 231 |
/// @return of -1 means that an error has occurred. |
| 232 |
virtual int32 Send(const uint8 *pBuf, size_t bytesToSend); |
| 233 |
|
| 234 |
/// Attempts to send at most nNumItem blocks described by sendVector |
| 235 |
/// to the socket descriptor associated with the socket object. |
| 236 |
/// @param sendVector pointer to an array of iovec structures |
| 237 |
/// @param nNumItems number of items in the vector to process |
| 238 |
/// <br>\b NOTE: Buffers are processed in the order specified. |
| 239 |
/// @return number of bytes actually sent, return of zero means the |
| 240 |
/// connection has been shutdown on the other side, and a return of -1 |
| 241 |
/// means that an error has occurred. |
| 242 |
virtual int32 Send(const struct iovec *sendVector, int32 nNumItems); |
| 243 |
|
| 244 |
/// Copies data between one file descriptor and another. |
| 245 |
/// On some systems this copying is done within the kernel, and thus is |
| 246 |
/// more efficient than the combination of CSimpleSocket::Send and |
| 247 |
/// CSimpleSocket::Receive, which would require transferring data to and |
| 248 |
/// from user space. |
| 249 |
/// <br>\b Note: This is available on all implementations, but the kernel |
| 250 |
/// implementation is only available on Unix type systems. |
| 251 |
/// @param nOutFd descriptor opened for writing. |
| 252 |
/// @param nInFd descriptor opened for reading. |
| 253 |
/// @param pOffset from which to start reading data from input file. |
| 254 |
/// @param nCount number of bytes to copy between file descriptors. |
| 255 |
/// @return number of bytes written to the out socket descriptor. |
| 256 |
virtual int32 SendFile(int32 nOutFd, int32 nInFd, off_t *pOffset, int32 nCount); |
| 257 |
|
| 258 |
/// Returns blocking/non-blocking state of socket. |
| 259 |
/// @return true if the socket is non-blocking, else return false. |
| 260 |
bool IsNonblocking(void) { |
| 261 |
return (m_bIsBlocking == false); |
| 262 |
}; |
| 263 |
|
| 264 |
/// Set the socket to blocking. |
| 265 |
/// @return true if successful set to blocking, else return false; |
| 266 |
bool SetBlocking(void); |
| 267 |
|
| 268 |
/// Set the socket as non-blocking. |
| 269 |
/// @return true if successful set to non-blocking, else return false; |
| 270 |
bool SetNonblocking(void); |
| 271 |
|
| 272 |
/// Get a pointer to internal receive buffer. The user MUST not free this |
| 273 |
/// pointer when finished. This memory is managed internally by the CSocket |
| 274 |
/// class. |
| 275 |
/// @return pointer to data if valid, else returns NULL. |
| 276 |
uint8 *GetData(void) { |
| 277 |
return m_pBuffer; |
| 278 |
}; |
| 279 |
|
| 280 |
/// Returns the number of bytes received on the last call to |
| 281 |
/// CSocket::Receive(). |
| 282 |
/// @return number of bytes received. |
| 283 |
int32 GetBytesReceived(void) { |
| 284 |
return m_nBytesReceived; |
| 285 |
}; |
| 286 |
|
| 287 |
/// Returns the number of bytes sent on the last call to |
| 288 |
/// CSocket::Send(). |
| 289 |
/// @return number of bytes sent. |
| 290 |
int32 GetBytesSent(void) { |
| 291 |
return m_nBytesSent; |
| 292 |
}; |
| 293 |
|
| 294 |
/// Controls the actions taken when CSimpleSocket::Close is executed on a |
| 295 |
/// socket object that has unsent data. The default value for this option |
| 296 |
/// is \b off. |
| 297 |
/// - Following are the three possible scenarios. |
| 298 |
/// -# \b bEnable is false, CSimpleSocket::Close returns immediately, but |
| 299 |
/// any unset data is transmitted (after CSimpleSocket::Close returns) |
| 300 |
/// -# \b bEnable is true and \b nTime is zero, CSimpleSocket::Close return |
| 301 |
/// immediately and any unsent data is discarded. |
| 302 |
/// -# \b bEnable is true and \b nTime is nonzero, CSimpleSocket::Close does |
| 303 |
/// not return until all unsent data is transmitted (or the connection is |
| 304 |
/// Closed by the remote system). |
| 305 |
/// <br><p> |
| 306 |
/// @param bEnable true to enable option false to disable option. |
| 307 |
/// @param nTime time in seconds to linger. |
| 308 |
/// @return true if option successfully set |
| 309 |
bool SetOptionLinger(bool bEnable, uint16 nTime); |
| 310 |
|
| 311 |
/// Tells the kernel that even if this port is busy (in the TIME_WAIT state), |
| 312 |
/// go ahead and reuse it anyway. If it is busy, but with another state, |
| 313 |
/// you will still get an address already in use error. |
| 314 |
/// @return true if option successfully set |
| 315 |
bool SetOptionReuseAddr(); |
| 316 |
|
| 317 |
/// Gets the timeout value that specifies the maximum number of seconds a |
| 318 |
/// call to CSimpleSocket::Open waits until it completes. |
| 319 |
/// @return the length of time in seconds |
| 320 |
int32 GetConnectTimeoutSec(void) { |
| 321 |
return m_stConnectTimeout.tv_sec; |
| 322 |
}; |
| 323 |
|
| 324 |
/// Gets the timeout value that specifies the maximum number of microseconds |
| 325 |
/// a call to CSimpleSocket::Open waits until it completes. |
| 326 |
/// @return the length of time in microseconds |
| 327 |
int32 GetConnectTimeoutUSec(void) { |
| 328 |
return m_stConnectTimeout.tv_usec; |
| 329 |
}; |
| 330 |
|
| 331 |
/// Sets the timeout value that specifies the maximum amount of time a call |
| 332 |
/// to CSimpleSocket::Receive waits until it completes. Use the method |
| 333 |
/// CSimpleSocket::SetReceiveTimeout to specify the number of seconds to wait. |
| 334 |
/// If a call to CSimpleSocket::Receive has blocked for the specified length of |
| 335 |
/// time without receiving additional data, it returns with a partial count |
| 336 |
/// or CSimpleSocket::GetSocketError set to CSimpleSocket::SocketEwouldblock if no data |
| 337 |
/// were received. |
| 338 |
/// @param nConnectTimeoutSec of timeout in seconds. |
| 339 |
/// @param nConnectTimeoutUsec of timeout in microseconds. |
| 340 |
/// @return true if socket connection timeout was successfully set. |
| 341 |
void SetConnectTimeout(int32 nConnectTimeoutSec, int32 nConnectTimeoutUsec = 0) |
| 342 |
{ |
| 343 |
m_stConnectTimeout.tv_sec = nConnectTimeoutSec; |
| 344 |
m_stConnectTimeout.tv_usec = nConnectTimeoutUsec; |
| 345 |
}; |
| 346 |
|
| 347 |
/// Gets the timeout value that specifies the maximum number of seconds a |
| 348 |
/// a call to CSimpleSocket::Receive waits until it completes. |
| 349 |
/// @return the length of time in seconds |
| 350 |
int32 GetReceiveTimeoutSec(void) { |
| 351 |
return m_stRecvTimeout.tv_sec; |
| 352 |
}; |
| 353 |
|
| 354 |
/// Gets the timeout value that specifies the maximum number of microseconds |
| 355 |
/// a call to CSimpleSocket::Receive waits until it completes. |
| 356 |
/// @return the length of time in microseconds |
| 357 |
int32 GetReceiveTimeoutUSec(void) { |
| 358 |
return m_stRecvTimeout.tv_usec; |
| 359 |
}; |
| 360 |
|
| 361 |
/// Sets the timeout value that specifies the maximum amount of time a call |
| 362 |
/// to CSimpleSocket::Receive waits until it completes. Use the method |
| 363 |
/// CSimpleSocket::SetReceiveTimeout to specify the number of seconds to wait. |
| 364 |
/// If a call to CSimpleSocket::Receive has blocked for the specified length of |
| 365 |
/// time without receiving additional data, it returns with a partial count |
| 366 |
/// or CSimpleSocket::GetSocketError set to CSimpleSocket::SocketEwouldblock if no data |
| 367 |
/// were received. |
| 368 |
/// @param nRecvTimeoutSec of timeout in seconds. |
| 369 |
/// @param nRecvTimeoutUsec of timeout in microseconds. |
| 370 |
/// @return true if socket timeout was successfully set. |
| 371 |
bool SetReceiveTimeout(int32 nRecvTimeoutSec, int32 nRecvTimeoutUsec = 0); |
| 372 |
|
| 373 |
/// Enable/disable multicast for a socket. This options is only valid for |
| 374 |
/// socket descriptors of type CSimpleSocket::SocketTypeUdp. |
| 375 |
/// @return true if multicast was enabled or false if socket type is not |
| 376 |
/// CSimpleSocket::SocketTypeUdp and the error will be set to |
| 377 |
/// CSimpleSocket::SocketProtocolError |
| 378 |
bool SetMulticast(bool bEnable, uint8 multicastTTL = 1); |
| 379 |
|
| 380 |
/// Return true if socket is multicast or false is socket is unicast |
| 381 |
/// @return true if multicast is enabled |
| 382 |
bool GetMulticast() { |
| 383 |
return m_bIsMulticast; |
| 384 |
}; |
| 385 |
|
| 386 |
/// Bind socket to a specific interface when using multicast. |
| 387 |
/// @return true if successfully bound to interface |
| 388 |
bool BindInterface(const char *pInterface); |
| 389 |
|
| 390 |
/// Gets the timeout value that specifies the maximum number of seconds a |
| 391 |
/// a call to CSimpleSocket::Send waits until it completes. |
| 392 |
/// @return the length of time in seconds |
| 393 |
int32 GetSendTimeoutSec(void) { |
| 394 |
return m_stSendTimeout.tv_sec; |
| 395 |
}; |
| 396 |
|
| 397 |
/// Gets the timeout value that specifies the maximum number of microseconds |
| 398 |
/// a call to CSimpleSocket::Send waits until it completes. |
| 399 |
/// @return the length of time in microseconds |
| 400 |
int32 GetSendTimeoutUSec(void) { |
| 401 |
return m_stSendTimeout.tv_usec; |
| 402 |
}; |
| 403 |
|
| 404 |
/// Gets the timeout value that specifies the maximum amount of time a call |
| 405 |
/// to CSimpleSocket::Send waits until it completes. |
| 406 |
/// @return the length of time in seconds |
| 407 |
bool SetSendTimeout(int32 nSendTimeoutSec, int32 nSendTimeoutUsec = 0); |
| 408 |
|
| 409 |
/// Returns the last error that occured for the instace of the CSimpleSocket |
| 410 |
/// instance. This method should be called immediately to retrieve the |
| 411 |
/// error code for the failing mehtod call. |
| 412 |
/// @return last error that occured. |
| 413 |
CSocketError GetSocketError(void) { |
| 414 |
return m_socketErrno; |
| 415 |
}; |
| 416 |
|
| 417 |
/// Get the total time the of the last operation in milliseconds. |
| 418 |
/// @return number of milliseconds of last operation. |
| 419 |
uint32 GetTotalTimeMs() { |
| 420 |
return m_timer.GetMilliSeconds(); |
| 421 |
}; |
| 422 |
|
| 423 |
/// Get the total time the of the last operation in microseconds. |
| 424 |
/// @return number of microseconds or last operation. |
| 425 |
uint32 GetTotalTimeUsec() { |
| 426 |
return m_timer.GetMicroSeconds(); |
| 427 |
}; |
| 428 |
|
| 429 |
/// Return Differentiated Services Code Point (DSCP) value currently set on the socket object. |
| 430 |
/// @return DSCP for current socket object. |
| 431 |
/// <br><br> \b NOTE: Windows special notes http://support.microsoft.com/kb/248611. |
| 432 |
int GetSocketDscp(void); |
| 433 |
|
| 434 |
/// Set Differentiated Services Code Point (DSCP) for socket object. |
| 435 |
/// @param nDscp value of TOS setting which will be converted to DSCP |
| 436 |
/// @return true if DSCP value was properly set |
| 437 |
/// <br><br> \b NOTE: Windows special notes http://support.microsoft.com/kb/248611. |
| 438 |
bool SetSocketDscp(int nDscp); |
| 439 |
|
| 440 |
/// Return socket descriptor |
| 441 |
/// @return socket descriptor which is a signed 32 bit integer. |
| 442 |
SOCKET GetSocketDescriptor() { |
| 443 |
return m_socket; |
| 444 |
}; |
| 445 |
|
| 446 |
/// Return socket descriptor |
| 447 |
/// @return socket descriptor which is a signed 32 bit integer. |
| 448 |
CSocketType GetSocketType() { |
| 449 |
return m_nSocketType; |
| 450 |
}; |
| 451 |
|
| 452 |
/// Returns clients Internet host address as a string in standard numbers-and-dots notation. |
| 453 |
/// @return NULL if invalid |
| 454 |
const char *GetClientAddr() { |
| 455 |
return inet_ntoa(m_stClientSockaddr.sin_addr); |
| 456 |
}; |
| 457 |
|
| 458 |
/// Returns the port number on which the client is connected. |
| 459 |
/// @return client port number. |
| 460 |
uint16 GetClientPort() { |
| 461 |
return m_stClientSockaddr.sin_port; |
| 462 |
}; |
| 463 |
|
| 464 |
/// Returns server Internet host address as a string in standard numbers-and-dots notation. |
| 465 |
/// @return NULL if invalid |
| 466 |
const char *GetServerAddr() { |
| 467 |
return inet_ntoa(m_stServerSockaddr.sin_addr); |
| 468 |
}; |
| 469 |
|
| 470 |
/// Returns the port number on which the server is connected. |
| 471 |
/// @return server port number. |
| 472 |
uint16 GetServerPort() { |
| 473 |
return ntohs(m_stServerSockaddr.sin_port); |
| 474 |
}; |
| 475 |
|
| 476 |
/// Get the TCP receive buffer window size for the current socket object. |
| 477 |
/// <br><br>\b NOTE: Linux will set the receive buffer to twice the value passed. |
| 478 |
/// @return zero on failure else the number of bytes of the TCP receive buffer window size if successful. |
| 479 |
uint32 GetReceiveWindowSize() { |
| 480 |
return GetWindowSize(SO_RCVBUF); |
| 481 |
}; |
| 482 |
|
| 483 |
/// Get the TCP send buffer window size for the current socket object. |
| 484 |
/// <br><br>\b NOTE: Linux will set the send buffer to twice the value passed. |
| 485 |
/// @return zero on failure else the number of bytes of the TCP receive buffer window size if successful. |
| 486 |
uint32 GetSendWindowSize() { |
| 487 |
return GetWindowSize(SO_SNDBUF); |
| 488 |
}; |
| 489 |
|
| 490 |
/// Set the TCP receive buffer window size for the current socket object. |
| 491 |
/// <br><br>\b NOTE: Linux will set the receive buffer to twice the value passed. |
| 492 |
/// @return zero on failure else the number of bytes of the TCP send buffer window size if successful. |
| 493 |
uint32 SetReceiveWindowSize(uint32 nWindowSize) { |
| 494 |
return SetWindowSize(SO_RCVBUF, nWindowSize); |
| 495 |
}; |
| 496 |
|
| 497 |
/// Set the TCP send buffer window size for the current socket object. |
| 498 |
/// <br><br>\b NOTE: Linux will set the send buffer to twice the value passed. |
| 499 |
/// @return zero on failure else the number of bytes of the TCP send buffer window size if successful. |
| 500 |
uint32 SetSendWindowSize(uint32 nWindowSize) { |
| 501 |
return SetWindowSize(SO_SNDBUF, nWindowSize); |
| 502 |
}; |
| 503 |
|
| 504 |
/// Disable the Nagle algorithm (Set TCP_NODELAY to true) |
| 505 |
/// @return false if failed to set socket option otherwise return true; |
| 506 |
bool DisableNagleAlgoritm(); |
| 507 |
|
| 508 |
/// Enable the Nagle algorithm (Set TCP_NODELAY to false) |
| 509 |
/// @return false if failed to set socket option otherwise return true; |
| 510 |
bool EnableNagleAlgoritm(); |
| 511 |
|
| 512 |
|
| 513 |
protected: |
| 514 |
/// Set internal socket error to that specified error |
| 515 |
/// @param error type of error |
| 516 |
void SetSocketError(CSimpleSocket::CSocketError error) { |
| 517 |
m_socketErrno = error; |
| 518 |
}; |
| 519 |
|
| 520 |
/// Set object socket handle to that specified as parameter |
| 521 |
/// @param socket value of socket descriptor |
| 522 |
void SetSocketHandle(SOCKET socket) { |
| 523 |
m_socket = socket; |
| 524 |
}; |
| 525 |
|
| 526 |
private: |
| 527 |
/// Generic function used to get the send/receive window size |
| 528 |
/// @return zero on failure else the number of bytes of the TCP window size if successful. |
| 529 |
uint32 GetWindowSize(uint32 nOptionName); |
| 530 |
|
| 531 |
/// Generic function used to set the send/receive window size |
| 532 |
/// @return zero on failure else the number of bytes of the TCP window size if successful. |
| 533 |
uint32 SetWindowSize(uint32 nOptionName, uint32 nWindowSize); |
| 534 |
|
| 535 |
|
| 536 |
/// Attempts to send at most nNumItem blocks described by sendVector |
| 537 |
/// to the socket descriptor associated with the socket object. |
| 538 |
/// @param sendVector pointer to an array of iovec structures |
| 539 |
/// @param nNumItems number of items in the vector to process |
| 540 |
/// <br>\b Note: This implementation is for systems that don't natively |
| 541 |
/// support this functionality. |
| 542 |
/// @return number of bytes actually sent, return of zero means the |
| 543 |
/// connection has been shutdown on the other side, and a return of -1 |
| 544 |
/// means that an error has occurred. |
| 545 |
int32 Writev(const struct iovec *pVector, size_t nCount); |
| 546 |
|
| 547 |
/// Flush the socket descriptor owned by the object. |
| 548 |
/// @return true data was successfully sent, else return false; |
| 549 |
bool Flush(); |
| 550 |
|
| 551 |
CSimpleSocket *operator=(CSimpleSocket &socket); |
| 552 |
|
| 553 |
protected: |
| 554 |
SOCKET m_socket; /// socket handle |
| 555 |
CSocketError m_socketErrno; /// number of last error |
| 556 |
uint8 *m_pBuffer; /// internal send/receive buffer |
| 557 |
int32 m_nBufferSize; /// size of internal send/receive buffer |
| 558 |
int32 m_nSocketDomain; /// socket type PF_INET, PF_INET6 |
| 559 |
CSocketType m_nSocketType; /// socket type - UDP, TCP or RAW |
| 560 |
int32 m_nBytesReceived; /// number of bytes received |
| 561 |
int32 m_nBytesSent; /// number of bytes sent |
| 562 |
uint32 m_nFlags; /// socket flags |
| 563 |
bool m_bIsBlocking; /// is socket blocking |
| 564 |
bool m_bIsMulticast; /// is the UDP socket multicast; |
| 565 |
struct timeval m_stConnectTimeout; /// connection timeout |
| 566 |
struct timeval m_stRecvTimeout; /// receive timeout |
| 567 |
struct timeval m_stSendTimeout; /// send timeout |
| 568 |
struct sockaddr_in m_stServerSockaddr; /// server address |
| 569 |
struct sockaddr_in m_stClientSockaddr; /// client address |
| 570 |
struct sockaddr_in m_stMulticastGroup; /// multicast group to bind to |
| 571 |
struct linger m_stLinger; /// linger flag |
| 572 |
CStatTimer m_timer; /// internal statistics. |
| 573 |
#ifdef WIN32 |
| 574 |
WSADATA m_hWSAData; /// Windows |
| 575 |
#endif |
| 576 |
fd_set m_writeFds; /// write file descriptor set |
| 577 |
fd_set m_readFds; /// read file descriptor set |
| 578 |
fd_set m_errorFds; /// error file descriptor set |
| 579 |
}; |
| 580 |
|
| 581 |
|
| 582 |
#endif /* __SOCKET_H__ */ |
| 583 |
|