nnl::Socket Class Reference

Socket class. More...

#include <socket.hh>

List of all members.

Public Member Functions

 Socket ()
 Constructor.
 Socket (int fd)
 Constructor.
virtual ~Socket ()
 Destructor.
int Connect (const char *host, int port, unsigned long timeoutMs)
 Requests a new connection to be created to remote endpoint.
void Send (const char *data, size_t len, DeltaMsecTime timeoutMs=1000)
 Requests data to be sent.
void Recv (unsigned long timeoutMs)
 Requests data to be received.
void WaitDataAvailable (unsigned long timeoutMs)
 Request waiting for data to be availabe.
int Close ()
 Closes the socket immediately.
int Listen (const char *host, int port)
 Listens on the port.
int Listen (const char *unixSocketPath)
 Bind local socket and listen on it.
int Accept (unsigned long timeoutMs)
 Requests accepting of connection.
void Sleep (DeltaMsecTime timeoutMs)
 Request sleep.
void SleepAbs (AbsMsecTime timeWakeMs)
 Request sleep.
void SleepAbs0 ()
 Optimized SleepAbs() for immediate wakeup.
int Fd (void) const
 Get the file descriptor.
int Port (void) const
 Get the port.
void Event (short events, short revents, AbsMsecTime currentTime)
 Signalize an event on the socket.
void SendCallBack ()
 Callback after ALL data is sent or error during sending.
void RecvCallBack ()
 Callback after data is received.
void SleepCallBack ()
 Callback after sleep is over.
void Terminate ()
 Request termination.
void InitNonsocket (int fd)
 Perform initialization of non-socket fd.

Protected Types

 S_Ok
 OK.
 S_Error
 Error.
 S_Timeout
 Timeout.
enum  Status { S_Ok, S_Error, S_Timeout }
 Operation status. More...

Protected Member Functions

const std::string & Error (void) const
 Get error.
size_t LenNotSent () const
 Return number of bytes not sent in last Send() operation.
bool IsConnected () const
 Return connected status.
bool CanTerminate ()
 Call whenever termination is possible.

Private Member Functions

void Constructor ()
 Common constructor body.
virtual void ConnectCompleteImpl (int result)
 Callback when connect is complete.
virtual void SendCompleteImpl (Status status)
 Callback when send is complete.
virtual void RecvCompleteImpl (int result, const char *buf, size_t lenRecv)
 Callback when data is received.
virtual void AcceptCompleteImpl (int result, int sock, std::string remote)
 Callback when a new connection is accepted.
virtual void SleepCompleteImpl ()
 Callback after sleep is over.
virtual void TerminateImpl ()
 Termination implementation.
virtual void DataAvailableImpl (Status status)
 Callback after data is available.

Private Attributes

SocketFnPtr mHelperFn [6]
 Dispatch functions.
Impl *const mpImpl
 implementation
int & mrFd
 reference to fd so Fd() op can be inlined
bool & mrIsConnected
 reference to connected flag so IsConnected() op can be inlined

Classes

struct  Impl
 Socket private implementation. More...


Detailed Description

Socket class.

Implements C++ socket - a connection endpoint.

Client should implement the virtual operations:

Server should implement the virtual operations: See also samples directory.


Member Enumeration Documentation

enum nnl::Socket::Status [protected]

Operation status.

Enumerator:
S_Ok  OK.
S_Error  Error.
S_Timeout  Timeout.


Constructor & Destructor Documentation

nnl::Socket::Socket ( int  fd  ) 

Constructor.

Used for connected socket (after accept())

Parameters:
fd file descriptor


Member Function Documentation

int nnl::Socket::Accept ( unsigned long  timeoutMs  ) 

Requests accepting of connection.

Parameters:
timeoutMs timeout of operation in ms
Returns:
0 on success, other value on error
See also:
AcceptCompleteImpl

virtual void nnl::Socket::AcceptCompleteImpl ( int  result,
int  sock,
std::string  remote 
) [private, virtual]

Callback when a new connection is accepted.

Parameters:
result 0 if success, other value if error
sock new socket (connection) file descriptor
remote remote address

bool nnl::Socket::CanTerminate (  )  [protected]

Call whenever termination is possible.

If termination is requested, TerminateImpl() is called. Consider also using TERMINATION_POINT macro below.

Returns:
true if termination was requested and TerminateImpl() was called

int nnl::Socket::Close (  ) 

Closes the socket immediately.

In case the socket is already closed, nothing happens.

Returns:
0 on success, other value on error

int nnl::Socket::Connect ( const char *  host,
int  port,
unsigned long  timeoutMs 
)

Requests a new connection to be created to remote endpoint.

If immediate connect is not possible (would block), a callback is called upon successfull connection.

Parameters:
host remote host
port remote port
timeoutMs connect timeout in ms
Returns:
0 on success, other value on error
See also:
ConnectCompleteImpl

virtual void nnl::Socket::ConnectCompleteImpl ( int  result  )  [private, virtual]

Callback when connect is complete.

