NLNET::CSock Class Reference

#include <sock.h>

Inheritance diagram for NLNET::CSock:

NLNET::CTcpSock NLNET::CUdpSock NLNET::CDummyTcpSock NLNET::CListenSock

Detailed Description

CSock: base socket class. One CSock object represents a communication between two hosts, the local one and the remote one. This class implements layer 0 of the NeL Network Engine. This class does not handle conversion between big endian and little endian ; the provided buffers are sent raw.

The "logging" boolean value is necessary because in this implementation we always log to one single global CLog object : there is not one CLog object per socket. Therefore we must prevent the socket used in CNetDisplayer from logging itself... otherwise we would have an infinite recursion.

The "connected" property may have a different meaning whether the socket is a stream socket (e.g. using TCP) or it is a connectionless datagram socket (e.g. using UDP). In the latter, "connected" only means that the local and the remote addresses have been set.

Important note: this class is thread-safe, meaning you can access to a CSock object from multiple threads BUT the only things you are allow to do in parallel are receive/send and read the connected() property.

You must call CSock::initNetwork() before using any network class (even CInetAddress). You must call CSock::releaseNetwork() at the end of your program.

By default, a socket is in blocking mode. Call setNonBlockingMode() to change this behaviour.

Author:
Olivier Cado

Nevrax France

Date:
2000-2001

Definition at line 121 of file sock.h.

Socket setup

virtual void close ()
virtual void connect (const CInetAddress &addr)
bool nonBlockingMode () const
 Returns the nonblocking mode.

void setNonBlockingMode (bool bm)
virtual ~CSock ()
 Destructor (shutdown + close).


Receiving data

bool dataAvailable ()
 Checks if there is some data to receive, waiting (blocking) at most for the time out value.

CSock::TSockResult receive (uint8 *buffer, uint32 &len, bool throw_exception=true)

Public Types

enum  TSockResult { Ok, WouldBlock, ConnectionClosed, Error }

Public Member Functions

uint64 bytesReceived () const
 Returns the number of bytes received since the latest connection.

uint64 bytesSent () const
 Returns the number of bytes sent since the latest connection.

sint32 getSendBufferSize ()
 Gets the send buffer size.

void setSendBufferSize (sint32 size)
 Sets the send buffer size.

void setTimeOutValue (long sec, long ms)
 Change the time out value used in getDataAvailable(), which is 0 by default.


Static Public Member Functions

std::string errorString (uint errorcode)
 Returns a string explaining the network error (see getLastError()).

uint getLastError ()
bool initialized ()
 Returns true if the network engine is initialized.

void initNetwork ()
 Initialize the network engine, if it is not already done.

void releaseNetwork ()
 Releases the network engine.


Protected Member Functions

void createSocket (int type, int protocol)
 Creates the socket and get a valid descriptor.

 CSock (SOCKET sock, const CInetAddress &remoteaddr)
 Construct a CSock object using an existing connected socket descriptor and its associated remote address.

 CSock (bool logging=true)
void setLocalAddress ()
 Sets the local address.


Protected Attributes

uint64 _BytesReceived
 Number of bytes received on this socket.

uint64 _BytesSent
 Number of bytes sent on this socket.

volatile bool _Connected
 True after calling connect().

CInetAddress _LocalAddr
 Address of local host (valid if connected).

bool _Logging
 If false, do not log any information.

bool _NonBlocking
 If true, the socket is in nonblocking mode.

CInetAddress _RemoteAddr
 Address of the remote host (valid if connected).

SOCKET _Sock
 Socket descriptor.

long _TimeoutMs
 Secondary time out value (ms) for select in dataAvailable().

long _TimeoutS
 Main time out value (sec) for select in dataAvailable().


Private Attributes

uint32 _MaxReceiveTime
uint32 _MaxSendTime

Static Private Attributes

bool _Initialized
 True if the network library has been initialized.


Member Enumeration Documentation

enum NLNET::CSock::TSockResult
 

Enumeration values:
Ok 
WouldBlock 
ConnectionClosed 
Error 

Definition at line 125 of file sock.h.


Constructor & Destructor Documentation

virtual NLNET::CSock::~CSock  )  [virtual]
 

Destructor (shutdown + close).

NLNET::CSock::CSock bool  logging = true  )  [protected]
 

Constructor.

Parameters:
logging Disable logging if the server socket object is used by the logging system, to avoid infinite recursion

NLNET::CSock::CSock SOCKET  sock,
const CInetAddress remoteaddr
[protected]
 

Construct a CSock object using an existing connected socket descriptor and its associated remote address.


Member Function Documentation

uint64 NLNET::CSock::bytesReceived  )  const [inline]
 

Returns the number of bytes received since the latest connection.

Definition at line 249 of file sock.h.

References uint64.

Referenced by NLNET::CBufClient::bytesDownloaded().

00249 { return _BytesReceived; }

uint64 NLNET::CSock::bytesSent  )  const [inline]
 

Returns the number of bytes sent since the latest connection.

Definition at line 252 of file sock.h.

References uint64.

Referenced by NLNET::CBufClient::bytesUploaded().

00252 { return _BytesSent; }

virtual void NLNET::CSock::close  )  [virtual]
 

Closes the socket (without shutdown) In general you don't need to call this method. But you can call it to:

  • close a listening socket (i.e. stop accepting connections), or
  • stop a select() in progress in another thread (in this case, just calling the destructor is not enough)

Reimplemented in NLNET::CDummyTcpSock.

Referenced by NLNET::CUdpSimSock::close(), NLNET::CListenTask::close(), NLNET::CTcpSock::connect(), and NLNET::sendEmail().

virtual void NLNET::CSock::connect const CInetAddress addr  )  [virtual]
 

Connection. This method does not return a boolean, otherwise a programmer could ignore the result and no exception would be thrown if connection fails :

Reimplemented in NLNET::CDummyTcpSock, and NLNET::CTcpSock.

Referenced by NLNET::CUdpSimSock::connect(), and NLNET::CLoginClient::connectToShard().

bool NLNET::CSock::connected  ) 
 

Returns if the socket is connected (mutexed).

Referenced by NLNET::CCallbackClient::connect(), NLNET::CBufSock::connect(), NLNET::CBufClient::connect(), NLNET::CUdpSimSock::connected(), NLNET::CLoginClient::connectToShard(), NLNET::CBufServer::disconnect(), NLNET::CBufClient::disconnect(), NLNET::CBufSock::pushBuffer(), NLNET::sendEmail(), NLNET::CBufClient::update(), and NLNET::CBufClient::~CBufClient().

void NLNET::CSock::createSocket int  type,
int  protocol
[protected]
 

Creates the socket and get a valid descriptor.

Referenced by NLNET::CListenSock::CListenSock(), NLNET::CTcpSock::connect(), NLNET::CTcpSock::connectWithCustomWindowSize(), and NLNET::CUdpSock::CUdpSock().

bool NLNET::CSock::dataAvailable  ) 
 

Checks if there is some data to receive, waiting (blocking) at most for the time out value.

Referenced by NLNET::CUdpSimSock::dataAvailable(), and NLNET::CClientReceiveTask::run().

SOCKET NLNET::CSock::descriptor  )  const [inline]
 

Returns the socket descriptor.

Definition at line 241 of file sock.h.

References _Sock.

Referenced by NLNET::CBufSock::asString(), and NLNET::CListenTask::run().

00241 { return _Sock; }

std::string NLNET::CSock::errorString uint  errorcode  )  [static]
 

Returns a string explaining the network error (see getLastError()).

uint NLNET::CSock::getLastError  )  [static]
 

Returns the code of the last error that has occured. Note: This code is platform-dependant. On Unix, it is errno; on Windows it is the Winsock error code. See also errorString()

sint32 NLNET::CSock::getSendBufferSize  ) 
 

Gets the send buffer size.

bool NLNET::CSock::initialized  )  [inline, static]
 

Returns true if the network engine is initialized.

Definition at line 261 of file sock.h.

00261 { return CSock::_Initialized; }

void NLNET::CSock::initNetwork  )  [static]
 

Initialize the network engine, if it is not already done.

const CInetAddress& NLNET::CSock::localAddr  )  const [inline]
 

Returns a const reference on the local address.

Definition at line 235 of file sock.h.

References _LocalAddr.

Referenced by NLNET::CLoginServer::init(), NLNET::CUdpSimSock::localAddr(), and NLNET::CListenTask::localAddr().

00235 {       return _LocalAddr; }

bool NLNET::CSock::nonBlockingMode  )  const [inline]
 

Returns the nonblocking mode.

Definition at line 166 of file sock.h.

References _NonBlocking.

00166 { return _NonBlocking; }

CSock::TSockResult NLNET::CSock::receive uint8 buffer,
uint32 len,
bool  throw_exception = true
 

Receive a partial or an entire block of data, depending on nonblocking mode.

In blocking mode: the method waits until 'len' bytes have been received.

In nonblocking mode: the method reads the bytes that have already been received only, and resets 'len' to the number of bytes read. The actual length may be smaller than the demanded length. In no data is available, the return value is CSock::WouldBlock. If dataAvailable() returns true, you are sure that receive() will not return CSock::WouldBlock.

In case of graceful disconnection:

In case of failure (e.g. connection reset by peer) :

  • the return value is CSock::Error or an ESocket exception is thrown. You may want to close the connection manually.

Reimplemented in NLNET::CUdpSock.

Referenced by NLNET::CNonBlockingBufSock::receivePart(), and NLNET::sendEMailCommand().

void NLNET::CSock::releaseNetwork  )  [static]
 

Releases the network engine.

const CInetAddress& NLNET::CSock::remoteAddr  )  const [inline]
 

Returns the address of the remote host.

Definition at line 238 of file sock.h.

References _RemoteAddr.

Referenced by NLNET::CBufServer::hostAddress(), NLNET::CNonBlockingBufSock::receivePart(), NLNET::CBufClient::remoteAddress(), and NLNET::sendEMailCommand().

00238 { return _RemoteAddr; }

CSock::TSockResult NLNET::CSock::send const uint8 buffer,
uint32 len,
bool  throw_exception = true
 

Sends a message.

In blocking mode: the method waits until 'len' bytes have been sent.

In nonblocking mode : the method resets len to the actual number of bytes sent. Even if less bytes than expected have been sent, it returns CSock::Ok. The caller is expected to test the actual len to check if the remaining data must be resent.

Returns:
CSock::Ok or CSock::Error (in case of failure). When throw_exception is true, the method throws an ESocket exception in case of failure.

Referenced by NLNET::CBufSock::flush(), NLNET::sendEMailCommand(), and NLNET::CUdpSimSock::sendUDPNow().

void NLNET::CSock::setLocalAddress  )  [protected]
 

Sets the local address.

Referenced by NLNET::CUdpSock::bind(), and NLNET::CUdpSock::sendTo().

void NLNET::CSock::setNonBlockingMode bool  bm  ) 
 

Sets the socket in nonblocking mode. Call this method *after* connect(), otherwise you will get an "would block" error (10035 on Windows). In nonblocking mode, use received() and sent() instead of receive() and send()

Referenced by NLNET::CNonBlockingBufSock::setNonBlocking().

void NLNET::CSock::setSendBufferSize sint32  size  ) 
 

Sets the send buffer size.

void NLNET::CSock::setTimeOutValue long  sec,
long  ms
[inline]
 

Change the time out value used in getDataAvailable(), which is 0 by default.

Definition at line 143 of file sock.h.

References _TimeoutMs, and _TimeoutS.

Referenced by NLNET::CClientReceiveTask::run().

00144         {
00145                 _TimeoutS = sec;
00146                 _TimeoutMs = ms;
00147         }

uint32 NLNET::CSock::timeOutValue  )  const [inline]
 

Returns the time out value in millisecond.

Definition at line 244 of file sock.h.

References _TimeoutMs, _TimeoutS, and uint32.

00244 { return _TimeoutS*1000 + _TimeoutMs; }


Field Documentation

uint64 NLNET::CSock::_BytesReceived [protected]
 

Number of bytes received on this socket.

Definition at line 300 of file sock.h.

uint64 NLNET::CSock::_BytesSent [protected]
 

Number of bytes sent on this socket.

Definition at line 303 of file sock.h.

volatile bool NLNET::CSock::_Connected [protected]
 

True after calling connect().

Definition at line 297 of file sock.h.

bool NLNET::CSock::_Initialized [static, private]
 

True if the network library has been initialized.

Definition at line 314 of file sock.h.

CInetAddress NLNET::CSock::_LocalAddr [protected]
 

Address of local host (valid if connected).

Definition at line 284 of file sock.h.

Referenced by localAddr().

bool NLNET::CSock::_Logging [protected]
 

If false, do not log any information.

Definition at line 290 of file sock.h.

uint32 NLNET::CSock::_MaxReceiveTime [private]
 

Definition at line 317 of file sock.h.

uint32 NLNET::CSock::_MaxSendTime [private]
 

Definition at line 318 of file sock.h.

bool NLNET::CSock::_NonBlocking [protected]
 

If true, the socket is in nonblocking mode.

Definition at line 293 of file sock.h.

Referenced by nonBlockingMode().

CInetAddress NLNET::CSock::_RemoteAddr [protected]
 

Address of the remote host (valid if connected).

Definition at line 287 of file sock.h.

Referenced by remoteAddr().

SOCKET NLNET::CSock::_Sock [protected]
 

Socket descriptor.

Definition at line 281 of file sock.h.

Referenced by descriptor().

long NLNET::CSock::_TimeoutMs [protected]
 

Secondary time out value (ms) for select in dataAvailable().

Definition at line 309 of file sock.h.

Referenced by setTimeOutValue(), and timeOutValue().

long NLNET::CSock::_TimeoutS [protected]
 

Main time out value (sec) for select in dataAvailable().

Definition at line 306 of file sock.h.

Referenced by setTimeOutValue(), and timeOutValue().


The documentation for this class was generated from the following file:
Generated on Tue Mar 16 14:04:14 2004 for NeL by doxygen 1.3.6