CNetworkQueue Class Reference

Encapsulates a Networkqueue in a object. More...

#include <NQclass.h>

Collaboration diagram for CNetworkQueue:

Collaboration graph
[legend]

List of all members.

Public Member Functions

 CNetworkQueue ()
 constructor of the NetworkQueue object
 ~CNetworkQueue ()
 destructor of the NetworkQueue object
CNQMessagecreateMsg ()
 create a valid CNQMessage object reference
bool freeMsg (CNQMessage *ref)
 free a CNQMessage reference which has previously been created or returned this CNQMessage instance might be returned at CNetworkQueue::createMsg, CNetworkQueue::recvMsg() and CNetworkQueue::sendRecvMsg()
bool addPeer (UINT32 ip_hostorder, UINT32 port_hostorder)
 Add a new NetworkQueue endpoint to the accept list.
bool removePeer (UINT32 ip_hostorder, UINT32 port_hostorder)
 Remove a NetworkQueue endpoint from the accept list.
bool create (const ONECHAR *ip_remote_peer, UINT16 portnumber_remote, UINT16 portnumber_local)
 create and connect the Networkqueue to a remote peer
bool close ()
 closes the Networkqueue and deinitializes peer connection
UINT32 getLastError ()
 get the last error code
bool getLastErrorString (ONECHAR *buffer, UINT32 buflength)
 Get the error string of the last occured error code.
void SendProcessTask ()
 public only by software design reason
void ReceiveProcessTask ()
 public only by software design reason
send and receive API
bool sendMsgTo (CNQMessage *msg, UINT32 ip_hostorder, UINT16 port_hostorder)
 Send a message from local Networkqueue immediately to remote peer.
bool sendMsgTo (CNQMessage *msg, UINT32 ip_hostorder, UINT16 port_hostorder, const NQ_TIMEVAL &timeout)
 Send a message from Networkqueue with timeout.
bool recvMsg (CNQMessage **newmsg)
 Receive/wait for next message from Networkqueue.
bool recvMsg (CNQMessage **newmsg, const NQ_TIMEVAL &timeout)
 Receive next message from Networkqueue and give wait timeout.
bool sendRecvMsg (CNQMessage **response, CNQMessage *request, UINT32 ip_hostorder, UINT16 port_hostorder, const NQ_TIMEVAL &timeout)
 Send and receive a message from Networkqueue with timeout (rpc-like call).
bool getStatistics (NQ_STATISTICS &stats)
 get statistical information about the NetworkQueue

Private Member Functions

bool sendMsgToInternal (UINT32 ip_hostorder, UINT16 port_hostorder, CNQMessage *msg, HANDLE recv_notify_sem)
 Internal message validation check.
CNQMessageobtainReference ()
bool incrementReferenceCount (CNQMessage *ref)
bool putbackReference (CNQMessage *ref)

Private Attributes

CNQMessagePool bufs
NQ_STATISTICS m_stats
socktype m_socket
UINT32 m_iLastError
INT32 m_iLastSystemError
HANDLE m_sendSemaphore
HANDLE m_receiveSemaphore
CNQPacketQueue m_SendPacketQueue
CNQPacketQueue m_ReceivePacketQueue
CNQPacketQueue m_AckPacketWaitingQueue
CNQPacketQueue m_ReceiveResponseSemaphorePacketQueue
CNQResponseWaitQueue m_ResponseWaitQueue
CNQPeerList m_PeerList
bool m_bSuccessfulInitialized
HANDLE m_hSendThread
HANDLE m_hReceiveThread
volatile bool m_bShutdownSendThread
volatile bool m_bSendThreadRunning
volatile bool m_bShutdownReceiveThread
volatile bool m_bReceiveThreadRunning
UINT16 m_iNextRequestId

Friends

class CNQMessage
class CNQMessageBuffers


Detailed Description

Encapsulates a Networkqueue in a object.

creates a Networkqueue endpoint for communication with a nother Networkqueue endpoint.

Definition at line 8 of file NQclass.h.


Constructor & Destructor Documentation

