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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds