rust/hedgewars-server/src/server/network.rs
changeset 14714 6a2e13e36b7f
parent 14694 08a8605bafaf
child 14717 8a45c90f4580
equal deleted inserted replaced
14713:e5415faa117b 14714:6a2e13e36b7f
   307     }
   307     }
   308 
   308 
   309     fn register_client(
   309     fn register_client(
   310         &mut self,
   310         &mut self,
   311         poll: &Poll,
   311         poll: &Poll,
   312         id: ClientId,
       
   313         client_socket: ClientSocket,
   312         client_socket: ClientSocket,
   314         addr: SocketAddr,
   313         addr: SocketAddr,
   315     ) {
   314     ) -> ClientId {
       
   315         let entry = self.clients.vacant_entry();
       
   316         let client_id = entry.key();
       
   317 
   316         poll.register(
   318         poll.register(
   317             client_socket.inner(),
   319             client_socket.inner(),
   318             Token(id),
   320             Token(client_id),
   319             Ready::readable() | Ready::writable(),
   321             Ready::readable() | Ready::writable(),
   320             PollOpt::edge(),
   322             PollOpt::edge(),
   321         )
   323         )
   322         .expect("could not register socket with event loop");
   324         .expect("could not register socket with event loop");
   323 
   325 
   324         let entry = self.clients.vacant_entry();
   326         let client = NetworkClient::new(client_id, client_socket, addr);
   325         let client = NetworkClient::new(id, client_socket, addr);
       
   326         info!("client {} ({}) added", client.id, client.peer_addr);
   327         info!("client {} ({}) added", client.id, client.peer_addr);
   327         entry.insert(client);
   328         entry.insert(client);
       
   329 
       
   330         client_id
   328     }
   331     }
   329 
   332 
   330     fn flush_server_messages(&mut self, mut response: handlers::Response) {
   333     fn flush_server_messages(&mut self, mut response: handlers::Response) {
   331         debug!("{} pending server messages", response.len());
   334         debug!("{} pending server messages", response.len());
   332         let output = response.extract_messages(&mut self.server);
   335         let output = response.extract_messages(&mut self.server);
   369 
   372 
   370     pub fn accept_client(&mut self, poll: &Poll) -> io::Result<()> {
   373     pub fn accept_client(&mut self, poll: &Poll) -> io::Result<()> {
   371         let (client_socket, addr) = self.listener.accept()?;
   374         let (client_socket, addr) = self.listener.accept()?;
   372         info!("Connected: {}", addr);
   375         info!("Connected: {}", addr);
   373 
   376 
   374         let client_id = self.server.add_client();
   377         let client_id = self.register_client(poll, self.create_client_socket(client_socket)?, addr);
   375         self.register_client(
   378 
   376             poll,
   379         let mut response = handlers::Response::new(client_id);
   377             client_id,
   380 
   378             self.create_client_socket(client_socket)?,
   381         handlers::handle_client_accept(&mut self.server, client_id, &mut response);
   379             addr,
   382 
   380         );
   383         if !response.is_empty() {
   381         //TODO: create response for initial messages
   384             self.flush_server_messages(response);
       
   385         }
   382 
   386 
   383         Ok(())
   387         Ok(())
   384     }
   388     }
   385 
   389 
   386     fn operation_failed(
   390     fn operation_failed(