CNetworkQueue::CNetworkQueue (  ) 

constructor of the NetworkQueue object

Definition at line 56 of file NQclass.cpp.

Here is the call graph for this function:

CNetworkQueue::~CNetworkQueue (  ) 

destructor of the NetworkQueue object

Definition at line 73 of file NQclass.cpp.

Here is the call graph for this function:


Member Function Documentation

CNQMessage * CNetworkQueue::createMsg (  ) 

create a valid CNQMessage object reference

The CNQMessage object reference must be freed by CNetworkQueue::freeMsg. You must use only this function, to create valid CNQMessage objects, because CNQMessage object references are handled internaly in a dedicated memory area, accessable by the asynchronous thread operations.

Returns:
valid Reference to a CNQMessage object or NULL
See also:
freeMsg
Note:
IMPORTANT In case of successful return (!= NULL), the user must not forget to put the borrowed CNQMessage buffer, that was given to the user on return, back to the CNQMessage buffer pool via CNetworkQueue::freeMsg() when the buffer is not accessed/used any more.

Definition at line 94 of file NQclass.cpp.

Referenced by SimulateLocalPeer().

Here is the call graph for this function:

Here is the caller graph for this function:

bool CNetworkQueue::freeMsg ( CNQMessage ref  ) 

free a CNQMessage reference which has previously been created or returned this CNQMessage instance might be returned at CNetworkQueue::createMsg, CNetworkQueue::recvMsg() and CNetworkQueue::sendRecvMsg()

Definition at line 99 of file NQclass.cpp.

Referenced by SimulateLocalPeer(), and SimulateRemotePeer().

Here is the call graph for this function:

Here is the caller graph for this function:

bool CNetworkQueue::addPeer ( UINT32  ip_hostorder,
UINT32  port_hostorder 
)

Add a new NetworkQueue endpoint to the accept list.

add a new NetworkQueue endpoint to the accept list.

Parameters:
ip_hostorder The IP address (in host byte order)
port_hostorder The Port number (in host byte order)
This method adds a remote peer (identified by (ip, port) combination) to the list of allowed remote peers. After this was done successfully, messages can be sent and received from the remote peer.

Parameters:
ip_hostorder ip address of the peer in host byte order
port_hostorder port of the peer in host byte order
Return values:
true peer is added or was already in list
false peer was not added to list (reason: list full) OR mutex locking error (synchronized access); in debug mode an assert is thrown there
See also:
removePeer

Definition at line 144 of file NQclass.cpp.

Referenced by SimulateRemotePeer().

Here is the call graph for this function:

Here is the caller graph for this function:

bool CNetworkQueue::removePeer ( UINT32  ip_hostorder,
UINT32  port_hostorder 
)

Remove a NetworkQueue endpoint from the accept list.

remove a NetworkQueue endpoint from the accept list.

Parameters:
ip_hostorder The IP address (in host byte order)
port_hostorder The Port number (in host byte order)
This method removes a remote peer (identified by (ip, port) combination) from the list of allowed remote peers. After this was done successfully, messages can NOT be sent and received from the remote peer any more.

Parameters:
ip_hostorder ip address of the peer in host byte order
port_hostorder port of the peer in host byte order
Return values:
true peer was removed from the list
false peer was not removed from the list (reason: peer is not in list) OR mutex locking error (synchronized access); in debug mode an assert is thrown there
See also:
addPeer

Definition at line 169 of file NQclass.cpp.

Referenced by SimulateRemotePeer().

Here is the call graph for this function:

Here is the caller graph for this function:

bool CNetworkQueue::create ( const ONECHAR ip_remote_peer,
UINT16  portnumber_remote,
UINT16  portnumber_local 
)

create and connect the Networkqueue to a remote peer

create and connect the Networkqueue to a remote peer

Parameters:
ip_remote_peer The remote peer IP address as a string
portnumber_remote The remote peer port number to send to (in host byte order)
portnumber_local The local port to send from (in host byte order)
This method is mandatory to call successfully before making use of the Network Queue. Internal structures (socket, queues, task/threads, semaphores) are initialized.

Parameters:
ip_remote_peer string dotted IP address of the remote peer (e.g. "192.168.1.11") RegisterPeer will be done internally NULL, "": no peer by default, peers have to be added by hand by calling bool RegisterPeer(ip, port)
portnumber_remote UDP port number of the remote, will be ignored in case of ip_remote_peer==NULL || ""
portnumber_local UDP port number of the local peer side
Return values:
true successfully initialized local queue part, communication may take place now
false something happened during initialization, extended error information can be obtained by CNetworkQueue::getLastError()
The extended error can be one of the following:

See also:
Network Queue Error Codes

Definition at line 213 of file NQclass.cpp.

Referenced by SimulateLocalPeer(), and SimulateRemotePeer().

Here is the call graph for this function:

Here is the caller graph for this function:

bool CNetworkQueue::close (  ) 

closes the Networkqueue and deinitializes peer connection

The method is mandatory to call before exiting the user application. Internal structures (socket, queues, task/threads, semaphores) are shut down, closed and freed.

Preconditions: This method assumes that there will be no more sending/receiving, all loops, events, functions which might be triggered by the send and receive semaphores must have been ended / cancelled.

Return values:
true successfully deinitialized local queue part
false something happened during deinitialization, extended error information can be obtained by CNetworkQueue::getLastError()
Note:
On false return, the deinitialization process was aborted and not finished! There might be leftovers like still initialized semaphores, tasks/threads not properly shut down and the UDP socket still alive.
The extended error can be one of the following:

See also:
Network Queue Error Codes

Definition at line 371 of file NQclass.cpp.

Referenced by SimulateLocalPeer(), SimulateRemotePeer(), and ~CNetworkQueue().

Here is the call graph for this function:

Here is the caller graph for this function:

bool CNetworkQueue::sendMsgTo ( CNQMessage msg,
UINT32  ip_hostorder,
UINT16  port_hostorder 
)

Send a message from local Networkqueue immediately to remote peer.

Definition at line 482 of file NQclass.cpp.

Referenced by SimulateLocalPeer(), and SimulateRemotePeer().

Here is the call graph for this function:

Here is the caller graph for this function:

bool CNetworkQueue::sendMsgTo ( CNQMessage msg,
UINT32  ip_hostorder,
UINT16  port_hostorder,
const NQ_TIMEVAL timeout 
)

Send a message from Networkqueue with timeout.

Send a message from Networkqueue.

Parameters:
msg reference to the CNQMessage object to send
ip_hostorder the destination peer IP address (in host byte order)
port_hostorder the destination peer port address (in host byte order)
timeout timeout to wait max. for the next message
Returns:
true, if a message was successfully send
This method is an important part of the user API. A constructed message can be sent out to the remote peer with the help of this method. Reception is guaranteed by awaiting of a special 'acknowledge' packet. The message is checked for validity and after that injected in the send queue. The send thread/task is notified by the help of a semaphore that a new message is waiting to be send out. On successful reception inside user part at the remote peer an 'ACK' packet is send there. This function will return true in case the sent ack packet was received at local peer.

Parameters:
msg pointer reference to an already set up CNQMessage buffer
ip_hostorder ip address of remote peer to send packet to
port_hostorder port of remote peer to send packet to
timeout timeout to wait until ack packet must have been arrived
Return values:
true message was successfully received at remote side! (and ack packet successfully travelled back to local peer)
false timeout or something bad happened On false return, extended error information can be obtained by getLastError() and getLastErrorString().
The extended error can be one of the following:

See also:
Network Queue Error Codes

Definition at line 531 of file NQclass.cpp.

Here is the call graph for this function:

bool CNetworkQueue::recvMsg ( CNQMessage **  newmsg  ) 

Receive/wait for next message from Networkqueue.

Parameters:
newmsg pointer variable to get the CNQMessage reference of the next received message
Returns:
true, if a message was received
Attention:
You must free the CNQMessage Object with CNetworkQueue::freeMsg after using it!
The method is an important part of the user API. It is an asynchronous method with instant return. In case of true return, a new message was waiting in the receive queue and was put to newmsg reference pointer. In case of false return, there was no new message waiting or an error happened during extraction. In case of true return and a CNQMessage that wants to be acknowledged, an ack message is sent back to remote peer internally and automatically.

Parameters:
newmsg A pointer reference to a (CNQMessage *) variable, which can be uninitialized. The pointer reference will be overwritten!
Return values:
true message was successfully extracted and put into the (CNQMessage *) reference.
false something happened, no message was put into the input/output parameter. Most probably simply no new message was waiting to be received. On false return, extended error information can be obtained by getLastError() and getLastErrorString().
The extended error can be one of the following:

Note:
IMPORTANT In case of return true , the user must not forget to put the borrowed CNQMessage buffer, that was given to the user in newmsg, back to the CNQMessage buffer pool via freeMsg() when the buffer is not accessed/used any more.
See also:
Network Queue Error Codes

Definition at line 735 of file NQclass.cpp.

Referenced by SimulateLocalPeer(), and SimulateRemotePeer().

Here is the call graph for this function:

Here is the caller graph for this function:

bool CNetworkQueue::recvMsg ( CNQMessage **  newmsg,
const NQ_TIMEVAL timeout 
)

Receive next message from Networkqueue and give wait timeout.

Receive/wait for next message from Networkqueue.

Parameters:
newmsg pointer variable to get the CNQMessage reference of the next received message
timeout timeout to wait max. for the next message
Returns:
true, if a message was received
Attention:
You must free the CNQMessage Object with CNetworkQueue::freeMsg after using it!
This method is an important part of the user API. It is an synchronous method with TIMEOUT. In case of true return, a new message was waiting in the receive queue and was put to newmsg reference pointer inside the timeout period. In case of false return, there was no new message waiting until the timeout elapsed or an error happened during extraction out of the receive queue. In case of true return and a CNQMessage that wants to be acknowledged, an ack message is sent back to remote peer internally and automatically.

Parameters:
newmsg A pointer reference to a (CNQMessage *) variable, which can be uninitialized. The pointer reference will be overwritten!
timeout platform-independent version of a usual "timeval" structure passing a timeout of 0 (0 seconds, 0 microseconds) means INFINITE timeout
Return values:
true message was successfully extracted and put into the (CNQMessage *) reference.
false Some error occured or no message was put into the input/output parameter. Most probably simply no new message was waiting to be received during the timeout period. On false return, extended error information can be obtained by getLastError() and getLastErrorString().
The extended error can be one of the following:

Note:
IMPORTANT In case of true return, the user must not forget to put the borrowed CNQMessage buffer, that was given to the user in newmsg, back to the CNQMessage buffer pool via freeMsg() when the buffer is not accessed/used any more.
See also:
Network Queue Error Codes

Definition at line 827 of file NQclass.cpp.

Here is the call graph for this function:

bool CNetworkQueue::sendRecvMsg ( CNQMessage **  response,
CNQMessage request,
UINT32  ip_hostorder,
UINT16  port_hostorder,
const NQ_TIMEVAL timeout 
)

Send and receive a message from Networkqueue with timeout (rpc-like call).

Parameters:
response pointer variable to get the response CNQMessage reference
request reference reference to the CNQMessage object to send for the request
ip_hostorder the destination peer IP address (in host byte order)
port_hostorder the destination peer port address (in host byte order)
timeout timeout to wait max. for the next message
Returns:
true, if a message was received
Attention:
You must free the CNQMessage Object with CNetworkQueue::freeMsg after using it!
This method is an important part of the user API. It is an synchronous RPC-call method with TIMEOUT. A request is send out to the remote peer and the method blocks, until either an appropriate answer arrives or a timeout period expires. In case of true return, a proper response was waiting in the receive queue and was put to response reference pointer inside the timeout period. In case of false return, there was no appropriate answer message waiting until the timeout elapsed. It might be also an error happened during sending / receiving.

Parameters:
response A pointer reference to a (CNQMessage *) variable, which can be uninitialized. The pointer reference will be overwritten!
request A pointer reference to a CNQMessage that contains a request. The request CNQMessage will be altered during transaction (adding/altering of semaphore).
ip_hostorder IP address where to send the packet to in host byte order
port_hostorder UDP port where to send the packet to (at ip) in host byte order
timeout platform-independent version of a usual "timeval" timeout structure passing a timeout of 0 (0 seconds, 0 microseconds) means INFINITE timeout
Return values:
true message was successfully extracted and put into the (CNQMessage *) reference.
false something happened, no message was put into the input/output parameter. Most probably simply no new message was waiting to be received during the timeout period. On false return, extended error information can be obtained by getLastError() and getLastErrorString().
The extended error can be one of the following:

Definition at line 932 of file NQclass.cpp.

Referenced by SimulateLocalPeer().

Here is the call graph for this function:

Here is the caller graph for this function:

bool CNetworkQueue::getStatistics ( NQ_STATISTICS stats  ) 

get statistical information about the NetworkQueue

Parameters:
stats pointer to a STATISTICS structure to retrieve the stastical information
Returns:
true, if the statistical information was retrieved successfully
This simple public method can be used to retrieve current accumulated statistics It returns true in any case in this implementation. The 'stats' parameter is filled with the data set.

Parameters:
stats pointer to a STATISTICS structure to retrieve the stastical information
Return values:
true statistical information was retrieved successfully
false error occured, extended error information can be obtained by getLastError() and getLastErrorString().
See also:
Network Queue Error Codes

Definition at line 1039 of file NQclass.cpp.

Referenced by SimulateLocalPeer(), and SimulateRemotePeer().

Here is the caller graph for this function:

UINT32 CNetworkQueue::getLastError (  ) 

get the last error code

This public method can be used to get to know the last extended error code that happened inside.

Definition at line 1053 of file NQclass.cpp.

Referenced by SimulateLocalPeer(), and SimulateRemotePeer().

Here is the caller graph for this function:

bool CNetworkQueue::getLastErrorString ( ONECHAR buffer,
UINT32  buflength 
)

Get the error string of the last occured error code.

This function is a translator. It converts the abstract error code into a descriptive string that is aimed to give human readable output what error happened (and maybe why). The last error that happened (as returned by getLastError()) is used for translation.

Parameters:
buffer pointer to a ONECHAR array to hold the resulting string
buflength number of characters in the ONECHAR array buffer
Return values:
true successfully translated, encoded and returned
false error happened during translation or encoding (i.e. bad lasterror, buffer too small)
The error handling is only done by asserts. In case the buffer is too small, false is returned! Based on experience I recommend to use more than 120 chars for safety.

In case the translation failed, the returned string will be "No error or unknown error?!".

Definition at line 1075 of file NQclass.cpp.

void CNetworkQueue::SendProcessTask (  ) 

public only by software design reason

This member function is called internally by the sender thread/task and performs NetworkQueue send logic:

Definition at line 1160 of file NQclass.cpp.

Here is the call graph for this function:

void CNetworkQueue::ReceiveProcessTask (  ) 

public only by software design reason

This member function is called internally by the receiver thread/task and performs NetworkQueue receive logic.

Definition at line 1308 of file NQclass.cpp.

Here is the call graph for this function:

bool CNetworkQueue::sendMsgToInternal ( UINT32  ip_hostorder,
UINT16  port_hostorder,
CNQMessage msg,
HANDLE  recv_notify_sem 
) [private]

Internal message validation check.

The message is checked for validity and after that injected in the send queue. The send thread/task is notified by the help of a semaphore that a new message is waiting to be send out.

Parameters:
msg pointer reference to an already set up CNQMessage buffer
ip_hostorder ip address of remote peer to send packet to
port_hostorder port of remote peer to send packet to
recv_notify_sem semaphore to notify in case an "answer" message is received to that packet this parameter should be NO_SEMAPHORE in any other case
Return values:
true message was successfully injected and marked for sending (does not mean that is was successfully send out!)
false something bad happened, message will not be sent out at all On false return, extended error information can be obtained by getLastError() and getLastErrorString().
The extended error can be one of the following:

See also:
Network Queue Error Codes

Definition at line 659 of file NQclass.cpp.

Referenced by recvMsg(), sendMsgTo(), and sendRecvMsg().

Here is the call graph for this function:

Here is the caller graph for this function:

CNQMessage * CNetworkQueue::obtainReference (  )  [private]

Definition at line 106 of file NQclass.cpp.

Referenced by createMsg(), ReceiveProcessTask(), and recvMsg().

Here is the call graph for this function:

Here is the caller graph for this function:

bool CNetworkQueue::incrementReferenceCount ( CNQMessage ref  )  [private]

Definition at line 113 of file NQclass.cpp.

Referenced by ReceiveProcessTask(), sendMsgTo(), and sendMsgToInternal().

Here is the call graph for this function:

Here is the caller graph for this function:

bool CNetworkQueue::putbackReference ( CNQMessage ref  )  [private]

Definition at line 121 of file NQclass.cpp.

Referenced by close(), freeMsg(), ReceiveProcessTask(), recvMsg(), sendMsgTo(), sendMsgToInternal(), and SendProcessTask().

Here is the call graph for this function:

Here is the caller graph for this function:


Friends And Related Function Documentation

friend class CNQMessage [friend]

Definition at line 10 of file NQclass.h.

friend class CNQMessageBuffers [friend]

Definition at line 11 of file NQclass.h.


Member Data Documentation

Definition at line 147 of file NQclass.h.

Referenced by close(), create(), incrementReferenceCount(), obtainReference(), and putbackReference().

Definition at line 152 of file NQclass.h.

Referenced by close(), CNetworkQueue(), create(), ReceiveProcessTask(), and SendProcessTask().

Definition at line 154 of file NQclass.h.

Referenced by close(), CNetworkQueue(), create(), and getLastErrorString().

Definition at line 157 of file NQclass.h.

Referenced by close(), create(), sendMsgTo(), sendMsgToInternal(), and SendProcessTask().

Definition at line 158 of file NQclass.h.

Referenced by close(), create(), ReceiveProcessTask(), and recvMsg().

Definition at line 161 of file NQclass.h.

Referenced by close(), CNetworkQueue(), sendMsgTo(), sendMsgToInternal(), and SendProcessTask().

Definition at line 162 of file NQclass.h.

Referenced by close(), CNetworkQueue(), ReceiveProcessTask(), and recvMsg().

Definition at line 165 of file NQclass.h.

Referenced by CNetworkQueue(), ReceiveProcessTask(), and sendMsgTo().

Definition at line 167 of file NQclass.h.

Referenced by close(), ReceiveProcessTask(), and sendRecvMsg().

Definition at line 168 of file NQclass.h.

Referenced by ReceiveProcessTask(), and sendRecvMsg().

HANDLE CNetworkQueue::m_hSendThread [private]

Definition at line 176 of file NQclass.h.

Referenced by close(), and create().

Definition at line 177 of file NQclass.h.

Referenced by close(), and create().

volatile bool CNetworkQueue::m_bShutdownSendThread [private]

Definition at line 179 of file NQclass.h.

Referenced by close(), create(), and SendProcessTask().

volatile bool CNetworkQueue::m_bSendThreadRunning [private]

Definition at line 180 of file NQclass.h.

Referenced by close(), create(), and SendProcessTask().

volatile bool CNetworkQueue::m_bShutdownReceiveThread [private]

Definition at line 182 of file NQclass.h.

Referenced by close(), create(), and ReceiveProcessTask().

volatile bool CNetworkQueue::m_bReceiveThreadRunning [private]

Definition at line 183 of file NQclass.h.

Referenced by close(), create(), and ReceiveProcessTask().

Definition at line 186 of file NQclass.h.

Referenced by create(), sendMsgTo(), and sendRecvMsg().


The documentation for this class was generated from the following files:

Generated on Tue Apr 22 18:19:58 2008 for NetworkQueue by  doxygen 1.5.5