void AnaClientsManager::handle_receive (ana::error_code ec, ana::net_id id, ana::read_buffer buf) { if (! ec ) { InputMessage bis( mili::substr(buf->string(), Pos(0), Count(HEADER_SIZE)) ); fud_size last_pos; bis >> last_pos; get_client(id)->handle_response( mili::substr(buf->string(), Pos(HEADER_SIZE), Pos((size_t) last_pos)) ); }
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::read_config( const ana::read_buffer& buffer, config& cfg) { std::istringstream input( buffer->string() ); read_gz(cfg, input); }
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 ); } } } } }