366 fn register_client( |
366 fn register_client( |
367 &mut self, |
367 &mut self, |
368 poll: &Poll, |
368 poll: &Poll, |
369 client_socket: ClientSocket, |
369 client_socket: ClientSocket, |
370 addr: SocketAddr, |
370 addr: SocketAddr, |
371 ) -> ClientId { |
371 ) -> io::Result<ClientId> { |
372 let entry = self.clients.vacant_entry(); |
372 let entry = self.clients.vacant_entry(); |
373 let client_id = entry.key(); |
373 let client_id = entry.key(); |
374 |
374 |
375 poll.register( |
375 poll.register( |
376 client_socket.inner(), |
376 client_socket.inner(), |
377 Token(client_id), |
377 Token(client_id), |
378 Ready::readable() | Ready::writable(), |
378 Ready::readable() | Ready::writable(), |
379 PollOpt::edge(), |
379 PollOpt::edge(), |
380 ) |
380 )?; |
381 .expect("could not register socket with event loop"); |
|
382 |
381 |
383 let client = NetworkClient::new( |
382 let client = NetworkClient::new( |
384 client_id, |
383 client_id, |
385 client_socket, |
384 client_socket, |
386 addr, |
385 addr, |
387 create_ping_timeout(&mut self.timer, PING_PROBES_COUNT - 1, client_id), |
386 create_ping_timeout(&mut self.timer, PING_PROBES_COUNT - 1, client_id), |
388 ); |
387 ); |
389 info!("client {} ({}) added", client.id, client.peer_addr); |
388 info!("client {} ({}) added", client.id, client.peer_addr); |
390 entry.insert(client); |
389 entry.insert(client); |
391 |
390 |
392 client_id |
391 Ok(client_id) |
393 } |
392 } |
394 |
393 |
395 fn handle_response(&mut self, mut response: handlers::Response, poll: &Poll) { |
394 fn handle_response(&mut self, mut response: handlers::Response, poll: &Poll) { |
396 if response.is_empty() { |
395 if response.is_empty() { |
397 return; |
396 return; |
451 } |
450 } |
452 Ok(()) |
451 Ok(()) |
453 } |
452 } |
454 |
453 |
455 #[cfg(feature = "official-server")] |
454 #[cfg(feature = "official-server")] |
456 pub fn handle_io_result(&mut self) { |
455 pub fn handle_io_result(&mut self) -> io::Result<()> { |
457 if let Some((client_id, result)) = self.io.try_recv() { |
456 if let Some((client_id, result)) = self.io.try_recv() { |
458 let mut response = handlers::Response::new(client_id); |
457 let mut response = handlers::Response::new(client_id); |
459 handlers::handle_io_result(&mut self.server, client_id, &mut response, result); |
458 handlers::handle_io_result(&mut self.server, client_id, &mut response, result); |
460 } |
459 } |
|
460 Ok(()) |
461 } |
461 } |
462 |
462 |
463 fn create_client_socket(&self, socket: TcpStream) -> io::Result<ClientSocket> { |
463 fn create_client_socket(&self, socket: TcpStream) -> io::Result<ClientSocket> { |
464 Ok(ClientSocket::Plain(socket)) |
464 Ok(ClientSocket::Plain(socket)) |
465 } |
465 } |
495 match server_token { |
495 match server_token { |
496 utils::SERVER_TOKEN => { |
496 utils::SERVER_TOKEN => { |
497 let (client_socket, addr) = self.listener.accept()?; |
497 let (client_socket, addr) = self.listener.accept()?; |
498 info!("Connected(plaintext): {}", addr); |
498 info!("Connected(plaintext): {}", addr); |
499 let client_id = |
499 let client_id = |
500 self.register_client(poll, self.create_client_socket(client_socket)?, addr); |
500 self.register_client(poll, self.create_client_socket(client_socket)?, addr)?; |
501 self.init_client(poll, client_id); |
501 self.init_client(poll, client_id); |
502 } |
502 } |
503 #[cfg(feature = "tls-connections")] |
503 #[cfg(feature = "tls-connections")] |
504 utils::SECURE_SERVER_TOKEN => { |
504 utils::SECURE_SERVER_TOKEN => { |
505 let (client_socket, addr) = self.ssl.listener.accept()?; |
505 let (client_socket, addr) = self.ssl.listener.accept()?; |
506 info!("Connected(TLS): {}", addr); |
506 info!("Connected(TLS): {}", addr); |
507 self.register_client(poll, self.create_client_secure_socket(client_socket)?, addr); |
507 self.register_client(poll, self.create_client_secure_socket(client_socket)?, addr)?; |
508 } |
508 } |
509 _ => unreachable!(), |
509 _ => unreachable!(), |
510 } |
510 } |
511 |
511 |
512 Ok(()) |
512 Ok(()) |