// OnReceive //------------------------------------------------------------------------------ /*virtual*/ void Server::OnReceive( const ConnectionInfo * connection, void * data, uint32_t size, bool & keepMemory ) { keepMemory = true; // we'll take care of freeing the memory ClientState * cs = (ClientState *)connection->GetUserData(); ASSERT( cs ); // are we expecting a msg, or the payload for a msg? void * payload = nullptr; size_t payloadSize = 0; if ( cs->m_CurrentMessage == nullptr ) { // message cs->m_CurrentMessage = reinterpret_cast< const Protocol::IMessage * >( data ); if ( cs->m_CurrentMessage->HasPayload() ) { return; } } else { // payload ASSERT( cs->m_CurrentMessage->HasPayload() ); payload = data; payloadSize = size; } // determine message type const Protocol::IMessage * imsg = cs->m_CurrentMessage; Protocol::MessageType messageType = imsg->GetType(); PROTOCOL_DEBUG( "Client -> Server : %u (%s)\n", messageType, GetProtocolMessageDebugName( messageType ) ); switch ( messageType ) { case Protocol::MSG_CONNECTION: { const Protocol::MsgConnection * msg = static_cast< const Protocol::MsgConnection * >( imsg ); Process( connection, msg ); break; } case Protocol::MSG_STATUS: { const Protocol::MsgStatus * msg = static_cast< const Protocol::MsgStatus * >( imsg ); Process( connection, msg ); break; } case Protocol::MSG_NO_JOB_AVAILABLE: { const Protocol::MsgNoJobAvailable * msg = static_cast< const Protocol::MsgNoJobAvailable * >( imsg ); Process( connection, msg ); break; } case Protocol::MSG_JOB: { const Protocol::MsgJob * msg = static_cast< const Protocol::MsgJob * >( imsg ); Process( connection, msg, payload, payloadSize ); break; } case Protocol::MSG_MANIFEST: { const Protocol::MsgManifest * msg = static_cast< const Protocol::MsgManifest * >( imsg ); Process( connection, msg, payload, payloadSize ); break; } case Protocol::MSG_FILE: { const Protocol::MsgFile * msg = static_cast< const Protocol::MsgFile * >( imsg ); Process( connection, msg, payload, payloadSize ); break; } default: { // unknown message type ASSERT( false ); // this indicates a protocol bug Disconnect( connection ); break; } } // free everything FREE( (void *)( cs->m_CurrentMessage ) ); FREE( payload ); cs->m_CurrentMessage = nullptr; }
// OnReceive //------------------------------------------------------------------------------ /*virtual*/ void Client::OnReceive( const ConnectionInfo * connection, void * data, uint32_t size, bool & keepMemory ) { keepMemory = true; // we'll take care of freeing the memory MutexHolder mh( m_ServerListMutex ); ServerState * ss = (ServerState *)connection->GetUserData(); ASSERT( ss ); // are we expecting a msg, or the payload for a msg? void * payload = nullptr; size_t payloadSize = 0; if ( ss->m_CurrentMessage == nullptr ) { // message ss->m_CurrentMessage = reinterpret_cast< const Protocol::IMessage * >( data ); if ( ss->m_CurrentMessage->HasPayload() ) { return; } } else { // payload ASSERT( ss->m_CurrentMessage->HasPayload() ); payload = data; payloadSize = size; } // determine message type const Protocol::IMessage * imsg = ss->m_CurrentMessage; Protocol::MessageType messageType = imsg->GetType(); PROTOCOL_DEBUG( "Server -> Client : %u (%s)\n", messageType, GetProtocolMessageDebugName( messageType ) ); switch ( messageType ) { case Protocol::MSG_REQUEST_JOB: { const Protocol::MsgRequestJob * msg = static_cast< const Protocol::MsgRequestJob * >( imsg ); Process( connection, msg ); break; } case Protocol::MSG_JOB_RESULT: { const Protocol::MsgJobResult * msg = static_cast< const Protocol::MsgJobResult * >( imsg ); Process( connection, msg, payload, payloadSize ); break; } case Protocol::MSG_REQUEST_MANIFEST: { const Protocol::MsgRequestManifest * msg = static_cast< const Protocol::MsgRequestManifest * >( imsg ); Process( connection, msg ); break; } case Protocol::MSG_REQUEST_FILE: { const Protocol::MsgRequestFile * msg = static_cast< const Protocol::MsgRequestFile * >( imsg ); Process( connection, msg ); break; } case Protocol::MSG_SERVER_STATUS: { const Protocol::MsgServerStatus * msg = static_cast< const Protocol::MsgServerStatus * >( imsg ); Process( connection, msg ); break; } default: { // unknown message type ASSERT( false ); // this indicates a protocol bug Disconnect( connection ); break; } } // free everything FREE( (void *)( ss->m_CurrentMessage ) ); FREE( payload ); ss->m_CurrentMessage = nullptr; }