void asio_listener::handle_partial_body( ana::read_buffer buffer, const boost::system::error_code& ec, size_t accumulated, size_t last_msg_size) { try { if (ec) disconnect( ec ); else { accumulated += last_msg_size; //2nd param to add a completed packet stats_collector().log_receive( last_msg_size, accumulated == buffer->size() ); if ( accumulated > buffer->size() ) throw std::runtime_error("The read operation was too large."); if ( accumulated == buffer->size() ) { listener_->handle_receive( ec, id(), buffer ); delete next_message_timer_; next_message_timer_ = NULL; listen_one_message(); } else socket().async_read_some(boost::asio::buffer(buffer->base_char() + accumulated, buffer->size() - accumulated), boost::bind(&asio_listener::handle_partial_body, this, buffer, boost::asio::placeholders::error, accumulated, _2 )); } } catch(const std::exception&) { disconnect( ec); } }
virtual void handle_receive( ana::error_code error, net_id client, ana::read_buffer buffer) { if (! error) { std::string msg = buffer->string(); if (msg.empty()) std::cout << "Received empty buffer. Size: " << buffer->size() << "\n"; else if (msg[0] == '/') parse_command(client, msg); else { std::stringstream ss; ss << names_[client] << " : " << msg; server_->send_all_except(client, ana::buffer( ss.str() ), this); } } else handle_disconnect(error, client); }
void ana_network_manager::handle_receive( ana::error_code error, ana::net_id client, ana::read_buffer buffer) { if (error) network::disconnect( client ); else { std::set< ana_component* >::iterator it; it = std::find_if( components_.begin(), components_.end(), boost::bind(&ana_component::get_id, _1) == client ); if ( it != components_.end() ) (*it)->add_buffer( buffer, client ); else { if (components_.empty() ) throw std::runtime_error("Received a message while no component was running.\n"); std::map< ana::server*, clients_manager* >::iterator mgrs; for ( mgrs = server_manager_.begin(); mgrs != server_manager_.end(); ++mgrs) { if (mgrs->second->is_a_client( client ) ) // Is this your client? { if ( mgrs->second->is_pending_handshake( client ) ) // Did he login already? { // all handshakes are 4 bytes long if ( buffer->size() != sizeof(ana::ana_uint32) ) mgrs->first->disconnect( client ); ana::ana_uint32 handshake; { ana::serializer::bistream bis( buffer->string() ); bis >> handshake; ana::network_to_host_long( handshake ); //I'm expecting a 0 anyway } if ( handshake != 0 ) mgrs->first->disconnect( client ); else { mgrs->second->handshaked( client ); //send back it's id ana::serializer::bostream bos; ana::ana_uint32 network_byte_order_id = client; ana::host_to_network_long( network_byte_order_id ); bos << network_byte_order_id; ana_handshake_finisher_handler* handler = new ana_handshake_finisher_handler( mgrs->first, mgrs->second); mgrs->first->send_one(client, ana::buffer( bos.str() ), handler ); mgrs->first->set_header_first_mode( client ); } } else // just add the buffer to the associated clients_manager { ana::net_id server_id = mgrs->first->id(); it = std::find_if( components_.begin(), components_.end(), boost::bind(&ana_component::get_id, _1) == server_id ); if ( (*it)->is_client() ) throw std::runtime_error("Wrong id to receive from."); (*it)->add_buffer( buffer, client ); } } } } }