class CTcpSock

This class represents an asynchronous TCP socket. More...

Definition#include <sock.h>
List of all Methods
Annotated List
Files
Globals
Hierarchy
Index

Public Methods

Public Members

Protected Types

Protected Methods

Protected Members


Detailed Description

The CTcpSock represents a socket used for asynchronous operation.

When a CTcpSock has been created, it can be registered with an CEventLoop which passes 'events' from a select system call to the socket object, whenever data arrives or the socket is ready to recieve more data for writing.

Handling of the events is implemented as a finite state machine. Callbacks can be registered for whenever the socket changes state. Callback function pointers are stored in the m_cb* member variables.

See also: CEventLoop

 CTcpSock (void)

CTcpSock

Constructor which initializes datamembers.

 ~CTcpSock (void)

~CTcpSock

Destructor which automatically closes the socket if it has been connected.

int  create (void)

create

Creates the socket, i.e. gets a socket handle ( m_sock ).

Returns: zero on success

int  close (void)

close

Closes the socket if it has been connected. It is safe to call close even if the socket wasn't created or connected.

Returns: zero on success

int  connect (SOCKADDR_IN* name)

connect

Start connecting the socket to a remote peer. When the connect completes the m_cbConnectOk callback will be called.

Parameters:
nameaddress (host, port) of the remote peer

Returns: zero or EINPROGRESS on success

int  send (const char* buf, int len)

send

Start sending a buffer to the remote peer. When all bytes have been send the m_cbSendOk callback will be called.

Parameters:
bufpointer to the buffer
lennumber of bytes to send

Returns: number of bytes sent so far

int  sendString (const char* str)

sendString

Start sending a zero terminated string. When the whole string has been sent the m_cbSendOk callback will be called.

Parameters:
strthe string to send

Returns: number of bytes sent so far

int  recvBuf (char* buf, int len)

recvBuf

Start receiving a buffer. When the desired number of bytes have been read or the remote end has closed the socket, the m_cbRecvBufOk callback will be called.

Parameters:
bufpointer to buffer for receiving data
lennumber of bytes to receive

Returns: zero on success

int  readLine (char* buf, int max_len)

readLine

Start reading a line from the socket. When a full line have been read or the remote end has closed the socket, the m_cbReadLineOk callback will be called.

Parameters:
bufpointer to buffer for receiving data
max_lenmaximum number of bytes to read.

Returns: zero on success

int  setFdSets (fd_set* rfds, fd_set* wfds, fd_set* efds)

setFdSets

Sets fd_sets for this socket depending on it's state. The fd_sets are used by a CEventLoop for the select system call. Basically this socket indicates to the EventLoop if it is currently interested in read, write or exception events.

Parameters:
rfdsread fd_set
wfdswrite fd_set
efdsexception fd_set

Returns: zero on success

int  checkFdSets (fd_set* rfds, fd_set* wfds, fd_set* efds)

checkFdSets

Checks fd_sets for this socket after the CEventLoop returns from the select system call, to see if there was any events for this socket. If an event arrived processEvent is called. Before returning to the EventLoop any new internal events are handled by calling the appropriate callback.

Parameters:
rfdsread fd_set
wfdswrite fd_set
efdsexception fd_set

Returns: zero on success

void  deleteSock (void)

deleteSock

Ask the socket to delete itself, just before returning to the EventLoop. This allows for 'nested' callbacks that needs to delete the socket object.

SOCKET m_sock

m_sock

The socket handle (i.e. file descriptor on UNIX)

void* m_context

m_context

This is a pointer to some arbitrary context you can assign to the socket. This is useful if you have one set of callback functions which handle multiple sockets. They can use the context data to figure out where the particular socket 'belong'

TCallBack* m_cbConnectOk

m_cbConnectOk

Callback for when the socket has succesfully connected to the remote peer.

TCallBack* m_cbSendOk

m_cbSendOk

Callback for when a send or sendString has completed.

TCallBack* m_cbReadLineOk

m_cbReadLineOk

Callback for when a line has been read from the socket.

TCallBack* m_cbRecvBufOk

m_cbRecvBufOk

Callback for when a buffer has been received.

const char* m_wbuf

m_wbuf

Pointer to the buffer being written ( send , sendString ).

int m_wlen

m_wlen

Number of bytes to be sent.

int m_sent

m_sent

Number of bytes actually sent (so far).

char* m_rbuf

m_rbuf

Pointer to the buffer being read to ( readLine , recvBuf ).

int m_rlen

m_rlen

Number of bytes to be read

int m_read

m_read

Number of bytes actually read (so far).

int m_eof

m_eof

Non-zero if the socket has been closed at the remote end.

enum ESockStates {start, created, connecting, ready, sending, recievingBuf, readingLine, closed }

ESockStates

[protected: ]

The different states of the socket

ESockStates m_state

m_state

[protected: ]

enum EEvents {read = 1, write = 2, except = 4 }

EEvents

[protected: ]

Types of events received from the EventLoop

enum ESockInternalEvents {noEvent, connectOk, sendOk, readLineOk, recvBufOk, deleteEvent }

ESockInternalEvents

[protected: ]

Internal events used to do callbacks and delete the socket before returning to the EventLoop, if needed.

ESockInternalEvents m_intStatus

m_intStatus

[protected: ]

int  processEvent (int event)

processEvent

[protected: ]

Processes an event received from the EventLoop. Takes the appropriate action depending on the current state ( m_state ).

Parameters:
eventtype of event: read, write, exception

Returns: zero on success

void  readLineLoop (void)

readLineLoop

[protected: ]

Called every time data arrives, when reading a line

void  recvBufLoop (void)

recvBufLoop

[protected: ]

Called every time data arrives, when reading to a buffer