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

  • Complex hybrid environments can make it difficult to track interdependencies, increasing the risk of disrupting critical business services. In this white paper by EMA, you'll learn how application discovery and dependency mapping can help you: Meet granular targets for availability, cost, and time-to-revenue for cloud services. Accelerate mean time to repair (MTTR) while communicating better with stakeholders. Manage even the most complex hybrid environments more efficiently and effectively Understand the …

  • Following an IT incident through to resolution is more than just acknowledging an alert and restarting a server. The recent State of On-Call Report found that it takes most companies an average of 10-30 minutes to resolve an incident with an average of 5 people involved. Imagine how much money and time companies are spending to deal with incident resolution. But what if you had a tool that offered solutions baked in? Or a tool that sent alerts to the right person or team every time? These are the kind of …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date