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 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:
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.
Article updated on June 21 1998
virtual int GetHeaderLength();
virtual BOOL IsCorrectHeader(char* header);
virtual int GetMessageSizeFromHeader(char* header);
Article moved from Internet section on January 26 1999

Comments
How to use it?
Posted by Banchan on 02/17/2009 12:08amCan you send me a sample.
Replyhelp
Posted by Legacy on 04/19/2003 12:00amOriginally 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++
Replythank u
How do you build it ?
Posted by Legacy on 07/14/2002 12:00amOriginally 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
Replybut...how to use it in my code?
Posted by Legacy on 03/25/2002 12:00amOriginally posted by: TamRhui
ReplyWould you mail example to me?
Posted by Legacy on 11/11/2001 12:00amOriginally posted by: Jockey Yan
very abstract!
ReplyBecause can't go your homepage: http://www.geocities.com/pbarvinko;
would you like to mail me a copy of exmple? or paste here.
How can I use this class to FTP file?(CTcpIpConnection)
Posted by Legacy on 11/23/2000 12:00amOriginally posted by: dingsg
How can I use this class to FTP file?(CTcpIpConnection)
Replyvery good class wraper
Posted by Legacy on 10/11/2000 12:00amOriginally 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.
ReplyCould you sent the example source code to me?
Demo Project
Posted by Legacy on 09/19/2000 12:00amOriginally posted by: Brandon
I was wondering if there was a demo project to give me a better understanding of your classes.
Replygood
Posted by Legacy on 09/18/2000 12:00amOriginally posted by: niebo
Very Good!!!!!!!
ReplyVery impressive!
Posted by Legacy on 04/02/2000 12:00amOriginally posted by: Alex
Reply