Asynchronous full-duplex connection class

.

CInOutConnection is a class that encapsulates Client/Server connection node. It works in asynchronous full-duplex mode. After initialization it creates four threads: receiving, sending, working and service threads. First two are obvious, working thread waits for a message to be posted into input queue and then processes it and service thread could break connection on inactivity timeout or could be used for performing idle periodic tasks.

For keeping input and output queues in multithreaded environment I created CMessageQueue, which is simply wrapper under class from STL. The main difference is that before inserting or removing messages from the queue one should call CMessageQueue::BlockQueue() and then CMessageQueue::UnBlockQueue() in order to get exclusive access to the queue. For single message it could be done via CMessageQueue::GetFirstMessage() or CMessageQueue::AddSingleMessage() functions. To determine if there are messages in the queue one should call CMessageQueue::IsQueueFull().

Class termination.

To initiate connection termination, function CInOutConnection::SetTerminate() should be called. To check if its time to terminate, application could call CInOutConnection::Terminate() function.

Message exchange.

To be independent of network providers class exposes two virtual functions: cioReceiveMessage(int* Error, int* length) and cioSendMessage(void* msg, int msg_size). They need to be overwritten in derived class for work with specific network providers. Depending on result of these functions network connection could be considered as broken and shutdown could be initiated.

After message was successfully received it will be stick into input queue. Working thread, that checks for messages in input queue will be triggered automatically and will extract message from queue for processing. Currently there are three ways of message processing:

1. Application can register callback function within the class and this function will be called for every incoming message. Callback function is defined like this: typedef void (InOutCallbackFunc)(void* param), where param is pointer to this of the CInOutConnection class.

2. Application can register callback window within the class. In case of message arrival WN_COMMAND message will be sent to window. LParam in this case will be set to CInOutConnection::this and WParam will point to user-registered message Id.

3. If there is no callback function and callback window registered, CInOutConnection::ProcessCurrentMessage(void* msg, DWORD length) will be called. Application can overwrite this function or CInOutConnection::ProcessMessage() to gain specific functionality for message processing.

Message exchange protocol.

CinOutConnection class provides basic functionality for message exchange protocol support. On receiving stage several functions are called to ensure, that incoming message is not garbage and has some useful information:).

They are:

		virtual int GetHeaderLength();
		virtual BOOL IsCorrectHeader(char* header);
		virtual int GetMessageSizeFromHeader(char* header);

Message receiving works as follows:

Receive function calls GetHeaderLength() first to know the length of the initial packet of data (header). Then it passes received header to IsCorrectHeader() and GetMessageSizeFromHeader() respectively to find out, if message is valid one and to find out the size of the actual message. Then it tries to receive message of the length, returned by GetMessageSizeFromHeader() function.

Function PrepareMessageToSend(void* msg, DWORD length, DWORD& len) is used for preparation on message to be sent. It can add header to the message, convert it to uuencode format or perfom any action, needed by application.

Thread behavior could be controlled by overriding InitXXXThread(), XXXThreadTerminated(), OnXXXThreadError() and OnXXXThreadIdle() functions.

CInOutConnection::OnServiceTimer() could be used to perform background periodical tasks. It is called every second from Service Thread. (Be aware of using shared resources!).

CTcpIpConnection class.

CtcpIpConnection is class derived from CinOutConnection, modified to work with Win32 TCP/IP networking. You need couple of connected sockets (one for receiving and one for transmission) to initialize class. CioReceiveMessage and cioSendMessage adjusted to work properly with sockets, as well as ResetConnection() and SetTerminate() functions.

Node creation from application point of view:

To create connected node, following tasks should be performed:

1. Derive class from CinOutConnection to implement specific network provider functionality and message transmission format. (You can use CtcpIpConnection for TCP/IP Windows sockets).

2. Initialize connection. (for CtcpIpConnection: Get couple of connected sockets and call InitConnection function)

3. !!! Thread, that initialized CinOutConnection creation, must wait for it to finish on threads exit. In other words this thread MUST call WaitForTermination() class function on its exit.

Download source

Article updated on June 21 1998



Comments

  • How can I use this class to FTP file?(CTcpIpConnection)

    Posted by Legacy on 11/23/2000 12:00am

    Originally posted by: dingsg


    How can I use this class to FTP file?(CTcpIpConnection)

    Reply
  • Home page for this class

    Posted by Legacy on 04/21/1999 12:00am

    Originally posted by: Pavlo Barvinko

    Hello Everybody!
    You can find examples, bug fixes and comments for this class at

    http://www.geocities.com/SiliconValley/Network/8827/

    Good luck!

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As everyone scrambles to protect customers and consumers from the Heartbleed virus, there will be a variety of mitigating solutions offered up to address this pesky bug. There are a variety of points within the data path where solutions could be put into place to mitigate this (and similar) vulnerabilities and customers must choose the most strategic point in the network at which to deploy their selected mitigation. Read this white paper to learn the ins and outs of mitigating the risk of Heartbleed and the …

  • Live Event Date: September 19, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT In response to the rising number of data breaches and the regulatory and legal impact that can occur as a result of these incidents, leading analysts at Forrester Research have developed five important design principles that will help security professionals reduce their attack surface and mitigate vulnerabilities. Check out this upcoming eSeminar and join Chris Sherman of Forrester Research to learn how to deal with the influx of new device …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds