summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPetr Mrázek2012-03-11 15:00:38 (GMT)
committerPetr Mrázek2012-03-11 15:00:38 (GMT)
commit9f3026df545efeccb9bf33fe56f47bdee4c06e4a (patch)
treef01ed18171054d77ce41bd9fa827806561458025
parentf8a2b0977253079b66d4ec14d5fceaa8458d11d6 (diff)
downloadclsocket-9f3026df545efeccb9bf33fe56f47bdee4c06e4a.tar.gz
clsocket-9f3026df545efeccb9bf33fe56f47bdee4c06e4a.tar.bz2
clsocket-9f3026df545efeccb9bf33fe56f47bdee4c06e4a.tar.xz
Unify whitespace and formatting
-rw-r--r--src/ActiveSocket.cpp68
-rw-r--r--src/ActiveSocket.h26
-rwxr-xr-xsrc/Host.h2
-rw-r--r--src/PassiveSocket.cpp84
-rw-r--r--src/PassiveSocket.h42
-rwxr-xr-xsrc/SimpleSocket.cpp640
-rwxr-xr-xsrc/SimpleSocket.h352
7 files changed, 638 insertions, 576 deletions
diff --git a/src/ActiveSocket.cpp b/src/ActiveSocket.cpp
index 6da25e8..e68b758 100644
--- a/src/ActiveSocket.cpp
+++ b/src/ActiveSocket.cpp
@@ -58,12 +58,12 @@ bool CActiveSocket::ConnectTCP(const uint8 *pAddr, int16 nPort)
struct in_addr stIpAddress;
//------------------------------------------------------------------
- // Preconnection setup that must be preformed
+ // Preconnection setup that must be preformed
//------------------------------------------------------------------
memset(&m_stServerSockaddr, 0, sizeof(m_stServerSockaddr));
m_stServerSockaddr.sin_family = AF_INET;
- if ((m_pHE = GETHOSTBYNAME(pAddr)) == NULL)
+ if ((m_pHE = GETHOSTBYNAME(pAddr)) == NULL)
{
#ifdef WIN32
TranslateSocketError();
@@ -88,30 +88,30 @@ bool CActiveSocket::ConnectTCP(const uint8 *pAddr, int16 nPort)
m_stServerSockaddr.sin_port = htons(nPort);
//------------------------------------------------------------------
- // Connect to address "xxx.xxx.xxx.xxx" (IPv4) address only.
- //
+ // Connect to address "xxx.xxx.xxx.xxx" (IPv4) address only.
+ //
//------------------------------------------------------------------
m_timer.Initialize();
m_timer.SetStartTime();
- if (connect(m_socket, (struct sockaddr*)&m_stServerSockaddr, sizeof(m_stServerSockaddr)) ==
- CSimpleSocket::SocketError)
+ if (connect(m_socket, (struct sockaddr*)&m_stServerSockaddr, sizeof(m_stServerSockaddr)) ==
+ CSimpleSocket::SocketError)
{
//--------------------------------------------------------------
- // Get error value this might be a non-blocking socket so we
+ // Get error value this might be a non-blocking socket so we
// must first check.
//--------------------------------------------------------------
TranslateSocketError();
//--------------------------------------------------------------
// If the socket is non-blocking and the current socket error
- // is SocketEinprogress or SocketEwouldblock then poll connection
+ // is SocketEinprogress or SocketEwouldblock then poll connection
// with select for designated timeout period.
// Linux returns EINPROGRESS and Windows returns WSAEWOULDBLOCK.
//--------------------------------------------------------------
- if ((IsNonblocking()) &&
- ((GetSocketError() == CSimpleSocket::SocketEwouldblock) ||
- (GetSocketError() == CSimpleSocket::SocketEinprogress)))
+ if ((IsNonblocking()) &&
+ ((GetSocketError() == CSimpleSocket::SocketEwouldblock) ||
+ (GetSocketError() == CSimpleSocket::SocketEinprogress)))
{
bRetVal = Select(GetConnectTimeoutSec(), GetConnectTimeoutUSec());
}
@@ -139,12 +139,12 @@ bool CActiveSocket::ConnectUDP(const uint8 *pAddr, int16 nPort)
struct in_addr stIpAddress;
//------------------------------------------------------------------
- // Pre-connection setup that must be preformed
+ // Pre-connection setup that must be preformed
//------------------------------------------------------------------
memset(&m_stServerSockaddr, 0, sizeof(m_stServerSockaddr));
m_stServerSockaddr.sin_family = AF_INET;
- if ((m_pHE = GETHOSTBYNAME(pAddr)) == NULL)
+ if ((m_pHE = GETHOSTBYNAME(pAddr)) == NULL)
{
#ifdef WIN32
TranslateSocketError();
@@ -169,8 +169,8 @@ bool CActiveSocket::ConnectUDP(const uint8 *pAddr, int16 nPort)
m_stServerSockaddr.sin_port = htons(nPort);
//------------------------------------------------------------------
- // Connect to address "xxx.xxx.xxx.xxx" (IPv4) address only.
- //
+ // Connect to address "xxx.xxx.xxx.xxx" (IPv4) address only.
+ //
//------------------------------------------------------------------
m_timer.Initialize();
m_timer.SetStartTime();
@@ -198,12 +198,12 @@ bool CActiveSocket::ConnectRAW(const uint8 *pAddr, int16 nPort)
bool bRetVal = false;
struct in_addr stIpAddress;
//------------------------------------------------------------------
- // Pre-connection setup that must be preformed
+ // Pre-connection setup that must be preformed
//------------------------------------------------------------------
memset(&m_stServerSockaddr, 0, sizeof(m_stServerSockaddr));
m_stServerSockaddr.sin_family = AF_INET;
- if ((m_pHE = GETHOSTBYNAME(pAddr)) == NULL)
+ if ((m_pHE = GETHOSTBYNAME(pAddr)) == NULL)
{
#ifdef WIN32
TranslateSocketError();
@@ -228,8 +228,8 @@ bool CActiveSocket::ConnectRAW(const uint8 *pAddr, int16 nPort)
m_stServerSockaddr.sin_port = htons(nPort);
//------------------------------------------------------------------
- // Connect to address "xxx.xxx.xxx.xxx" (IPv4) address only.
- //
+ // Connect to address "xxx.xxx.xxx.xxx" (IPv4) address only.
+ //
//------------------------------------------------------------------
m_timer.Initialize();
m_timer.SetStartTime();
@@ -277,24 +277,24 @@ bool CActiveSocket::Open(const uint8 *pAddr, int16 nPort)
switch (m_nSocketType)
{
- case CSimpleSocket::SocketTypeTcp :
- {
- bRetVal = ConnectTCP(pAddr, nPort);
- break;
- }
- case CSimpleSocket::SocketTypeUdp :
- {
- bRetVal = ConnectUDP(pAddr, nPort);
- break;
- }
- case CSimpleSocket::SocketTypeRaw :
- break;
- default:
- break;
+ case CSimpleSocket::SocketTypeTcp :
+ {
+ bRetVal = ConnectTCP(pAddr, nPort);
+ break;
+ }
+ case CSimpleSocket::SocketTypeUdp :
+ {
+ bRetVal = ConnectUDP(pAddr, nPort);
+ break;
+ }
+ case CSimpleSocket::SocketTypeRaw :
+ break;
+ default:
+ break;
}
//--------------------------------------------------------------------------
- // If successful then create a local copy of the address and port
+ // If successful then create a local copy of the address and port
//--------------------------------------------------------------------------
if (bRetVal)
{
diff --git a/src/ActiveSocket.h b/src/ActiveSocket.h
index c38fa5b..08f0982 100644
--- a/src/ActiveSocket.h
+++ b/src/ActiveSocket.h
@@ -48,40 +48,42 @@
class CPassiveSocket;
/// Provides a platform independent class to create an active socket.
-/// An active socket is used to create a socket which connects to a server.
+/// An active socket is used to create a socket which connects to a server.
/// This type of object would be used when an application needs to send/receive
-/// data from a server.
+/// data from a server.
class CActiveSocket : public CSimpleSocket {
public:
friend class CPassiveSocket;
CActiveSocket(CSocketType type = SocketTypeTcp);
- virtual ~CActiveSocket() { Close(); };
+ virtual ~CActiveSocket() {
+ Close();
+ };
- /// Established a connection to the address specified by pAddr.
- /// Connection-based protocol sockets (CSocket::SocketTypeTcp) may
- /// successfully call Open() only once, however; connectionless protocol
- /// sockets (CSocket::SocketTypeUdp) may use Open() multiple times to
- /// change their association.
+ /// Established a connection to the address specified by pAddr.
+ /// Connection-based protocol sockets (CSocket::SocketTypeTcp) may
+ /// successfully call Open() only once, however; connectionless protocol
+ /// sockets (CSocket::SocketTypeUdp) may use Open() multiple times to
+ /// change their association.
/// @param pAddr specifies the destination address to connect.
/// @param nPort specifies the destination port.
/// @return true if successful connection made, otherwise false.
virtual bool Open(const uint8 *pAddr, int16 nPort);
private:
- /// Utility function used to create a TCP connection, called from Open().
+ /// Utility function used to create a TCP connection, called from Open().
/// @return true if successful connection made, otherwise false.
bool ConnectTCP(const uint8 *pAddr, int16 nPort);
- /// Utility function used to create a UDP connection, called from Open().
+ /// Utility function used to create a UDP connection, called from Open().
/// @return true if successful connection made, otherwise false.
bool ConnectUDP(const uint8 *pAddr, int16 nPort);
- /// Utility function used to create a RAW connection, called from Open().
+ /// Utility function used to create a RAW connection, called from Open().
/// @return true if successful connection made, otherwise false.
bool ConnectRAW(const uint8 *pAddr, int16 nPort);
- private:
+private:
struct hostent *m_pHE;
};
diff --git a/src/Host.h b/src/Host.h
index b24d225..60d7523 100755
--- a/src/Host.h
+++ b/src/Host.h
@@ -249,3 +249,5 @@ extern "C"
#endif /* __HOST_H__ */
+
+
diff --git a/src/PassiveSocket.cpp b/src/PassiveSocket.cpp
index 1f94390..eb89dbe 100644
--- a/src/PassiveSocket.cpp
+++ b/src/PassiveSocket.cpp
@@ -1,6 +1,6 @@
/*---------------------------------------------------------------------------*/
/* */
-/* PassiveSocket.cpp - Passive Socket Implementation */
+/* PassiveSocket.cpp - Passive Socket Implementation */
/* */
/* Author : Mark Carrier (mark@carrierlabs.com) */
/* */
@@ -68,9 +68,9 @@ bool CPassiveSocket::BindMulticast(const uint8 *pInterface, const uint8 *pGroup,
memset(&m_stMulticastGroup,0,sizeof(m_stMulticastGroup));
m_stMulticastGroup.sin_family = AF_INET;
m_stMulticastGroup.sin_port = htons(nPort);
-
+
//--------------------------------------------------------------------------
- // If no IP Address (interface ethn) is supplied, or the loop back is
+ // If no IP Address (interface ethn) is supplied, or the loop back is
// specified then bind to any interface, else bind to specified interface.
//--------------------------------------------------------------------------
if ((pInterface == NULL) || (!strlen((const char *)pInterface)))
@@ -84,9 +84,9 @@ bool CPassiveSocket::BindMulticast(const uint8 *pInterface, const uint8 *pGroup,
m_stMulticastGroup.sin_addr.s_addr = inAddr;
}
}
-
+
//--------------------------------------------------------------------------
- // Bind to the specified port
+ // Bind to the specified port
//--------------------------------------------------------------------------
if (bind(m_socket, (struct sockaddr *)&m_stMulticastGroup, sizeof(m_stMulticastGroup)) == 0)
{
@@ -96,7 +96,7 @@ bool CPassiveSocket::BindMulticast(const uint8 *pInterface, const uint8 *pGroup,
m_stMulticastRequest.imr_multiaddr.s_addr = inet_addr((const char *)pGroup);
m_stMulticastRequest.imr_interface.s_addr = m_stMulticastGroup.sin_addr.s_addr;
- if (SETSOCKOPT(m_socket, IPPROTO_IP, IP_ADD_MEMBERSHIP,
+ if (SETSOCKOPT(m_socket, IPPROTO_IP, IP_ADD_MEMBERSHIP,
(void *)&m_stMulticastRequest,
sizeof(m_stMulticastRequest)) == CSimpleSocket::SocketSuccess)
{
@@ -111,7 +111,7 @@ bool CPassiveSocket::BindMulticast(const uint8 *pInterface, const uint8 *pGroup,
//--------------------------------------------------------------------------
- // If there was a socket error then close the socket to clean out the
+ // If there was a socket error then close the socket to clean out the
// connection in the backlog.
//--------------------------------------------------------------------------
TranslateSocketError();
@@ -128,12 +128,12 @@ bool CPassiveSocket::BindMulticast(const uint8 *pInterface, const uint8 *pGroup,
//------------------------------------------------------------------------------
//
-// Listen() -
+// Listen() -
//
//------------------------------------------------------------------------------
bool CPassiveSocket::Listen(const uint8 *pAddr, int16 nPort, int32 nConnectionBacklog)
{
- bool bRetVal = false;
+ bool bRetVal = false;
#ifdef WIN32
ULONG inAddr;
#else
@@ -156,9 +156,9 @@ bool CPassiveSocket::Listen(const uint8 *pAddr, int16 nPort, int32 nConnectionBa
memset(&m_stServerSockaddr,0,sizeof(m_stServerSockaddr));
m_stServerSockaddr.sin_family = AF_INET;
m_stServerSockaddr.sin_port = htons(nPort);
-
+
//--------------------------------------------------------------------------
- // If no IP Address (interface ethn) is supplied, or the loop back is
+ // If no IP Address (interface ethn) is supplied, or the loop back is
// specified then bind to any interface, else bind to specified interface.
//--------------------------------------------------------------------------
if ((pAddr == NULL) || (!strlen((const char *)pAddr)))
@@ -172,12 +172,12 @@ bool CPassiveSocket::Listen(const uint8 *pAddr, int16 nPort, int32 nConnectionBa
m_stServerSockaddr.sin_addr.s_addr = inAddr;
}
}
-
+
m_timer.Initialize();
m_timer.SetStartTime();
//--------------------------------------------------------------------------
- // Bind to the specified port
+ // Bind to the specified port
//--------------------------------------------------------------------------
if (bind(m_socket, (struct sockaddr *)&m_stServerSockaddr, sizeof(m_stServerSockaddr)) != CSimpleSocket::SocketError)
{
@@ -187,8 +187,8 @@ bool CPassiveSocket::Listen(const uint8 *pAddr, int16 nPort, int32 nConnectionBa
{
bRetVal = true;
}
- }
- else
+ }
+ else
{
bRetVal = true;
}
@@ -197,7 +197,7 @@ bool CPassiveSocket::Listen(const uint8 *pAddr, int16 nPort, int32 nConnectionBa
m_timer.SetEndTime();
//--------------------------------------------------------------------------
- // If there was a socket error then close the socket to clean out the
+ // If there was a socket error then close the socket to clean out the
// connection in the backlog.
//--------------------------------------------------------------------------
TranslateSocketError();
@@ -213,7 +213,7 @@ bool CPassiveSocket::Listen(const uint8 *pAddr, int16 nPort, int32 nConnectionBa
//------------------------------------------------------------------------------
//
-// Accept() -
+// Accept() -
//
//------------------------------------------------------------------------------
CActiveSocket *CPassiveSocket::Accept()
@@ -239,9 +239,9 @@ CActiveSocket *CPassiveSocket::Accept()
m_timer.Initialize();
m_timer.SetStartTime();
-
+
nSockLen = sizeof(m_stClientSockaddr);
-
+
do
{
errno = 0;
@@ -271,9 +271,9 @@ CActiveSocket *CPassiveSocket::Accept()
}
} while (socketErrno == CSimpleSocket::SocketInterrupted);
-
+
m_timer.SetEndTime();
-
+
if (socketErrno != CSimpleSocket::SocketSuccess)
{
delete pClientSocket;
@@ -297,35 +297,35 @@ int32 CPassiveSocket::Send(const uint8 *pBuf, size_t bytesToSend)
switch(m_nSocketType)
{
- case CSimpleSocket::SocketTypeUdp:
+ case CSimpleSocket::SocketTypeUdp:
+ {
+ if (IsSocketValid())
{
- if (IsSocketValid())
+ if ((bytesToSend > 0) && (pBuf != NULL))
{
- if ((bytesToSend > 0) && (pBuf != NULL))
- {
- m_timer.Initialize();
- m_timer.SetStartTime();
+ m_timer.Initialize();
+ m_timer.SetStartTime();
- m_nBytesSent = SENDTO(m_socket, pBuf, bytesToSend, 0,
- (const sockaddr *)&m_stClientSockaddr,
- sizeof(m_stClientSockaddr));
+ m_nBytesSent = SENDTO(m_socket, pBuf, bytesToSend, 0,
+ (const sockaddr *)&m_stClientSockaddr,
+ sizeof(m_stClientSockaddr));
- m_timer.SetEndTime();
+ m_timer.SetEndTime();
- if (m_nBytesSent == CSimpleSocket::SocketError)
- {
- TranslateSocketError();
- }
+ if (m_nBytesSent == CSimpleSocket::SocketError)
+ {
+ TranslateSocketError();
}
}
- break;
}
- case CSimpleSocket::SocketTypeTcp:
- CSimpleSocket::Send(pBuf, bytesToSend);
- break;
- default:
- SetSocketError(SocketProtocolError);
- break;
+ break;
+ }
+ case CSimpleSocket::SocketTypeTcp:
+ CSimpleSocket::Send(pBuf, bytesToSend);
+ break;
+ default:
+ SetSocketError(SocketProtocolError);
+ break;
}
return m_nBytesSent;
diff --git a/src/PassiveSocket.h b/src/PassiveSocket.h
index b26f284..163d369 100644
--- a/src/PassiveSocket.h
+++ b/src/PassiveSocket.h
@@ -46,51 +46,53 @@
/// Provides a platform independent class to create a passive socket.
/// A passive socket is used to create a "listening" socket. This type
-/// of object would be used when an application needs to wait for
-/// inbound connections. Support for CSimpleSocket::SocketTypeTcp,
+/// of object would be used when an application needs to wait for
+/// inbound connections. Support for CSimpleSocket::SocketTypeTcp,
/// CSimpleSocket::SocketTypeUdp, and CSimpleSocket::SocketTypeRaw is handled
-/// in a similar fashion. The big difference is that the method
+/// in a similar fashion. The big difference is that the method
/// CPassiveSocket::Accept should not be called on the latter two socket
-/// types.
+/// types.
class CPassiveSocket : public CSimpleSocket {
public:
CPassiveSocket(CSocketType type = SocketTypeTcp);
- virtual ~CPassiveSocket() { Close(); };
+ virtual ~CPassiveSocket() {
+ Close();
+ };
- /// Extracts the first connection request on the queue of pending
- /// connections and creates a newly connected socket. Used with
+ /// Extracts the first connection request on the queue of pending
+ /// connections and creates a newly connected socket. Used with
/// CSocketType CSimpleSocket::SocketTypeTcp. It is the responsibility of
/// the caller to delete the returned object when finished.
/// @return if successful a pointer to a newly created CActiveSocket object
- /// will be returned and the internal error condition of the CPassiveSocket
+ /// will be returned and the internal error condition of the CPassiveSocket
/// object will be CPassiveSocket::SocketSuccess. If an error condition was encountered
/// the NULL will be returned and one of the following error conditions will be set:
- /// CPassiveSocket::SocketEwouldblock, CPassiveSocket::SocketInvalidSocket,
+ /// CPassiveSocket::SocketEwouldblock, CPassiveSocket::SocketInvalidSocket,
/// CPassiveSocket::SocketConnectionAborted, CPassiveSocket::SocketInterrupted
/// CPassiveSocket::SocketProtocolError, CPassiveSocket::SocketFirewallError
virtual CActiveSocket *Accept(void);
/// Bind to a multicast group on a specified interface, multicast group, and port
- ///
+ ///
/// @param pInterface - interface on which to bind.
/// @param pGroup - multicast group address to bind.
- /// @param nPort - port on which multicast
+ /// @param nPort - port on which multicast
/// @return true if able to bind to interface and multicast group.
/// If not successful, the false is returned and one of the following error
- /// condiitions will be set: CPassiveSocket::SocketAddressInUse, CPassiveSocket::SocketProtocolError,
+ /// condiitions will be set: CPassiveSocket::SocketAddressInUse, CPassiveSocket::SocketProtocolError,
/// CPassiveSocket::SocketInvalidSocket. The following socket errors are for Linux/Unix
/// derived systems only: CPassiveSocket::SocketInvalidSocketBuffer
bool BindMulticast(const uint8 *pInterface, const uint8 *pGroup, int16 nPort);
/// Create a listening socket at local ip address 'x.x.x.x' or 'localhost'
/// if pAddr is NULL on port nPort.
- ///
+ ///
/// @param pAddr specifies the IP address on which to listen.
/// @param nPort specifies the port on which to listen.
/// @param nConnectionBacklog specifies connection queue backlog (default 30,000)
- /// @return true if a listening socket was created.
+ /// @return true if a listening socket was created.
/// If not successful, the false is returned and one of the following error
- /// condiitions will be set: CPassiveSocket::SocketAddressInUse, CPassiveSocket::SocketProtocolError,
+ /// condiitions will be set: CPassiveSocket::SocketAddressInUse, CPassiveSocket::SocketProtocolError,
/// CPassiveSocket::SocketInvalidSocket. The following socket errors are for Linux/Unix
/// derived systems only: CPassiveSocket::SocketInvalidSocketBuffer
virtual bool Listen(const uint8 *pAddr, int16 nPort, int32 nConnectionBacklog = 30000);
@@ -102,14 +104,14 @@ public:
/// connection has been shutdown on the other side, and a return of -1
/// means that an error has occurred. If an error was signaled then one
/// of the following error codes will be set: CPassiveSocket::SocketInvalidSocket,
- /// CPassiveSocket::SocketEwouldblock, SimpleSocket::SocketConnectionReset,
- /// CPassiveSocket::SocketInvalidSocketBuffer, CPassiveSocket::SocketInterrupted,
+ /// CPassiveSocket::SocketEwouldblock, SimpleSocket::SocketConnectionReset,
+ /// CPassiveSocket::SocketInvalidSocketBuffer, CPassiveSocket::SocketInterrupted,
/// CPassiveSocket::SocketProtocolError, CPassiveSocket::SocketNotconnected
- /// <br>\b Note: This function is used only for a socket of type
- /// CSimpleSocket::SocketTypeUdp
+ /// <br>\b Note: This function is used only for a socket of type
+ /// CSimpleSocket::SocketTypeUdp
virtual int32 Send(const uint8 *pBuf, size_t bytesToSend);
- private:
+private:
struct ip_mreq m_stMulticastRequest; /// group address for multicast
};
diff --git a/src/SimpleSocket.cpp b/src/SimpleSocket.cpp
index 55d982e..aa42ec1 100755
--- a/src/SimpleSocket.cpp
+++ b/src/SimpleSocket.cpp
@@ -43,11 +43,11 @@
#include "SimpleSocket.h"
CSimpleSocket::CSimpleSocket(CSocketType nType) :
- m_socket(INVALID_SOCKET),
- m_socketErrno(CSimpleSocket::SocketInvalidSocket),
- m_pBuffer(NULL), m_nBufferSize(0), m_nSocketDomain(AF_INET),
- m_nSocketType(SocketTypeInvalid), m_nBytesReceived(-1),
- m_nBytesSent(-1), m_nFlags(0),
+ m_socket(INVALID_SOCKET),
+ m_socketErrno(CSimpleSocket::SocketInvalidSocket),
+ m_pBuffer(NULL), m_nBufferSize(0), m_nSocketDomain(AF_INET),
+ m_nSocketType(SocketTypeInvalid), m_nBytesReceived(-1),
+ m_nBytesSent(-1), m_nFlags(0),
m_bIsBlocking(true)
{
SetConnectTimeout(1, 0);
@@ -57,74 +57,74 @@ CSimpleSocket::CSimpleSocket(CSocketType nType) :
switch(nType)
{
- //----------------------------------------------------------------------
- // Declare socket type stream - TCP
- //----------------------------------------------------------------------
- case CSimpleSocket::SocketTypeTcp:
- {
- m_nSocketDomain = AF_INET;
- m_nSocketType = CSimpleSocket::SocketTypeTcp;
- break;
- }
- case CSimpleSocket::SocketTypeTcp6:
- {
- m_nSocketDomain = AF_INET6;
- m_nSocketType = CSimpleSocket::SocketTypeTcp6;
- break;
- }
- //----------------------------------------------------------------------
- // Declare socket type datagram - UDP
- //----------------------------------------------------------------------
+ //----------------------------------------------------------------------
+ // Declare socket type stream - TCP
+ //----------------------------------------------------------------------
+ case CSimpleSocket::SocketTypeTcp:
+ {
+ m_nSocketDomain = AF_INET;
+ m_nSocketType = CSimpleSocket::SocketTypeTcp;
+ break;
+ }
+ case CSimpleSocket::SocketTypeTcp6:
+ {
+ m_nSocketDomain = AF_INET6;
+ m_nSocketType = CSimpleSocket::SocketTypeTcp6;
+ break;
+ }
+ //----------------------------------------------------------------------
+ // Declare socket type datagram - UDP
+ //----------------------------------------------------------------------
case CSimpleSocket::SocketTypeUdp:
- {
- m_nSocketDomain = AF_INET;
- m_nSocketType = CSimpleSocket::SocketTypeUdp;
- break;
- }
+ {
+ m_nSocketDomain = AF_INET;
+ m_nSocketType = CSimpleSocket::SocketTypeUdp;
+ break;
+ }
case CSimpleSocket::SocketTypeUdp6:
- {
- m_nSocketDomain = AF_INET6;
- m_nSocketType = CSimpleSocket::SocketTypeUdp6;
- break;
- }
- //----------------------------------------------------------------------
- // Declare socket type raw Ethernet - Ethernet
- //----------------------------------------------------------------------
- case CSimpleSocket::SocketTypeRaw:
- {
+ {
+ m_nSocketDomain = AF_INET6;
+ m_nSocketType = CSimpleSocket::SocketTypeUdp6;
+ break;
+ }
+ //----------------------------------------------------------------------
+ // Declare socket type raw Ethernet - Ethernet
+ //----------------------------------------------------------------------
+ case CSimpleSocket::SocketTypeRaw:
+ {
#ifdef _LINUX
- m_nSocketDomain = AF_PACKET;
- m_nSocketType = CSimpleSocket::SocketTypeRaw;
+ m_nSocketDomain = AF_PACKET;
+ m_nSocketType = CSimpleSocket::SocketTypeRaw;
#endif
#ifdef WIN32
- m_nSocketType = CSimpleSocket::SocketTypeInvalid;
+ m_nSocketType = CSimpleSocket::SocketTypeInvalid;
#endif
- break;
- }
- default:
- m_nSocketType = CSimpleSocket::SocketTypeInvalid;
- break;
+ break;
+ }
+ default:
+ m_nSocketType = CSimpleSocket::SocketTypeInvalid;
+ break;
}
}
CSimpleSocket::CSimpleSocket(CSimpleSocket &socket)
{
m_pBuffer = new uint8[socket.m_nBufferSize];
- m_nBufferSize = socket.m_nBufferSize;
- memcpy(m_pBuffer, socket.m_pBuffer, socket.m_nBufferSize);
+ m_nBufferSize = socket.m_nBufferSize;
+ memcpy(m_pBuffer, socket.m_pBuffer, socket.m_nBufferSize);
}
CSimpleSocket *CSimpleSocket::operator=(CSimpleSocket &socket)
{
- if (m_nBufferSize != socket.m_nBufferSize)
- {
- delete m_pBuffer;
- m_pBuffer = new uint8[socket.m_nBufferSize];
- m_nBufferSize = socket.m_nBufferSize;
- memcpy(m_pBuffer, socket.m_pBuffer, socket.m_nBufferSize);
- }
-
- return this;
+ if (m_nBufferSize != socket.m_nBufferSize)
+ {
+ delete m_pBuffer;
+ m_pBuffer = new uint8[socket.m_nBufferSize];
+ m_nBufferSize = socket.m_nBufferSize;
+ memcpy(m_pBuffer, socket.m_pBuffer, socket.m_nBufferSize);
+ }
+
+ return this;
}
@@ -140,14 +140,14 @@ bool CSimpleSocket::Initialize()
#ifdef WIN32
//-------------------------------------------------------------------------
- // Data structure containing general Windows Sockets Info
+ // Data structure containing general Windows Sockets Info
//-------------------------------------------------------------------------
memset(&m_hWSAData, 0, sizeof(m_hWSAData));
WSAStartup(MAKEWORD(2, 0), &m_hWSAData);
#endif
//-------------------------------------------------------------------------
- // Create the basic Socket Handle
+ // Create the basic Socket Handle
//-------------------------------------------------------------------------
m_timer.Initialize();
m_timer.SetStartTime();
@@ -169,22 +169,22 @@ bool CSimpleSocket::Initialize()
bool CSimpleSocket::BindInterface(uint8 *pInterface)
{
bool bRetVal = false;
- struct in_addr stInterfaceAddr;
+ struct in_addr stInterfaceAddr;
if (GetMulticast() == true)
{
if (pInterface)
{
- stInterfaceAddr.s_addr= inet_addr((const char *)pInterface);
+ stInterfaceAddr.s_addr= inet_addr((const char *)pInterface);
if (SETSOCKOPT(m_socket, IPPROTO_IP, IP_MULTICAST_IF, &stInterfaceAddr, sizeof(stInterfaceAddr)) == SocketSuccess)
{
bRetVal = true;
}
}
- }
+ }
else
{
- SetSocketError(CSimpleSocket::SocketProtocolError);
+ SetSocketError(CSimpleSocket::SocketProtocolError);
}
return bRetVal;
@@ -225,7 +225,7 @@ bool CSimpleSocket::SetMulticast(bool bEnable, uint8 multicastTTL)
//------------------------------------------------------------------------------
//
-// SetSocketDscp()
+// SetSocketDscp()
//
//------------------------------------------------------------------------------
bool CSimpleSocket::SetSocketDscp(int32 nDscp)
@@ -251,13 +251,13 @@ bool CSimpleSocket::SetSocketDscp(int32 nDscp)
//------------------------------------------------------------------------------
//
-// GetSocketDscp()
+// GetSocketDscp()
//
//------------------------------------------------------------------------------
int32 CSimpleSocket::GetSocketDscp(void)
{
int32 nTempVal = 0;
- socklen_t nLen = 0;
+ socklen_t nLen = 0;
if (IsSocketValid())
{
@@ -276,7 +276,7 @@ int32 CSimpleSocket::GetSocketDscp(void)
//------------------------------------------------------------------------------
//
-// GetWindowSize()
+// GetWindowSize()
//
//------------------------------------------------------------------------------
uint16 CSimpleSocket::GetWindowSize(uint32 nOptionName)
@@ -291,7 +291,7 @@ uint16 CSimpleSocket::GetWindowSize(uint32 nOptionName)
socklen_t nLen = sizeof(nTcpWinSize);
//---------------------------------------------------------------------
- // query for buffer size
+ // query for buffer size
//---------------------------------------------------------------------
GETSOCKOPT(m_socket, SOL_SOCKET, nOptionName, &nTcpWinSize, &nLen);
TranslateSocketError();
@@ -392,68 +392,68 @@ int32 CSimpleSocket::Send(const uint8 *pBuf, size_t bytesToSend)
switch(m_nSocketType)
{
- case CSimpleSocket::SocketTypeTcp:
+ case CSimpleSocket::SocketTypeTcp:
+ {
+ if (IsSocketValid())
{
- if (IsSocketValid())
+ if ((bytesToSend > 0) && (pBuf != NULL))
{
- if ((bytesToSend > 0) && (pBuf != NULL))
+ m_timer.Initialize();
+ m_timer.SetStartTime();
+
+ //---------------------------------------------------------
+ // Check error condition and attempt to resend if call
+ // was interrupted by a signal.
+ //---------------------------------------------------------
+ do
{
- m_timer.Initialize();
- m_timer.SetStartTime();
-
- //---------------------------------------------------------
- // Check error condition and attempt to resend if call
- // was interrupted by a signal.
- //---------------------------------------------------------
- do
- {
- m_nBytesSent = SEND(m_socket, pBuf, bytesToSend, 0);
- TranslateSocketError();
- } while (GetSocketError() == CSimpleSocket::SocketInterrupted);
+ m_nBytesSent = SEND(m_socket, pBuf, bytesToSend, 0);
+ TranslateSocketError();
+ } while (GetSocketError() == CSimpleSocket::SocketInterrupted);
- m_timer.SetEndTime();
- }
+ m_timer.SetEndTime();
}
- break;
}
- case CSimpleSocket::SocketTypeUdp:
+ break;
+ }
+ case CSimpleSocket::SocketTypeUdp:
+ {
+ if (IsSocketValid())
{
- if (IsSocketValid())
+ if ((bytesToSend > 0) && (pBuf != NULL))
{
- if ((bytesToSend > 0) && (pBuf != NULL))
+ m_timer.Initialize();
+ m_timer.SetStartTime();
+
+ //---------------------------------------------------------
+ // Check error condition and attempt to resend if call
+ // was interrupted by a signal.
+ //---------------------------------------------------------
+ // if (GetMulticast())
+ // {
+ // do
+ // {
+ // m_nBytesSent = SENDTO(m_socket, pBuf, bytesToSend, 0, (const sockaddr *)&m_stMulticastGroup,
+ // sizeof(m_stMulticastGroup));
+ // TranslateSocketError();
+ // } while (GetSocketError() == CSimpleSocket::SocketInterrupted);
+ // }
+ // else
{
- m_timer.Initialize();
- m_timer.SetStartTime();
-
- //---------------------------------------------------------
- // Check error condition and attempt to resend if call
- // was interrupted by a signal.
- //---------------------------------------------------------
- // if (GetMulticast())
- // {
- // do
- // {
- // m_nBytesSent = SENDTO(m_socket, pBuf, bytesToSend, 0, (const sockaddr *)&m_stMulticastGroup,
- // sizeof(m_stMulticastGroup));
- // TranslateSocketError();
- // } while (GetSocketError() == CSimpleSocket::SocketInterrupted);
- // }
- // else
+ do
{
- do
- {
- m_nBytesSent = SENDTO(m_socket, pBuf, bytesToSend, 0, (const sockaddr *)&m_stServerSockaddr, sizeof(m_stServerSockaddr));
- TranslateSocketError();
- } while (GetSocketError() == CSimpleSocket::SocketInterrupted);
- }
-
- m_timer.SetEndTime();
+ m_nBytesSent = SENDTO(m_socket, pBuf, bytesToSend, 0, (const sockaddr *)&m_stServerSockaddr, sizeof(m_stServerSockaddr));
+ TranslateSocketError();
+ } while (GetSocketError() == CSimpleSocket::SocketInterrupted);
}
+
+ m_timer.SetEndTime();
}
- break;
}
- default:
- break;
+ break;
+ }
+ default:
+ break;
}
return m_nBytesSent;
@@ -467,32 +467,32 @@ int32 CSimpleSocket::Send(const uint8 *pBuf, size_t bytesToSend)
//------------------------------------------------------------------------------
bool CSimpleSocket::Close(void)
{
- bool bRetVal = false;
-
- //--------------------------------------------------------------------------
- // delete internal buffer
- //--------------------------------------------------------------------------
- if (m_pBuffer != NULL)
- {
- delete [] m_pBuffer;
- m_pBuffer = NULL;
- }
-
- //--------------------------------------------------------------------------
- // if socket handle is currently valid, close and then invalidate
- //--------------------------------------------------------------------------
- if (IsSocketValid())
- {
- if (CLOSE(m_socket) != CSimpleSocket::SocketError)
- {
- m_socket = INVALID_SOCKET;
- bRetVal = true;
- }
- }
-
- TranslateSocketError();
-
- return bRetVal;
+ bool bRetVal = false;
+
+ //--------------------------------------------------------------------------
+ // delete internal buffer
+ //--------------------------------------------------------------------------
+ if (m_pBuffer != NULL)
+ {
+ delete [] m_pBuffer;
+ m_pBuffer = NULL;
+ }
+
+ //--------------------------------------------------------------------------
+ // if socket handle is currently valid, close and then invalidate
+ //--------------------------------------------------------------------------
+ if (IsSocketValid())
+ {
+ if (CLOSE(m_socket) != CSimpleSocket::SocketError)
+ {
+ m_socket = INVALID_SOCKET;
+ bRetVal = true;
+ }
+ }
+
+ TranslateSocketError();
+
+ return bRetVal;
}
@@ -503,12 +503,12 @@ bool CSimpleSocket::Close(void)
//------------------------------------------------------------------------------
bool CSimpleSocket::Shutdown(CShutdownMode nShutdown)
{
- CSocketError nRetVal = SocketEunknown;
+ CSocketError nRetVal = SocketEunknown;
- nRetVal = (CSocketError)shutdown(m_socket, CSimpleSocket::Sends);
- TranslateSocketError();
+ nRetVal = (CSocketError)shutdown(m_socket, CSimpleSocket::Sends);
+ TranslateSocketError();
- return (nRetVal == CSimpleSocket::SocketSuccess) ? true: false;
+ return (nRetVal == CSimpleSocket::SocketSuccess) ? true: false;
}
@@ -542,7 +542,7 @@ bool CSimpleSocket::Flush()
bRetVal = true;
}
- TranslateSocketError();
+ TranslateSocketError();
}
//----------------------------------------------------------------------
@@ -568,7 +568,7 @@ int32 CSimpleSocket::Writev(const struct iovec *pVector, size_t nCount)
//--------------------------------------------------------------------------
// Send each buffer as a separate send, windows does not support this
- // function call.
+ // function call.
//--------------------------------------------------------------------------
for (i = 0; i < (int32)nCount; i++)
{
@@ -579,12 +579,12 @@ int32 CSimpleSocket::Writev(const struct iovec *pVector, size_t nCount)
nBytesSent += nBytes;
}
-
+
if (i > 0)
{
Flush();
}
-
+
return nBytesSent;
}
@@ -623,7 +623,7 @@ bool CSimpleSocket::SetReceiveTimeout(int32 nRecvTimeoutSec, int32 nRecvTimeoutU
m_stRecvTimeout.tv_usec = nRecvTimeoutUsec;
//--------------------------------------------------------------------------
- // Sanity check to make sure the options are supported!
+ // Sanity check to make sure the options are supported!
//--------------------------------------------------------------------------
if (SETSOCKOPT(m_socket, SOL_SOCKET, SO_RCVTIMEO, &m_stRecvTimeout,
sizeof(struct timeval)) == CSimpleSocket::SocketError)
@@ -650,7 +650,7 @@ bool CSimpleSocket::SetSendTimeout(int32 nSendTimeoutSec, int32 nSendTimeoutUsec
m_stSendTimeout.tv_usec = nSendTimeoutUsec;
//--------------------------------------------------------------------------
- // Sanity check to make sure the options are supported!
+ // Sanity check to make sure the options are supported!
//--------------------------------------------------------------------------
if (SETSOCKOPT(m_socket, SOL_SOCKET, SO_SNDTIMEO, &m_stSendTimeout,
sizeof(struct timeval)) == CSimpleSocket::SocketError)
@@ -666,7 +666,7 @@ bool CSimpleSocket::SetSendTimeout(int32 nSendTimeoutSec, int32 nSendTimeoutUsec
//------------------------------------------------------------------------------
//
// SetOptionReuseAddr()
-//
+//
//------------------------------------------------------------------------------
bool CSimpleSocket::SetOptionReuseAddr()
{
@@ -687,7 +687,7 @@ bool CSimpleSocket::SetOptionReuseAddr()
//------------------------------------------------------------------------------
//
// SetOptionLinger()
-//
+//
//------------------------------------------------------------------------------
bool CSimpleSocket::SetOptionLinger(bool bEnable, uint16 nTime)
{
@@ -709,12 +709,12 @@ bool CSimpleSocket::SetOptionLinger(bool bEnable, uint16 nTime)
//------------------------------------------------------------------------------
//
-// Receive() - Attempts to receive a block of data on an established
-// connection. Data is received in an internal buffer managed
-// by the class. This buffer is only valid until the next call
+// Receive() - Attempts to receive a block of data on an established
+// connection. Data is received in an internal buffer managed
+// by the class. This buffer is only valid until the next call
// to Receive(), a call to Close(), or until the object goes out
-// of scope.
-//
+// of scope.
+//
//------------------------------------------------------------------------------
int32 CSimpleSocket::Receive(int32 nMaxBytes)
{
@@ -741,11 +741,11 @@ int32 CSimpleSocket::Receive(int32 nMaxBytes)
//--------------------------------------------------------------------------
// Allocate a new internal buffer to receive data.
//--------------------------------------------------------------------------
- if (m_pBuffer == NULL)
- {
- m_nBufferSize = nMaxBytes;
- m_pBuffer = new uint8[nMaxBytes];
- }
+ if (m_pBuffer == NULL)
+ {
+ m_nBufferSize = nMaxBytes;
+ m_pBuffer = new uint8[nMaxBytes];
+ }
SetSocketError(SocketSuccess);
@@ -755,56 +755,56 @@ int32 CSimpleSocket::Receive(int32 nMaxBytes)
switch (m_nSocketType)
{
//----------------------------------------------------------------------
- // If zero bytes are received, then return. If SocketERROR is
- // received, free buffer and return CSocket::SocketError (-1) to caller.
+ // If zero bytes are received, then return. If SocketERROR is
+ // received, free buffer and return CSocket::SocketError (-1) to caller.
//----------------------------------------------------------------------
- case CSimpleSocket::SocketTypeTcp:
+ case CSimpleSocket::SocketTypeTcp:
+ {
+ do
{
- do
+ m_nBytesReceived = RECV(m_socket, (m_pBuffer + m_nBytesReceived),
+ nMaxBytes, m_nFlags);
+ TranslateSocketError();
+ } while ((GetSocketError() == CSimpleSocket::SocketInterrupted));
+
+ break;
+ }
+ case CSimpleSocket::SocketTypeUdp:
+ {
+ uint32 srcSize;
+
+ srcSize = sizeof(struct sockaddr_in);
+
+ if (GetMulticast() == true)
+ {
+ do
{
- m_nBytesReceived = RECV(m_socket, (m_pBuffer + m_nBytesReceived),
- nMaxBytes, m_nFlags);
+ m_nBytesReceived = RECVFROM(m_socket, m_pBuffer, nMaxBytes, 0,
+ &m_stMulticastGroup, &srcSize);
TranslateSocketError();
- } while ((GetSocketError() == CSimpleSocket::SocketInterrupted));
-
- break;
+ } while (GetSocketError() == CSimpleSocket::SocketInterrupted);
}
- case CSimpleSocket::SocketTypeUdp:
+ else
{
- uint32 srcSize;
-
- srcSize = sizeof(struct sockaddr_in);
-
- if (GetMulticast() == true)
+ do
{
- do
- {
- m_nBytesReceived = RECVFROM(m_socket, m_pBuffer, nMaxBytes, 0,
- &m_stMulticastGroup, &srcSize);
- TranslateSocketError();
- } while (GetSocketError() == CSimpleSocket::SocketInterrupted);
- }
- else
- {
- do
- {
- m_nBytesReceived = RECVFROM(m_socket, m_pBuffer, nMaxBytes, 0,
- &m_stClientSockaddr, &srcSize);
- TranslateSocketError();
- } while (GetSocketError() == CSimpleSocket::SocketInterrupted);
- }
-
- break;
+ m_nBytesReceived = RECVFROM(m_socket, m_pBuffer, nMaxBytes, 0,
+ &m_stClientSockaddr, &srcSize);
+ TranslateSocketError();
+ } while (GetSocketError() == CSimpleSocket::SocketInterrupted);
}
- default:
- break;
+
+ break;
+ }
+ default:
+ break;
}
-
+
m_timer.SetEndTime();
TranslateSocketError();
//--------------------------------------------------------------------------
- // If we encounter an error translate the error code and return. One
+ // If we encounter an error translate the error code and return. One
// possible error code could be EAGAIN (EWOULDBLOCK) if the socket is
// non-blocking. This does not mean there is an error, but no data is
// yet available on the socket.
@@ -900,7 +900,7 @@ bool CSimpleSocket::SetBlocking(void)
//------------------------------------------------------------------------------
//
-// SendFile() - stands-in for system provided sendfile
+// SendFile() - stands-in for system provided sendfile
//
//------------------------------------------------------------------------------
int32 CSimpleSocket::SendFile(int32 nOutFd, int32 nInFd, off_t *pOffset, int32 nCount)
@@ -912,7 +912,7 @@ int32 CSimpleSocket::SendFile(int32 nOutFd, int32 nInFd, off_t *pOffset, int32 n
if (lseek(nInFd, *pOffset, SEEK_SET) == -1)
{
- return -1;
+ return -1;
}
while (nOutCount < nCount)
@@ -931,18 +931,18 @@ int32 CSimpleSocket::SendFile(int32 nOutFd, int32 nInFd, off_t *pOffset, int32 n
nOutCount += nInCount;
}
-
+
*pOffset += nOutCount;
TranslateSocketError();
return nOutCount;
}
-
+
//------------------------------------------------------------------------------
//
-// TranslateSocketError() -
+// TranslateSocketError() -
//
//------------------------------------------------------------------------------
void CSimpleSocket::TranslateSocketError(void)
@@ -950,118 +950,118 @@ void CSimpleSocket::TranslateSocketError(void)
#ifdef _LINUX
switch (errno)
{
- case EXIT_SUCCESS:
- SetSocketError(CSimpleSocket::SocketSuccess);
- break;
- case ENOTCONN:
- SetSocketError(CSimpleSocket::SocketNotconnected);
- break;
- case ENOTSOCK:
- case EBADF:
- case EACCES:
- case EAFNOSUPPORT:
- case EMFILE:
- case ENFILE:
- case ENOBUFS:
- case ENOMEM:
- case EPROTONOSUPPORT:
- SetSocketError(CSimpleSocket::SocketInvalidSocket);
- break;
- case ECONNREFUSED :
- SetSocketError(CSimpleSocket::SocketConnectionRefused);
- break;
- case ETIMEDOUT:
- SetSocketError(CSimpleSocket::SocketTimedout);
- break;
- case EINPROGRESS:
- SetSocketError(CSimpleSocket::SocketEinprogress);
- break;
- case EWOULDBLOCK:
- // case EAGAIN:
- SetSocketError(CSimpleSocket::SocketEwouldblock);
- break;
- case EINTR:
- SetSocketError(CSimpleSocket::SocketInterrupted);
- break;
- case ECONNABORTED:
- SetSocketError(CSimpleSocket::SocketConnectionAborted);
- break;
- case EINVAL:
- case EPROTO:
- SetSocketError(CSimpleSocket::SocketProtocolError);
- break;
- case EPERM:
- SetSocketError(CSimpleSocket::SocketFirewallError);
- break;
- case EFAULT:
- SetSocketError(CSimpleSocket::SocketInvalidSocketBuffer);
- break;
- case ECONNRESET:
- SetSocketError(CSimpleSocket::SocketConnectionReset);
- break;
- case ENOPROTOOPT:
- SetSocketError(CSimpleSocket::SocketConnectionReset);
- break;
- default:
- SetSocketError(CSimpleSocket::SocketEunknown);
- break;
+ case EXIT_SUCCESS:
+ SetSocketError(CSimpleSocket::SocketSuccess);
+ break;
+ case ENOTCONN:
+ SetSocketError(CSimpleSocket::SocketNotconnected);
+ break;
+ case ENOTSOCK:
+ case EBADF:
+ case EACCES:
+ case EAFNOSUPPORT:
+ case EMFILE:
+ case ENFILE:
+ case ENOBUFS:
+ case ENOMEM:
+ case EPROTONOSUPPORT:
+ SetSocketError(CSimpleSocket::SocketInvalidSocket);
+ break;
+ case ECONNREFUSED :
+ SetSocketError(CSimpleSocket::SocketConnectionRefused);
+ break;
+ case ETIMEDOUT:
+ SetSocketError(CSimpleSocket::SocketTimedout);
+ break;
+ case EINPROGRESS:
+ SetSocketError(CSimpleSocket::SocketEinprogress);
+ break;
+ case EWOULDBLOCK:
+ // case EAGAIN:
+ SetSocketError(CSimpleSocket::SocketEwouldblock);
+ break;
+ case EINTR:
+ SetSocketError(CSimpleSocket::SocketInterrupted);
+ break;
+ case ECONNABORTED:
+ SetSocketError(CSimpleSocket::SocketConnectionAborted);
+ break;
+ case EINVAL:
+ case EPROTO:
+ SetSocketError(CSimpleSocket::SocketProtocolError);
+ break;
+ case EPERM:
+ SetSocketError(CSimpleSocket::SocketFirewallError);
+ break;
+ case EFAULT:
+ SetSocketError(CSimpleSocket::SocketInvalidSocketBuffer);
+ break;
+ case ECONNRESET:
+ SetSocketError(CSimpleSocket::SocketConnectionReset);
+ break;
+ case ENOPROTOOPT:
+ SetSocketError(CSimpleSocket::SocketConnectionReset);
+ break;
+ default:
+ SetSocketError(CSimpleSocket::SocketEunknown);
+ break;
}
#endif
#ifdef WIN32
int32 nError = WSAGetLastError();
switch (nError)
{
- case EXIT_SUCCESS:
- SetSocketError(CSimpleSocket::SocketSuccess);
- break;
- case WSAEBADF:
- case WSAENOTCONN:
- SetSocketError(CSimpleSocket::SocketNotconnected);
- break;
- case WSAEINTR:
- SetSocketError(CSimpleSocket::SocketInterrupted);
- break;
- case WSAEACCES:
- case WSAEAFNOSUPPORT:
- case WSAEINVAL:
- case WSAEMFILE:
- case WSAENOBUFS:
- case WSAEPROTONOSUPPORT:
- SetSocketError(CSimpleSocket::SocketInvalidSocket);
- break;
- case WSAECONNREFUSED :
- SetSocketError(CSimpleSocket::SocketConnectionRefused);
- break;
- case WSAETIMEDOUT:
- SetSocketError(CSimpleSocket::SocketTimedout);
- break;
- case WSAEINPROGRESS:
- SetSocketError(CSimpleSocket::SocketEinprogress);
- break;
- case WSAECONNABORTED:
- SetSocketError(CSimpleSocket::SocketConnectionAborted);
- break;
- case WSAEWOULDBLOCK:
- SetSocketError(CSimpleSocket::SocketEwouldblock);
- break;
- case WSAENOTSOCK:
- SetSocketError(CSimpleSocket::SocketInvalidSocket);
- break;
- case WSAECONNRESET:
- SetSocketError(CSimpleSocket::SocketConnectionReset);
- break;
- case WSANO_DATA:
- SetSocketError(CSimpleSocket::SocketInvalidAddress);
- break;
- case WSAEADDRINUSE:
- SetSocketError(CSimpleSocket::SocketAddressInUse);
- break;
- case WSAEFAULT:
- SetSocketError(CSimpleSocket::SocketInvalidPointer);
- break;
- default:
- SetSocketError(CSimpleSocket::SocketEunknown);
- break;
+ case EXIT_SUCCESS:
+ SetSocketError(CSimpleSocket::SocketSuccess);
+ break;
+ case WSAEBADF:
+ case WSAENOTCONN:
+ SetSocketError(CSimpleSocket::SocketNotconnected);
+ break;
+ case WSAEINTR:
+ SetSocketError(CSimpleSocket::SocketInterrupted);
+ break;
+ case WSAEACCES:
+ case WSAEAFNOSUPPORT:
+ case WSAEINVAL:
+ case WSAEMFILE:
+ case WSAENOBUFS:
+ case WSAEPROTONOSUPPORT:
+ SetSocketError(CSimpleSocket::SocketInvalidSocket);
+ break;
+ case WSAECONNREFUSED :
+ SetSocketError(CSimpleSocket::SocketConnectionRefused);
+ break;
+ case WSAETIMEDOUT:
+ SetSocketError(CSimpleSocket::SocketTimedout);
+ break;
+ case WSAEINPROGRESS:
+ SetSocketError(CSimpleSocket::SocketEinprogress);
+ break;
+ case WSAECONNABORTED:
+ SetSocketError(CSimpleSocket::SocketConnectionAborted);
+ break;
+ case WSAEWOULDBLOCK:
+ SetSocketError(CSimpleSocket::SocketEwouldblock);
+ break;
+ case WSAENOTSOCK:
+ SetSocketError(CSimpleSocket::SocketInvalidSocket);
+ break;
+ case WSAECONNRESET:
+ SetSocketError(CSimpleSocket::SocketConnectionReset);
+ break;
+ case WSANO_DATA:
+ SetSocketError(CSimpleSocket::SocketInvalidAddress);
+ break;
+ case WSAEADDRINUSE:
+ SetSocketError(CSimpleSocket::SocketAddressInUse);
+ break;
+ case WSAEFAULT:
+ SetSocketError(CSimpleSocket::SocketInvalidPointer);
+ break;
+ default:
+ SetSocketError(CSimpleSocket::SocketEunknown);
+ break;
}
#endif
}
@@ -1086,7 +1086,7 @@ bool CSimpleSocket::Select(int32 nTimeoutSec, int32 nTimeoutUSec)
FD_SET(m_socket, &m_errorFds);
FD_SET(m_socket, &m_readFds);
FD_SET(m_socket, &m_writeFds);
-
+
//---------------------------------------------------------------------
// If timeout has been specified then set value, otherwise set timeout
// to NULL which will block until a descriptor is ready for read/write
@@ -1098,14 +1098,14 @@ bool CSimpleSocket::Select(int32 nTimeoutSec, int32 nTimeoutUSec)
timeout.tv_usec = nTimeoutUSec;
pTimeout = &timeout;
}
-
+
nNumDescriptors = SELECT(m_socket+1, &m_readFds, &m_writeFds, &m_errorFds, pTimeout);
// nNumDescriptors = SELECT(m_socket+1, &m_readFds, NULL, NULL, pTimeout);
-
+
//----------------------------------------------------------------------
// Handle timeout
//----------------------------------------------------------------------
- if (nNumDescriptors == 0)
+ if (nNumDescriptors == 0)
{
SetSocketError(CSimpleSocket::SocketTimedout);
}
@@ -1116,17 +1116,17 @@ bool CSimpleSocket::Select(int32 nTimeoutSec, int32 nTimeoutUSec)
else if ((FD_ISSET(m_socket, &m_readFds)) || (FD_ISSET(m_socket, &m_writeFds)))
{
int32 nLen = sizeof(nError);
-
+
if (GETSOCKOPT(m_socket, SOL_SOCKET, SO_ERROR, &nError, &nLen) == 0)
{
errno = nError;
-
+
if (nError == 0)
{
bRetVal = true;
}
}
-
+
TranslateSocketError();
}
diff --git a/src/SimpleSocket.h b/src/SimpleSocket.h
index bda3ee1..a35e760 100755
--- a/src/SimpleSocket.h
+++ b/src/SimpleSocket.h
@@ -50,32 +50,32 @@
#include <errno.h>
#if defined(_LINUX) || defined (_DARWIN)
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <netinet/tcp.h>
- #include <netinet/ip.h>
- #include <netdb.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netinet/tcp.h>
+#include <netinet/ip.h>
+#include <netdb.h>
#endif
#ifdef _LINUX
- #include <linux/if_packet.h>
- #include <linux/if_packet.h>
- #include <linux/if_ether.h>
- #include <linux/if.h>
- #include <sys/sendfile.h>
+#include <linux/if_packet.h>
+#include <linux/if_packet.h>
+#include <linux/if_ether.h>
+#include <linux/if.h>
+#include <sys/sendfile.h>
#endif
#if defined(_LINUX) || defined (_DARWIN)
- #include <sys/time.h>
- #include <sys/uio.h>
- #include <unistd.h>
- #include <fcntl.h>
+#include <sys/time.h>
+#include <sys/uio.h>
+#include <unistd.h>
+#include <fcntl.h>
#endif
#ifdef WIN32
- #include <io.h>
- #include <winsock2.h>
- #include <Ws2tcpip.h>
+#include <io.h>
+#include <winsock2.h>
+#include <Ws2tcpip.h>
- #define IPTOS_LOWDELAY 0x10 ///
+#define IPTOS_LOWDELAY 0x10
#endif
#include "Host.h"
@@ -85,29 +85,29 @@
// General class macro definitions and typedefs
//-----------------------------------------------------------------------------
#ifndef INVALID_SOCKET
- #define INVALID_SOCKET ~(0)
+#define INVALID_SOCKET ~(0)
#endif
#define SOCKET_SENDFILE_BLOCKSIZE 8192
/// Provides a platform independent class to for socket development.
-/// This class is designed to abstract socket communication development in a
-/// platform independent manner.
+/// This class is designed to abstract socket communication development in a
+/// platform independent manner.
/// - Socket types
/// -# CActiveSocket Class
/// -# CPassiveSocket Class
class CSimpleSocket {
public:
/// Defines the three possible states for shuting down a socket.
- typedef enum
+ typedef enum
{
Receives = SHUT_RD, ///< Shutdown passive socket.
Sends = SHUT_WR, ///< Shutdown active socket.
Both = SHUT_RDWR ///< Shutdown both active and passive sockets.
- } CShutdownMode;
+ } CShutdownMode;
/// Defines the socket types defined by CSimpleSocket class.
- typedef enum
+ typedef enum
{
SocketTypeInvalid, ///< Invalid socket type.
SocketTypeTcp, ///< Defines socket as TCP socket.
@@ -118,7 +118,7 @@ public:
} CSocketType;
/// Defines all error codes handled by the CSimpleSocket class.
- typedef enum
+ typedef enum
{
SocketError = -1, ///< Generic socket error translates to error below.
SocketSuccess = 0, ///< No socket error.
@@ -143,48 +143,50 @@ public:
public:
CSimpleSocket(CSocketType type = SocketTypeTcp);
- CSimpleSocket(CSimpleSocket &socket);
+ CSimpleSocket(CSimpleSocket &socket);
- virtual ~CSimpleSocket()
- {
- if (m_pBuffer != NULL)
- {
- delete [] m_pBuffer;
- m_pBuffer = NULL;
- }
- };
+ virtual ~CSimpleSocket()
+ {
+ if (m_pBuffer != NULL)
+ {
+ delete [] m_pBuffer;
+ m_pBuffer = NULL;
+ }
+ };
/// Initialize instance of CSocket. This method MUST be called before an
- /// object can be used. Errors : CSocket::SocketProtocolError,
+ /// object can be used. Errors : CSocket::SocketProtocolError,
/// CSocket::SocketInvalidSocket,
/// @return true if properly initialized.
virtual bool Initialize(void);
- /// Close socket
- /// @return true if successfully closed otherwise returns false.
- virtual bool Close(void);
+ /// Close socket
+ /// @return true if successfully closed otherwise returns false.
+ virtual bool Close(void);
- /// Shutdown shut down socket send and receive operations
- /// CShutdownMode::Receives - Disables further receive operations.
- /// CShutdownMode::Sends - Disables further send operations.
- /// CShutdownBoth:: - Disables further send and receive operations.
- /// @param nShutdown specifies the type of shutdown.
- /// @return true if successfully shutdown otherwise returns false.
- virtual bool Shutdown(CShutdownMode nShutdown);
+ /// Shutdown shut down socket send and receive operations
+ /// CShutdownMode::Receives - Disables further receive operations.
+ /// CShutdownMode::Sends - Disables further send operations.
+ /// CShutdownBoth:: - Disables further send and receive operations.
+ /// @param nShutdown specifies the type of shutdown.
+ /// @return true if successfully shutdown otherwise returns false.
+ virtual bool Shutdown(CShutdownMode nShutdown);
/// Examine the socket descriptor sets currently owned by the instance of
- /// the socket class (the readfds, writefds, and errorfds parameters) to
- /// see whether some of their descriptors are ready for reading, are ready
+ /// the socket class (the readfds, writefds, and errorfds parameters) to
+ /// see whether some of their descriptors are ready for reading, are ready
/// for writing, or have an exceptional condition pending, respectively.
- /// Block until an event happens on the specified file descriptors.
+ /// Block until an event happens on the specified file descriptors.
/// @return true if socket has data ready, or false if not ready or timed out.
- virtual bool Select(void) { return Select(0,0); };
+ virtual bool Select(void) {
+ return Select(0,0);
+ };
/// Examine the socket descriptor sets currently owned by the instance of
- /// the socket class (the readfds, writefds, and errorfds parameters) to
- /// see whether some of their descriptors are ready for reading, are ready
+ /// the socket class (the readfds, writefds, and errorfds parameters) to
+ /// see whether some of their descriptors are ready for reading, are ready
/// for writing, or have an exceptional condition pending, respectively.
- /// @param nTimeoutSec timeout in seconds for select.
+ /// @param nTimeoutSec timeout in seconds for select.
/// @param nTimeoutUSec timeout in micro seconds for select.
/// @return true if socket has data ready, or false if not ready or timed out.
virtual bool Select(int32 nTimeoutSec, int32 nTimeoutUSec);
@@ -192,29 +194,31 @@ public:
/// Does the current instance of the socket object contain a valid socket
/// descriptor.
/// @return true if the socket object contains a valid socket descriptor.
- virtual bool IsSocketValid(void) { return (m_socket != SocketError); };
+ virtual bool IsSocketValid(void) {
+ return (m_socket != SocketError);
+ };
/// Provides a standard error code for cross platform development by
/// mapping the operating system error to an error defined by the CSocket
/// class.
void TranslateSocketError(void);
- /// Attempts to receive a block of data on an established connection.
+ /// Attempts to receive a block of data on an established connection.
/// @param nMaxBytes maximum number of bytes to receive.
/// @return number of bytes actually received.
- /// @return of zero means the connection has been shutdown on the other side.
- /// @return of -1 means that an error has occurred.
+ /// @return of zero means the connection has been shutdown on the other side.
+ /// @return of -1 means that an error has occurred.
virtual int32 Receive(int32 nMaxBytes = 1);
/// Attempts to send a block of data on an established connection.
/// @param pBuf block of data to be sent.
/// @param bytesToSend size of data block to be sent.
/// @return number of bytes actually sent.
- /// @return of zero means the connection has been shutdown on the other side.
- /// @return of -1 means that an error has occurred.
+ /// @return of zero means the connection has been shutdown on the other side.
+ /// @return of -1 means that an error has occurred.
virtual int32 Send(const uint8 *pBuf, size_t bytesToSend);
- /// Attempts to send at most nNumItem blocks described by sendVector
+ /// Attempts to send at most nNumItem blocks described by sendVector
/// to the socket descriptor associated with the socket object.
/// @param sendVector pointer to an array of iovec structures
/// @param nNumItems number of items in the vector to process
@@ -224,14 +228,14 @@ public:
/// means that an error has occurred.
virtual int32 Send(const struct iovec *sendVector, int32 nNumItems);
- /// Copies data between one file descriptor and another.
- /// On some systems this copying is done within the kernel, and thus is
- /// more efficient than the combination of CSimpleSocket::Send and
- /// CSimpleSocket::Receive, which would require transferring data to and
- /// from user space.
- /// <br>\b Note: This is available on all implementations, but the kernel
+ /// Copies data between one file descriptor and another.
+ /// On some systems this copying is done within the kernel, and thus is
+ /// more efficient than the combination of CSimpleSocket::Send and
+ /// CSimpleSocket::Receive, which would require transferring data to and
+ /// from user space.
+ /// <br>\b Note: This is available on all implementations, but the kernel
/// implementation is only available on Unix type systems.
- /// @param nOutFd descriptor opened for writing.
+ /// @param nOutFd descriptor opened for writing.
/// @param nInFd descriptor opened for reading.
/// @param pOffset from which to start reading data from input file.
/// @param nCount number of bytes to copy between file descriptors.
@@ -240,7 +244,9 @@ public:
/// Returns blocking/non-blocking state of socket.
/// @return true if the socket is non-blocking, else return false.
- bool IsNonblocking(void) { return (m_bIsBlocking == false); };
+ bool IsNonblocking(void) {
+ return (m_bIsBlocking == false);
+ };
/// Set the socket to blocking.
/// @return true if successful set to blocking, else return false;
@@ -251,31 +257,37 @@ public:
bool SetNonblocking(void);
/// Get a pointer to internal receive buffer. The user MUST not free this
- /// pointer when finished. This memory is managed internally by the CSocket
+ /// pointer when finished. This memory is managed internally by the CSocket
/// class.
/// @return pointer to data if valid, else returns NULL.
- uint8 *GetData(void) { return m_pBuffer; };
+ uint8 *GetData(void) {
+ return m_pBuffer;
+ };
- /// Returns the number of bytes received on the last call to
+ /// Returns the number of bytes received on the last call to
/// CSocket::Receive().
/// @return number of bytes received.
- int32 GetBytesReceived(void) { return m_nBytesReceived; };
+ int32 GetBytesReceived(void) {
+ return m_nBytesReceived;
+ };
- /// Returns the number of bytes sent on the last call to
+ /// Returns the number of bytes sent on the last call to
/// CSocket::Send().
/// @return number of bytes sent.
- int32 GetBytesSent(void) { return m_nBytesSent; };
+ int32 GetBytesSent(void) {
+ return m_nBytesSent;
+ };
- /// Controls the actions taken when CSimpleSocket::Close is executed on a
- /// socket object that has unsent data. The default value for this option
+ /// Controls the actions taken when CSimpleSocket::Close is executed on a
+ /// socket object that has unsent data. The default value for this option
/// is \b off.
/// - Following are the three possible scenarios.
- /// -# \b bEnable is false, CSimpleSocket::Close returns immediately, but
+ /// -# \b bEnable is false, CSimpleSocket::Close returns immediately, but
/// any unset data is transmitted (after CSimpleSocket::Close returns)
- /// -# \b bEnable is true and \b nTime is zero, CSimpleSocket::Close return
+ /// -# \b bEnable is true and \b nTime is zero, CSimpleSocket::Close return
/// immediately and any unsent data is discarded.
/// -# \b bEnable is true and \b nTime is nonzero, CSimpleSocket::Close does
- /// not return until all unsent data is transmitted (or the connection is
+ /// not return until all unsent data is transmitted (or the connection is
/// Closed by the remote system).
/// <br><p>
/// @param bEnable true to enable option false to disable option.
@@ -284,54 +296,62 @@ public:
bool SetOptionLinger(bool bEnable, uint16 nTime);
/// Tells the kernel that even if this port is busy (in the TIME_WAIT state),
- /// go ahead and reuse it anyway. If it is busy, but with another state,
+ /// go ahead and reuse it anyway. If it is busy, but with another state,
/// you will still get an address already in use error.
/// @return true if option successfully set
bool SetOptionReuseAddr();
/// Gets the timeout value that specifies the maximum number of seconds a
- /// call to CSimpleSocket::Open waits until it completes.
+ /// call to CSimpleSocket::Open waits until it completes.
/// @return the length of time in seconds
- int32 GetConnectTimeoutSec(void) { return m_stConnectTimeout.tv_sec; };
+ int32 GetConnectTimeoutSec(void) {
+ return m_stConnectTimeout.tv_sec;
+ };
/// Gets the timeout value that specifies the maximum number of microseconds
- /// a call to CSimpleSocket::Open waits until it completes.
+ /// a call to CSimpleSocket::Open waits until it completes.
/// @return the length of time in microseconds
- int32 GetConnectTimeoutUSec(void) { return m_stConnectTimeout.tv_usec; };
+ int32 GetConnectTimeoutUSec(void) {
+ return m_stConnectTimeout.tv_usec;
+ };
- /// Sets the timeout value that specifies the maximum amount of time a call
+ /// Sets the timeout value that specifies the maximum amount of time a call
/// to CSimpleSocket::Receive waits until it completes. Use the method
/// CSimpleSocket::SetReceiveTimeout to specify the number of seconds to wait.
/// If a call to CSimpleSocket::Receive has blocked for the specified length of
- /// time without receiving additional data, it returns with a partial count
- /// or CSimpleSocket::GetSocketError set to CSimpleSocket::SocketEwouldblock if no data
- /// were received.
+ /// time without receiving additional data, it returns with a partial count
+ /// or CSimpleSocket::GetSocketError set to CSimpleSocket::SocketEwouldblock if no data
+ /// were received.
/// @param nConnectTimeoutSec of timeout in seconds.
/// @param nConnectTimeoutUsec of timeout in microseconds.
/// @return true if socket connection timeout was successfully set.
- void SetConnectTimeout(int32 nConnectTimeoutSec, int32 nConnectTimeoutUsec = 0)
- {
- m_stConnectTimeout.tv_sec = nConnectTimeoutSec;
- m_stConnectTimeout.tv_usec = nConnectTimeoutUsec;
+ void SetConnectTimeout(int32 nConnectTimeoutSec, int32 nConnectTimeoutUsec = 0)
+ {
+ m_stConnectTimeout.tv_sec = nConnectTimeoutSec;
+ m_stConnectTimeout.tv_usec = nConnectTimeoutUsec;
};
/// Gets the timeout value that specifies the maximum number of seconds a
- /// a call to CSimpleSocket::Receive waits until it completes.
+ /// a call to CSimpleSocket::Receive waits until it completes.
/// @return the length of time in seconds
- int32 GetReceiveTimeoutSec(void) { return m_stRecvTimeout.tv_sec; };
+ int32 GetReceiveTimeoutSec(void) {
+ return m_stRecvTimeout.tv_sec;
+ };
/// Gets the timeout value that specifies the maximum number of microseconds
- /// a call to CSimpleSocket::Receive waits until it completes.
+ /// a call to CSimpleSocket::Receive waits until it completes.
/// @return the length of time in microseconds
- int32 GetReceiveTimeoutUSec(void) { return m_stRecvTimeout.tv_usec; };
+ int32 GetReceiveTimeoutUSec(void) {
+ return m_stRecvTimeout.tv_usec;
+ };
- /// Sets the timeout value that specifies the maximum amount of time a call
+ /// Sets the timeout value that specifies the maximum amount of time a call
/// to CSimpleSocket::Receive waits until it completes. Use the method
/// CSimpleSocket::SetReceiveTimeout to specify the number of seconds to wait.
/// If a call to CSimpleSocket::Receive has blocked for the specified length of
- /// time without receiving additional data, it returns with a partial count
- /// or CSimpleSocket::GetSocketError set to CSimpleSocket::SocketEwouldblock if no data
- /// were received.
+ /// time without receiving additional data, it returns with a partial count
+ /// or CSimpleSocket::GetSocketError set to CSimpleSocket::SocketEwouldblock if no data
+ /// were received.
/// @param nRecvTimeoutSec of timeout in seconds.
/// @param nRecvTimeoutUsec of timeout in microseconds.
/// @return true if socket timeout was successfully set.
@@ -340,101 +360,133 @@ public:
/// Enable/disable multicast for a socket. This options is only valid for
/// socket descriptors of type CSimpleSocket::SocketTypeUdp.
/// @return true if multicast was enabled or false if socket type is not
- /// CSimpleSocket::SocketTypeUdp and the error will be set to
- /// CSimpleSocket::SocketProtocolError
+ /// CSimpleSocket::SocketTypeUdp and the error will be set to
+ /// CSimpleSocket::SocketProtocolError
bool SetMulticast(bool bEnable, uint8 multicastTTL = 1);
/// Return true if socket is multicast or false is socket is unicast
/// @return true if multicast is enabled
- bool GetMulticast() { return m_bIsMulticast; };
+ bool GetMulticast() {
+ return m_bIsMulticast;
+ };
/// Bind socket to a specific interface when using multicast.
/// @return true if successfully bound to interface
bool BindInterface(uint8 *pInterface);
/// Gets the timeout value that specifies the maximum number of seconds a
- /// a call to CSimpleSocket::Send waits until it completes.
+ /// a call to CSimpleSocket::Send waits until it completes.
/// @return the length of time in seconds
- int32 GetSendTimeoutSec(void) { return m_stSendTimeout.tv_sec; };
+ int32 GetSendTimeoutSec(void) {
+ return m_stSendTimeout.tv_sec;
+ };
/// Gets the timeout value that specifies the maximum number of microseconds
- /// a call to CSimpleSocket::Send waits until it completes.
+ /// a call to CSimpleSocket::Send waits until it completes.
/// @return the length of time in microseconds
- int32 GetSendTimeoutUSec(void) { return m_stSendTimeout.tv_usec; };
+ int32 GetSendTimeoutUSec(void) {
+ return m_stSendTimeout.tv_usec;
+ };
- /// Gets the timeout value that specifies the maximum amount of time a call
- /// to CSimpleSocket::Send waits until it completes.
+ /// Gets the timeout value that specifies the maximum amount of time a call
+ /// to CSimpleSocket::Send waits until it completes.
/// @return the length of time in seconds
bool SetSendTimeout(int32 nSendTimeoutSec, int32 nSendTimeoutUsec = 0);
/// Returns the last error that occured for the instace of the CSimpleSocket
- /// instance. This method should be called immediately to retrieve the
+ /// instance. This method should be called immediately to retrieve the
/// error code for the failing mehtod call.
/// @return last error that occured.
- CSocketError GetSocketError(void) { return m_socketErrno; };
+ CSocketError GetSocketError(void) {
+ return m_socketErrno;
+ };
/// Get the total time the of the last operation in milliseconds.
/// @return number of milliseconds of last operation.
- uint32 GetTotalTimeMs() { return m_timer.GetMilliSeconds(); };
+ uint32 GetTotalTimeMs() {
+ return m_timer.GetMilliSeconds();
+ };
/// Get the total time the of the last operation in microseconds.
/// @return number of microseconds or last operation.
- uint32 GetTotalTimeUsec() { return m_timer.GetMicroSeconds(); };
+ uint32 GetTotalTimeUsec() {
+ return m_timer.GetMicroSeconds();
+ };
/// Return Differentiated Services Code Point (DSCP) value currently set on the socket object.
/// @return DSCP for current socket object.
/// <br><br> \b NOTE: Windows special notes http://support.microsoft.com/kb/248611.
- int GetSocketDscp(void);
+ int GetSocketDscp(void);
- /// Set Differentiated Services Code Point (DSCP) for socket object.
- /// @param nDscp value of TOS setting which will be converted to DSCP
- /// @return true if DSCP value was properly set
+ /// Set Differentiated Services Code Point (DSCP) for socket object.
+ /// @param nDscp value of TOS setting which will be converted to DSCP
+ /// @return true if DSCP value was properly set
/// <br><br> \b NOTE: Windows special notes http://support.microsoft.com/kb/248611.
- bool SetSocketDscp(int nDscp);
+ bool SetSocketDscp(int nDscp);
- /// Return socket descriptor
- /// @return socket descriptor which is a signed 32 bit integer.
- SOCKET GetSocketDescriptor() { return m_socket; };
+ /// Return socket descriptor
+ /// @return socket descriptor which is a signed 32 bit integer.
+ SOCKET GetSocketDescriptor() {
+ return m_socket;
+ };
- /// Return socket descriptor
- /// @return socket descriptor which is a signed 32 bit integer.
- CSocketType GetSocketType() { return m_nSocketType; };
+ /// Return socket descriptor
+ /// @return socket descriptor which is a signed 32 bit integer.
+ CSocketType GetSocketType() {
+ return m_nSocketType;
+ };
/// Returns clients Internet host address as a string in standard numbers-and-dots notation.
- /// @return NULL if invalid
- uint8 *GetClientAddr() { return (uint8 *)inet_ntoa(m_stClientSockaddr.sin_addr); };
+ /// @return NULL if invalid
+ uint8 *GetClientAddr() {
+ return (uint8 *)inet_ntoa(m_stClientSockaddr.sin_addr);
+ };
- /// Returns the port number on which the client is connected.
- /// @return client port number.
- int16 GetClientPort() { return m_stClientSockaddr.sin_port; };
+ /// Returns the port number on which the client is connected.
+ /// @return client port number.
+ int16 GetClientPort() {
+ return m_stClientSockaddr.sin_port;
+ };
/// Returns server Internet host address as a string in standard numbers-and-dots notation.
- /// @return NULL if invalid
- uint8 *GetServerAddr() { return (uint8 *)inet_ntoa(m_stServerSockaddr.sin_addr); };
+ /// @return NULL if invalid
+ uint8 *GetServerAddr() {
+ return (uint8 *)inet_ntoa(m_stServerSockaddr.sin_addr);
+ };
- /// Returns the port number on which the server is connected.
- /// @return server port number.
- int16 GetServerPort() { return ntohs(m_stServerSockaddr.sin_port); };
+ /// Returns the port number on which the server is connected.
+ /// @return server port number.
+ int16 GetServerPort() {
+ return ntohs(m_stServerSockaddr.sin_port);
+ };
/// Get the TCP receive buffer window size for the current socket object.
/// <br><br>\b NOTE: Linux will set the receive buffer to twice the value passed.
/// @return zero on failure else the number of bytes of the TCP receive buffer window size if successful.
- uint16 GetReceiveWindowSize() { return GetWindowSize(SO_RCVBUF); };
+ uint16 GetReceiveWindowSize() {
+ return GetWindowSize(SO_RCVBUF);
+ };
/// Get the TCP send buffer window size for the current socket object.
/// <br><br>\b NOTE: Linux will set the send buffer to twice the value passed.
/// @return zero on failure else the number of bytes of the TCP receive buffer window size if successful.
- uint16 GetSendWindowSize() { return GetWindowSize(SO_SNDBUF); };
+ uint16 GetSendWindowSize() {
+ return GetWindowSize(SO_SNDBUF);
+ };
/// Set the TCP receive buffer window size for the current socket object.
/// <br><br>\b NOTE: Linux will set the receive buffer to twice the value passed.
/// @return zero on failure else the number of bytes of the TCP send buffer window size if successful.
- uint16 SetReceiveWindowSize(uint16 nWindowSize) { return SetWindowSize(SO_RCVBUF, nWindowSize); };
+ uint16 SetReceiveWindowSize(uint16 nWindowSize) {
+ return SetWindowSize(SO_RCVBUF, nWindowSize);
+ };
/// Set the TCP send buffer window size for the current socket object.
/// <br><br>\b NOTE: Linux will set the send buffer to twice the value passed.
/// @return zero on failure else the number of bytes of the TCP send buffer window size if successful.
- uint16 SetSendWindowSize(uint16 nWindowSize) { return SetWindowSize(SO_SNDBUF, nWindowSize); };
+ uint16 SetSendWindowSize(uint16 nWindowSize) {
+ return SetWindowSize(SO_SNDBUF, nWindowSize);
+ };
/// Disable the Nagle algorithm (Set TCP_NODELAY to true)
/// @return false if failed to set socket option otherwise return true;
@@ -448,11 +500,15 @@ public:
protected:
/// Set internal socket error to that specified error
/// @param error type of error
- void SetSocketError(CSimpleSocket::CSocketError error) { m_socketErrno = error; };
+ void SetSocketError(CSimpleSocket::CSocketError error) {
+ m_socketErrno = error;
+ };
- /// Set object socket handle to that specified as parameter
+ /// Set object socket handle to that specified as parameter
/// @param socket value of socket descriptor
- void SetSocketHandle(SOCKET socket) { m_socket = socket; };
+ void SetSocketHandle(SOCKET socket) {
+ m_socket = socket;
+ };
private:
/// Generic function used to get the send/receive window size
@@ -464,7 +520,7 @@ private:
uint16 SetWindowSize(uint32 nOptionName, uint32 nWindowSize);
- /// Attempts to send at most nNumItem blocks described by sendVector
+ /// Attempts to send at most nNumItem blocks described by sendVector
/// to the socket descriptor associated with the socket object.
/// @param sendVector pointer to an array of iovec structures
/// @param nNumItems number of items in the vector to process
@@ -476,10 +532,10 @@ private:
int32 Writev(const struct iovec *pVector, size_t nCount);
/// Flush the socket descriptor owned by the object.
- /// @return true data was successfully sent, else return false;
+ /// @return true data was successfully sent, else return false;
bool Flush();
- CSimpleSocket *operator=(CSimpleSocket &socket);
+ CSimpleSocket *operator=(CSimpleSocket &socket);
protected:
SOCKET m_socket; /// socket handle
@@ -503,10 +559,10 @@ protected:
CStatTimer m_timer; /// internal statistics.
#ifdef WIN32
WSADATA m_hWSAData; /// Windows
-#endif
- fd_set m_writeFds; /// write file descriptor set
- fd_set m_readFds; /// read file descriptor set
- fd_set m_errorFds; /// error file descriptor set
+#endif
+ fd_set m_writeFds; /// write file descriptor set
+ fd_set m_readFds; /// read file descriptor set
+ fd_set m_errorFds; /// error file descriptor set
};