/////////////////////////////////////////////////////////////////////////////// /// CListener::HandleRead /// @description The callback which accepts messages from the remote sender. /// @param e The errorcode if any associated. /// @param bytes_transferred The size of the datagram being read. /// @pre The connection has had start called and some message has been placed /// in the buffer by the receive call. /// @post The message is scheduled for delivery by the dispatcher to one or /// more modules. The message has been processed by the CConnection that /// manages messages between this process and the sender. ScheduleListen() /// is waiting for another datagram to arrive. /////////////////////////////////////////////////////////////////////////////// void CListener::HandleRead(const boost::system::error_code& e, std::size_t bytes_transferred) { Logger.Trace << __PRETTY_FUNCTION__ << std::endl; if (e) { Logger.Error<<"HandleRead failed: " << e.message(); ScheduleListen(); } Logger.Debug<<"Loading protobuf"<<std::endl; ProtocolMessageWindow pmw; if(!pmw.ParseFromArray(m_buffer.begin(), bytes_transferred)) { Logger.Error<<"Failed to load protobuf"<<std::endl; ScheduleListen(); return; } #ifdef CUSTOMNETWORK if((rand()%100) >= GetReliability()) { Logger.Debug<<"Dropped datagram "<<pm.hash()<<":"<<pm.sequence_num()<<std::endl; ScheduleListen(); return; } #endif Logger.Debug<<"Fetching Connection"<<std::endl; std::string uuid = pmw.source_uuid(); /// We can make the remote host from the endpoint: SRemoteHost host = { m_recv_from.address().to_string(), boost::lexical_cast<std::string>(m_recv_from.port()) }; ///Make sure the hostname is registered: CConnectionManager::Instance().PutHost(uuid,host); ///Get the pointer to the connection: ConnectionPtr conn = CConnectionManager::Instance().CreateConnection(uuid, m_recv_from); //ConnectionPtr conn = CConnectionManager::Instance().GetConnectionByUUID(uuid); Logger.Debug<<"Fetched Connection"<<std::endl; BOOST_FOREACH(const ProtocolMessage &pm, pmw.messages()) { if(pm.status() == ProtocolMessage::ACCEPTED) { Logger.Debug<<"Processing Accept Message"<<std::endl; Logger.Debug<<"Received ACK"<<pm.hash()<<":"<<pm.sequence_num()<<std::endl; conn->ReceiveACK(pm); } else if(conn->Receive(pm)) { Logger.Debug<<"Accepted message "<<pm.hash()<<":"<<pm.sequence_num()<<std::endl; CDispatcher::Instance().HandleRequest( boost::make_shared<const ModuleMessage>( ModuleMessage(pm.module_message())), uuid); } else if(pm.status() != ProtocolMessage::CREATED) { Logger.Debug<<"Rejected message "<<pm.hash()<<":"<<pm.sequence_num()<<std::endl; } } conn->OnReceive(); ScheduleListen(); }