Parameters:
result 0 if success, other value if error

virtual void nnl::Socket::DataAvailableImpl ( Status  status  )  [private, virtual]

Callback after data is available.

Callback for WaitDataAvailable(). It signalizes whether data can be read without blocking.

Parameters:
status operation status

const std::string& nnl::Socket::Error ( void   )  const [protected]

Get error.

Get error text.

Returns:
text of error

void nnl::Socket::Event ( short  events,
short  revents,
AbsMsecTime  currentTime 
)

Signalize an event on the socket.

Used by netman, should not be used by user.

Parameters:
events events requested
revents events got
currentTime current time

int nnl::Socket::Fd ( void   )  const [inline]

Get the file descriptor.

Returns:
file descriptor or -1 if none

void nnl::Socket::InitNonsocket ( int  fd  ) 

Perform initialization of non-socket fd.

This op is to be used in case you need to use a file descriptor with NNL which is not a socket, but e.g. an ordinary file.

bool nnl::Socket::IsConnected (  )  const [inline, protected]

Return connected status.

Note the fd can be 'open' (created) even if not connected, so it's safe to call Close() even if not connected.

int nnl::Socket::Listen ( const char *  unixSocketPath  ) 

Bind local socket and listen on it.

Parameters:
unixSocketPath socket name with full path
Returns:
0 on success, other value on error

int nnl::Socket::Listen ( const char *  host,
int  port 
)

Listens on the port.

Parameters:
host host address (ip or name) to listen on.. Will use INADDR_ANY if equals to 0.
port port to listen on
Returns:
0 on success, other value on error

int nnl::Socket::Port ( void   )  const

Get the port.

Returns:
port number

void nnl::Socket::Recv ( unsigned long  timeoutMs  ) 

Requests data to be received.

Tries to receive data. If it can't be done immediately (operation would block) the request is forwarded to netman which will do poll() first and then notify Socket.

Parameters:
timeoutMs timeout of operation in ms
See also:
RecvCompleteImpl

CanRecv

RecvCallBack

void nnl::Socket::RecvCallBack (  ) 

Callback after data is received.

Called by netman in case the data was already received in Recv() function. Just to separate the Recv() and RecvCompleteImpl().

virtual void nnl::Socket::RecvCompleteImpl ( int  result,
const char *  buf,
size_t  lenRecv 
) [private, virtual]

Callback when data is received.

Parameters:
result 0 if success, other value if error
buf buffer where data is stored
lenRecv number of bytes in buffer (received)

void nnl::Socket::Send ( const char *  data,
size_t  len,
DeltaMsecTime  timeoutMs = 1000 
)

Requests data to be sent.

Tries to send data. If not all the data can be sent immediately (operation would block), the request is forwarded to netman which will do poll() first and then notify Socket via CanSend(). If all the data is sent immediately, or error happened, netman calls SendCallBack(). SendCompleteImpl() is called from both callbacks which is implemented by user.

Parameters:
data data to be sent
len length of data
timeoutMs timeout of operation in ms
See also:
SendCompleteImpl

CanSend

SendCallBack

Warning:
The pointer to data to be sent must be valid until SendCompleteImpl is called, because the data is not copied!

void nnl::Socket::SendCallBack (  ) 

Callback after ALL data is sent or error during sending.

Called by netman in case ALL the data was already sent in Send() function or error happened in Send(). Just to separate the Send() and SendCompleteImpl().

virtual void nnl::Socket::SendCompleteImpl ( Status  status  )  [private, virtual]

Callback when send is complete.

Parameters:
status S_Ok if all data was sent, S_Error if error on connection, S_Timeout if timeout while sending
Note:
Some data can be sent even if status is S_Error or S_Timeout. Check the lenNotSent().

void nnl::Socket::Sleep ( DeltaMsecTime  timeoutMs  ) 

Request sleep.

Don't request any operation, just blocking for certain abount of time.

Parameters:
timeoutMs timeout of operation in ms

void nnl::Socket::SleepAbs ( AbsMsecTime  timeWakeMs  ) 

Request sleep.

Same as Sleep() but requests to be awaken at

Parameters:
timeWakeMs. 
timeWakeMs absolute time in ms when awaking is requested

void nnl::Socket::SleepCallBack (  ) 

Callback after sleep is over.

Called by netman in case sleep is over. Just to separate the Sleep() and SleepCompleteImpl().

virtual void nnl::Socket::TerminateImpl (  )  [private, virtual]

Termination implementation.

Client will implement termination here, e.g. delete this if allocated by new.

void nnl::Socket::WaitDataAvailable ( unsigned long  timeoutMs  ) 

Request waiting for data to be availabe.

Polls for data to read. Kind of same as Recv() but does not read the data.


Member Data Documentation

SocketFnPtr nnl::Socket::mHelperFn[6] [private]

Dispatch functions.

Warning:
Depens on the size of Socket::Impl::State


The documentation for this class was generated from the following file:
Generated on Tue Jun 19 10:31:48 2012 for nnl by  doxygen 1.4.7