Note that there are some explanatory texts on larger screens.

plurals
  1. POboost::asio::async_read not reading subsequent packets
    primarykey
    data
    text
    <p>I'm trying to make a TCP Chat Server that will communicate with an already existing chat client. I don't have access to the code of the existing chat client but I know what it sends over the network and what I should send back.</p> <p>I'm trying to use the <code>boost::asio</code> library but for some reason it doesn't want to perform the second <code>boost::asio::async_read</code> and I can't for the life of me figure it out.</p> <p>I edited the <code>boost::asio::async_read</code> function to add a <code>printf( "async_read\n" );</code> just to see if it actually was being called and indeed it is.</p> <p>Here is what I'm getting as output:</p> <pre><code>[INFO] Adding Server 0.0.0.0:10013 [INFO] Starting Servers [ROOM] Client Joined (#1) async_read [HEADER] \x70 [HEADER] 112 async_read [ROOM] Client Left (#1) </code></pre> <p>The client sends two packets right after each other:</p> <pre><code>Packet 1 (Header : Body Size): 00000000 70 00 p. Packet 2 (Body : Auth Info): 00000002 00 0c 59 38 3d 00 38 34 62 63 34 35 62 65 35 66 ..Y8=.84 bc45be5f 00000012 36 33 34 34 35 66 63 33 38 32 34 32 31 61 39 39 63445fc3 82421a99 00000022 31 66 37 66 39 62 00 31 30 35 2e 32 33 36 2e 33 1f7f9b.1 05.236.3 00000032 35 2e 39 39 00 61 39 33 35 66 65 62 32 64 62 66 5.99.a93 5feb2dbf 00000042 36 65 38 66 32 39 30 62 32 61 36 64 36 37 61 35 6e8f290b 2a6d67a5 00000052 31 36 63 65 65 30 36 34 64 38 64 63 37 00 28 00 16cee064 d8dc7.(. 00000062 00 00 81 06 01 00 77 61 63 00 03 00 09 00 00 00 ......wa c....... </code></pre> <p>As you can see, the server code only receives the first packet then stalls on the second <code>boost::asio::async_read</code> call</p> <p>Here is the modified code:</p> <pre><code>typedef std::deque&lt; ChatMessage &gt; ChatMessageQueue; class ChatParticipant { public: virtual ~ChatParticipant( ) { } virtual void Deliver( const ChatMessage &amp;Message ) = 0; }; typedef boost::shared_ptr&lt; ChatParticipant &gt; ChatParticipantPtr; class ChatRoom { public: void join( ChatParticipantPtr Participant ) { m_Participants.insert( Participant ); printf( "[ROOM] Client Joined (#%i)\n", m_Participants.size( ) ); std::for_each( m_RecentMessages.begin( ), m_RecentMessages.end( ), boost::bind( &amp;ChatParticipant::Deliver, Participant, _1 ) ); } void leave( ChatParticipantPtr Participant ) { printf( "[ROOM] Client Left (#%i)\n", m_Participants.count( Participant ) ); m_Participants.erase( Participant ); } void deliver( const ChatMessage &amp;Message ) { m_RecentMessages.push_back( Message ); while( m_RecentMessages.size( ) &gt; MaxRecentMessages ) m_RecentMessages.pop_front( ); std::for_each( m_Participants.begin( ), m_Participants.end( ), boost::bind( &amp;ChatParticipant::Deliver, _1, boost::ref( Message ) ) ); } private: std::set&lt; ChatParticipantPtr &gt; m_Participants; enum { MaxRecentMessages = 100 }; ChatMessageQueue m_RecentMessages; }; class ChatSession : public ChatParticipant, public boost::enable_shared_from_this&lt; ChatSession &gt; { public: ChatSession( boost::asio::io_service &amp;IOService, ChatRoom &amp;Room ) : m_Socket( IOService ), m_Room( Room ) { } tcp::socket &amp;Socket( ) { return m_Socket; } void Start( ) { m_Room.join( shared_from_this( ) ); boost::asio::async_read( m_Socket, boost::asio::buffer( m_ReadMessage.data( ), ChatMessage::HeaderLength ), boost::bind( &amp;ChatSession::Handle_ReadHeader, shared_from_this( ), boost::asio::placeholders::error ) ); } void Deliver( const ChatMessage &amp;Message ) { bool write_in_progress = !m_WriteMessages.empty( ); m_WriteMessages.push_back( Message ); if( !write_in_progress ) { boost::asio::async_write( m_Socket, boost::asio::buffer( m_WriteMessages.front( ).data( ), m_WriteMessages.front( ).length( ) ), boost::bind( &amp;ChatSession::Handle_Write, shared_from_this( ), boost::asio::placeholders::error ) ); } } void Handle_ReadHeader( const boost::system::error_code &amp;Error ) { if( Error ) { m_Room.leave( shared_from_this( ) ); return; } if( m_ReadMessage.DecodeHeader( ) ) { printf( "[HEADER] %s\n", Strings::StringToHex( m_ReadMessage.data( ) ).c_str( ) ); printf( "[HEADER] %i\n", m_ReadMessage.length( ) ); boost::asio::async_read( m_Socket, boost::asio::buffer( m_ReadMessage.body( ), m_ReadMessage.length( ) ), boost::bind( &amp;ChatSession::Handle_ReadBody, shared_from_this( ), boost::asio::placeholders::error ) ); } } void Handle_ReadBody( const boost::system::error_code &amp;Error ) { if( Error ) { m_Room.leave( shared_from_this( ) ); return; } printf( "[BODY] %s\n", Strings::StringToHex( m_ReadMessage.body( ) ).c_str( ) ); //m_Room.deliver( m_ReadMessage ); boost::asio::async_read( m_Socket, boost::asio::buffer( m_ReadMessage.data( ), ChatMessage::HeaderLength ), boost::bind( &amp;ChatSession::Handle_ReadHeader, shared_from_this( ), boost::asio::placeholders::error ) ); } void Handle_Write( const boost::system::error_code &amp;Error ) { if( Error ) { m_Room.leave( shared_from_this( ) ); return; } m_WriteMessages.pop_front( ); if( !m_WriteMessages.empty( ) ) { boost::asio::async_write( m_Socket, boost::asio::buffer( m_WriteMessages.front( ).data( ), m_WriteMessages.front( ).length( ) ), boost::bind( &amp;ChatSession::Handle_Write, shared_from_this( ), boost::asio::placeholders::error ) ); } } private: tcp::socket m_Socket; ChatRoom &amp;m_Room; ChatMessage m_ReadMessage; ChatMessageQueue m_WriteMessages; }; typedef boost::shared_ptr&lt; ChatSession &gt; ChatSessionPtr; class ChatServer { public: ChatServer( boost::asio::io_service &amp;IOService, const tcp::endpoint &amp;EndPoint ) : m_IOService( IOService ), m_Acceptor( IOService, EndPoint ) { StartAccepting( ); } void StartAccepting() { ChatSessionPtr NewSession( new ChatSession( m_IOService, m_Room ) ); m_Acceptor.async_accept( NewSession-&gt;Socket( ), boost::bind( &amp;ChatServer::Handle_Accept, this, NewSession, boost::asio::placeholders::error ) ); } void Handle_Accept( ChatSessionPtr Session, const boost::system::error_code&amp; Error ) { if( !Error ) Session-&gt;Start( ); StartAccepting( ); } private: boost::asio::io_service &amp;m_IOService; tcp::acceptor m_Acceptor; ChatRoom m_Room; }; typedef boost::shared_ptr&lt; ChatServer &gt; ChatServerPtr; int main( int argc, char **argv, char **envp ) { boost::asio::io_service IOService; tcp::endpoint EndPoint( tcp::v4( ), 10013 ); ChatServerPtr Server( new ChatServer( IOService, EndPoint ) ); printf( "[INFO] Adding Server %s:%i\n", EndPoint.address( ).to_string( ).c_str( ), EndPoint.port( ) ); IOService.run( ); return TRUE; } </code></pre> <p>Can you see what I'm doing wrong?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload