gameServer2/src/server/network.rs
changeset 13524 5359ff75da3a
parent 13478 d79795acaa73
child 13666 09f4a30e50cc
equal deleted inserted replaced
13523:8c5dd562c9f7 13524:5359ff75da3a
   103         self.socket.flush()?;
   103         self.socket.flush()?;
   104         result
   104         result
   105     }
   105     }
   106 
   106 
   107     pub fn send_raw_msg(&mut self, msg: &[u8]) {
   107     pub fn send_raw_msg(&mut self, msg: &[u8]) {
   108         self.buf_out.write(msg).unwrap();
   108         self.buf_out.write_all(msg).unwrap();
   109     }
   109     }
   110 
   110 
   111     pub fn send_string(&mut self, msg: &String) {
   111     pub fn send_string(&mut self, msg: &str) {
   112         self.send_raw_msg(&msg.as_bytes());
   112         self.send_raw_msg(&msg.as_bytes());
   113     }
   113     }
   114 
   114 
   115     pub fn send_msg(&mut self, msg: HWServerMessage) {
   115     pub fn send_msg(&mut self, msg: &HWServerMessage) {
   116         self.send_string(&msg.to_raw_protocol());
   116         self.send_string(&msg.to_raw_protocol());
   117     }
   117     }
   118 }
   118 }
   119 
   119 
   120 pub struct NetworkLayer {
   120 pub struct NetworkLayer {
   141 
   141 
   142     fn deregister_client(&mut self, poll: &Poll, id: ClientId) {
   142     fn deregister_client(&mut self, poll: &Poll, id: ClientId) {
   143         let mut client_exists = false;
   143         let mut client_exists = false;
   144         if let Some(ref client) = self.clients.get(id) {
   144         if let Some(ref client) = self.clients.get(id) {
   145             poll.deregister(&client.socket)
   145             poll.deregister(&client.socket)
   146                 .ok().expect("could not deregister socket");
   146                 .expect("could not deregister socket");
   147             info!("client {} ({}) removed", client.id, client.peer_addr);
   147             info!("client {} ({}) removed", client.id, client.peer_addr);
   148             client_exists = true;
   148             client_exists = true;
   149         }
   149         }
   150         if client_exists {
   150         if client_exists {
   151             self.clients.remove(id);
   151             self.clients.remove(id);
   154 
   154 
   155     fn register_client(&mut self, poll: &Poll, id: ClientId, client_socket: TcpStream, addr: SocketAddr) {
   155     fn register_client(&mut self, poll: &Poll, id: ClientId, client_socket: TcpStream, addr: SocketAddr) {
   156         poll.register(&client_socket, Token(id),
   156         poll.register(&client_socket, Token(id),
   157                       Ready::readable() | Ready::writable(),
   157                       Ready::readable() | Ready::writable(),
   158                       PollOpt::edge())
   158                       PollOpt::edge())
   159             .ok().expect("could not register socket with event loop");
   159             .expect("could not register socket with event loop");
   160 
   160 
   161         let entry = self.clients.vacant_entry();
   161         let entry = self.clients.vacant_entry();
   162         let client = NetworkClient::new(id, client_socket, addr);
   162         let client = NetworkClient::new(id, client_socket, addr);
   163         info!("client {} ({}) added", client.id, client.peer_addr);
   163         info!("client {} ({}) added", client.id, client.peer_addr);
   164         entry.insert(client);
   164         entry.insert(client);
   187         self.flush_server_messages();
   187         self.flush_server_messages();
   188 
   188 
   189         Ok(())
   189         Ok(())
   190     }
   190     }
   191 
   191 
   192     fn operation_failed(&mut self, poll: &Poll, client_id: ClientId, error: Error, msg: &str) -> io::Result<()> {
   192     fn operation_failed(&mut self, poll: &Poll, client_id: ClientId, error: &Error, msg: &str) -> io::Result<()> {
   193         let addr = if let Some(ref mut client) = self.clients.get_mut(client_id) {
   193         let addr = if let Some(ref mut client) = self.clients.get_mut(client_id) {
   194             client.peer_addr
   194             client.peer_addr
   195         } else {
   195         } else {
   196             SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0)
   196             SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0)
   197         };
   197         };
   222                         self.client_error(&poll, client_id)?,
   222                         self.client_error(&poll, client_id)?,
   223                     _ => {}
   223                     _ => {}
   224                 };
   224                 };
   225             }
   225             }
   226             Err(e) => self.operation_failed(
   226             Err(e) => self.operation_failed(
   227                 poll, client_id, e,
   227                 poll, client_id, &e,
   228                 "Error while reading from client socket")?
   228                 "Error while reading from client socket")?
   229         }
   229         }
   230 
   230 
   231         self.flush_server_messages();
   231         self.flush_server_messages();
   232 
   232 
   254             Ok(((), state)) if state == NetworkClientState::NeedsWrite => {
   254             Ok(((), state)) if state == NetworkClientState::NeedsWrite => {
   255                 self.pending.insert((client_id, state));
   255                 self.pending.insert((client_id, state));
   256             },
   256             },
   257             Ok(_) => {}
   257             Ok(_) => {}
   258             Err(e) => self.operation_failed(
   258             Err(e) => self.operation_failed(
   259                 poll, client_id, e,
   259                 poll, client_id, &e,
   260                 "Error while writing to client socket")?
   260                 "Error while writing to client socket")?
   261         }
   261         }
   262 
   262 
   263         Ok(())
   263         Ok(())
   264     }
   264     }