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
Article moved from Internet section on January 26 1999



Comments

  • How to use it?

    Posted by Banchan on 02/17/2009 12:08am

    Can you send me a sample.

    Reply
  • help

    Posted by Legacy on 04/19/2003 12:00am

    Originally posted by: furqan nadeem

    sir i wana design a net to cell phone messenger sir how this class can help me and can u suggest me more wht we should do to design it cause i m new to vc++
    thank u

    Reply
  • How do you build it ?

    Posted by Legacy on 07/14/2002 12:00am

    Originally posted by: Sid Young


    Could a VC6++ Project and Workspace file be included in future submissions, as now I have to spend then next hour or two working out just how to build it correctly.

    Sid

    Reply
  • but...how to use it in my code?

    Posted by Legacy on 03/25/2002 12:00am

    Originally posted by: TamRhui

    can email the samples to me? thanks.
    

    Reply
  • Would you mail example to me?

    Posted by Legacy on 11/11/2001 12:00am

    Originally posted by: Jockey Yan

    very abstract!
    Because can't go your homepage: http://www.geocities.com/pbarvinko;
    would you like to mail me a copy of exmple? or paste here.

    Reply
  • 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
  • very good class wraper

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

    Originally posted by: Young Sailor

    It is very nice this class wraper. I think it is good example in many aspects, I have two questions about it. this class wraper have no server initialize communication link code. Second, what the determine the connection numbers, if it provide this function it will very strong for pratical use.
    Could you sent the example source code to me?

    Reply
  • Demo Project

    Posted by Legacy on 09/19/2000 12:00am

    Originally posted by: Brandon

    I was wondering if there was a demo project to give me a better understanding of your classes.

    Reply
  • good

    Posted by Legacy on 09/18/2000 12:00am

    Originally posted by: niebo

    Very Good!!!!!!!

    Reply
  • Very impressive!

    Posted by Legacy on 04/02/2000 12:00am

    Originally posted by: Alex

    Paul, its very impressive piece of code. I like it a lot.
    
    Two things can make it even better:
    1. Add security by encoding messages.
    2. Add ID for each message. If connection was lost during
    message delivery, CInOut could try to reconnect
    automatically and then check for lost messages and resend.
    Of course it will mean serialization messages in archives.

    Thank you!

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

Top White Papers and Webcasts

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

  • Live Event Date: November 13, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT APIs can be a great source of competitive advantage. The practice of exposing backend services as APIs has become pervasive, however their use varies widely across companies and industries. Some companies leverage APIs to create internal, operational and development efficiencies, while others use them to drive ancillary revenue channels. Many companies successfully support both public and private programs from the same API by varying levels …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds