gameServer2/src/server/handlers/loggingin.rs
changeset 13419 81e0ed105f5d
parent 13416 cdf69667593b
child 13478 d79795acaa73
equal deleted inserted replaced
13418:bb24c3414b0d 13419:81e0ed105f5d
     1 use mio;
     1 use mio;
     2 
     2 
     3 use server::server::HWServer;
     3 use server::{
     4 use server::actions::Action;
     4     server::HWServer,
     5 use server::actions::Action::*;
     5     client::ClientId,
     6 use protocol::messages::HWProtocolMessage;
     6     actions::{Action, Action::*}
     7 use protocol::messages::HWServerMessage::*;
     7 };
       
     8 use protocol::messages::{
       
     9     HWProtocolMessage, HWServerMessage::*
       
    10 };
     8 use utils::is_name_illegal;
    11 use utils::is_name_illegal;
     9 
    12 
    10 pub fn handle(server: & mut HWServer, token: usize, message: HWProtocolMessage) {
    13 pub fn handle(server: & mut HWServer, client_id: ClientId, message: HWProtocolMessage) {
    11     match message {
    14     match message {
    12         HWProtocolMessage::Nick(nick) => {
    15         HWProtocolMessage::Nick(nick) => {
    13             let actions;
    16             let actions;
    14             {
    17             {
    15                 let client = &mut server.clients[token];
    18                 let client = &mut server.clients[client_id];
    16                 debug!("{} {}", nick, is_name_illegal(&nick));
    19                 debug!("{} {}", nick, is_name_illegal(&nick));
    17                 actions = if client.room_id != None {
    20                 actions = if client.room_id != None {
    18                     unreachable!()
    21                     unreachable!()
    19                 }
    22                 }
    20                 else if !client.nick.is_empty() {
    23                 else if !client.nick.is_empty() {
    23                 else if     is_name_illegal(&nick) {
    26                 else if     is_name_illegal(&nick) {
    24                     vec![ByeClient("Illegal nickname! Nicknames must be between 1-40 characters long, must not have a trailing or leading space and must not have any of these characters: $()*+?[]^{|}".to_string())]
    27                     vec![ByeClient("Illegal nickname! Nicknames must be between 1-40 characters long, must not have a trailing or leading space and must not have any of these characters: $()*+?[]^{|}".to_string())]
    25                 }
    28                 }
    26                 else {
    29                 else {
    27                     client.nick = nick.clone();
    30                     client.nick = nick.clone();
    28                     vec![SendMe(Nick(nick)), CheckRegistered]
    31                     vec![Nick(nick).send_self().action(),
       
    32                          CheckRegistered]
    29                 };
    33                 };
    30             }
    34             }
    31             server.react(token, actions);
    35             server.react(client_id, actions);
    32         },
    36         },
    33         HWProtocolMessage::Proto(proto) => {
    37         HWProtocolMessage::Proto(proto) => {
    34             let actions;
    38             let actions;
    35             {
    39             {
    36                 let client = &mut server.clients[token];
    40                 let client = &mut server.clients[client_id];
    37                 actions = if client.protocol_number != 0 {
    41                 actions = if client.protocol_number != 0 {
    38                     vec![ProtocolError("Protocol already known.".to_string())]
    42                     vec![ProtocolError("Protocol already known.".to_string())]
    39                 }
    43                 }
    40                 else if proto == 0 {
    44                 else if proto == 0 {
    41                     vec![ProtocolError("Bad number.".to_string())]
    45                     vec![ProtocolError("Bad number.".to_string())]
    42                 }
    46                 }
    43                 else {
    47                 else {
    44                     client.protocol_number = proto;
    48                     client.protocol_number = proto;
    45                     vec![SendMe(Proto(proto)), CheckRegistered]
    49                     vec![Proto(proto).send_self().action(),
       
    50                          CheckRegistered]
    46                 };
    51                 };
    47             }
    52             }
    48             server.react(token, actions);
    53             server.react(client_id, actions);
    49         },
    54         },
    50         _ => warn!("Incorrect command in logging-in state"),
    55         _ => warn!("Incorrect command in logging-in state"),
    51     }
    56     }
    52 }
    57 }