gameServer2/src/server/network.rs
changeset 13424 81e0ed105f5d
parent 13421 cdf69667593b
child 13483 d79795acaa73
equal deleted inserted replaced
13423:bb24c3414b0d 13424:81e0ed105f5d
    15 use slab::Slab;
    15 use slab::Slab;
    16 
    16 
    17 use utils;
    17 use utils;
    18 use protocol::{ProtocolDecoder, messages::*};
    18 use protocol::{ProtocolDecoder, messages::*};
    19 use super::{
    19 use super::{
    20     server::{HWServer, PendingMessage, Destination},
    20     server::{HWServer},
    21     client::ClientId
    21     client::ClientId
    22 };
    22 };
    23 
    23 
    24 const MAX_BYTES_PER_READ: usize = 2048;
    24 const MAX_BYTES_PER_READ: usize = 2048;
    25 
    25 
   164         entry.insert(client);
   164         entry.insert(client);
   165     }
   165     }
   166 
   166 
   167     fn flush_server_messages(&mut self) {
   167     fn flush_server_messages(&mut self) {
   168         debug!("{} pending server messages", self.server.output.len());
   168         debug!("{} pending server messages", self.server.output.len());
   169         for PendingMessage(destination, msg) in self.server.output.drain(..) {
   169         for (clients, message) in self.server.output.drain(..) {
   170             debug!("Message {:?} to {:?}", msg, destination);
   170             debug!("Message {:?} to {:?}", message, clients);
   171             match destination {
   171             let msg_string = message.to_raw_protocol();
   172                 Destination::ToAll => {
   172             for client_id in clients {
   173                     let msg_string = msg.to_raw_protocol();
   173                 if let Some(client) = self.clients.get_mut(client_id) {
   174                     for (client_id, client) in self.clients.iter_mut() {
   174                     client.send_string(&msg_string);
   175                         client.send_string(&msg_string);
   175                     self.pending.insert((client_id, NetworkClientState::NeedsWrite));
   176                         self.pending.insert((client_id, NetworkClientState::NeedsWrite));
       
   177                     }
       
   178                 },
       
   179                 Destination::ToSelf(id)  => {
       
   180                     if let Some(client) = self.clients.get_mut(id) {
       
   181                         client.send_msg(msg);
       
   182                         self.pending.insert((id, NetworkClientState::NeedsWrite));
       
   183                     }
       
   184                 }
       
   185                 Destination::ToOthers(id) => {
       
   186                     let msg_string = msg.to_raw_protocol();
       
   187                     for (client_id, client) in self.clients.iter_mut() {
       
   188                         if client_id != id {
       
   189                             client.send_string(&msg_string);
       
   190                             self.pending.insert((client_id, NetworkClientState::NeedsWrite));
       
   191                         }
       
   192                     }
       
   193                 },
       
   194                 Destination::ToSelected(client_ids) => {
       
   195                     let msg_string = msg.to_raw_protocol();
       
   196                     for id in client_ids {
       
   197                         if let Some(client) = self.clients.get_mut(id) {
       
   198                             client.send_string(&msg_string);
       
   199                             self.pending.insert((id, NetworkClientState::NeedsWrite));
       
   200                         }
       
   201                     }
       
   202                 }
   176                 }
   203             }
   177             }
   204         }
   178         }
   205     }
   179     }
   206 
   180