TcpMsgConnection

A TcpMsgConnection manages both the reading and writing of messages on a connected stream oriented socket using both a MsgWriter and MsgReader.

The TcpMsgConnection makes calls on an object that implements interface ITcpMsgHandler to obtain the messages to be sent and forward on the messages that are received.


TcpMsgConnection* CreateTcpMsgConnection(
    Iocp* iocp,
    boost::asio::ip::tcp::socket* s,
    ITcpMsgHandler* handler,
    const MessageReaderSettings& readerSettings,
    const MessageWriterSettings& writerSettings)

Create a TcpMsgConnection which can be used to read/write messages using the given socket and Iocp. The given ITcpMsgHandler object is used to read and write messages when requested to do so.

It is assumed the given socket has already been associated with 'iocp' which is the case for connected sockets obtained from TcpClient or TcpServer.

The MessageReaderSettings and MessageWriterSettings can affect performance. These setttings cannot be changed during the life of the connection.

Reading doesn't begin until StartReading() is called on the returned TcpMsgConnection. Writing doesn't begin until NotifyMoreMessagesToWrite() is called on the returned TcpMsgConnection.

Every call to CreateTcpMsgConnection() must be paired with a call to Close(TcpMsgConnection*).

Never returns nullptr.


void StartReading(TcpMsgConnection* c)

Can be called at most once on the given TcpMsgConnection. Causes reading of messages to begin.


bool NotifyMoreMessagesToWrite(TcpMsgConnection* c)

Notification that there are more messages available to be written. This can be called at any time by any thread without restriction, apart from all such calls being made before the connection is closed.

Returns false if the framework was already writing messages (i.e. the call was redundant). Calls to NotifyMoreMessagesToWrite() that return true are paired with calls to ITcpMsgHandler::OnWriteFailure().


void Close(TcpMsgConnection* c)

Close() must be paired with every call to CreateTcpMsgConnection().

Close() has an abortive nature, meaning it won't ensure reading up to the end-of-stream indicator, nor will it ensure all messages already prepared in memory with calls to ITcpMsgHandler::WriteMessage() will be sent through the socket. An application may therefore want to delay the call to Close() according to whether all data has been sent or received as required. For this purpose it is appropriate to make use of ITcpMsgHandler::OnReadFailure() which is called exactly once after reading has completed, and also ITcpMsgHandler::OnWriteFailure() which can be used to determine when writing has completed.

Close() must not be called from any of the methods of the ITcpMsgHandler, or else a dead-lock results.

Close() is synchronous meaning that it doesn't return until the socket has been closed and all methods on the ITcpMsgHandler have been called including the call to ITcpMsgHandler::OnRelease(). Therefore it is safe to delete the ITcpMsgHandler after calling Close().