3 |
3 |
4 use super::{actions::Destination, core::HWServer, coretypes::ClientId}; |
4 use super::{actions::Destination, core::HWServer, coretypes::ClientId}; |
5 use crate::{ |
5 use crate::{ |
6 protocol::messages::{HWProtocolMessage, HWServerMessage, HWServerMessage::*}, |
6 protocol::messages::{HWProtocolMessage, HWServerMessage, HWServerMessage::*}, |
7 server::actions::PendingMessage, |
7 server::actions::PendingMessage, |
|
8 utils, |
8 }; |
9 }; |
|
10 use base64::encode; |
9 use log::*; |
11 use log::*; |
|
12 use rand::{thread_rng, RngCore}; |
10 |
13 |
11 mod checker; |
14 mod checker; |
12 mod common; |
15 mod common; |
13 mod inroom; |
16 mod inroom; |
14 mod lobby; |
17 mod lobby; |
15 mod loggingin; |
18 mod loggingin; |
|
19 |
|
20 use self::loggingin::LoginResult; |
16 |
21 |
17 pub struct Response { |
22 pub struct Response { |
18 client_id: ClientId, |
23 client_id: ClientId, |
19 messages: Vec<PendingMessage>, |
24 messages: Vec<PendingMessage>, |
20 } |
25 } |
100 client_id: ClientId, |
105 client_id: ClientId, |
101 response: &mut Response, |
106 response: &mut Response, |
102 message: HWProtocolMessage, |
107 message: HWProtocolMessage, |
103 ) { |
108 ) { |
104 match message { |
109 match message { |
105 HWProtocolMessage::Ping => { |
110 HWProtocolMessage::Ping => response.add(Pong.send_self()), |
106 response.add(Pong.send_self()); |
|
107 } |
|
108 HWProtocolMessage::Quit(Some(msg)) => { |
|
109 common::remove_client(server, response, "User quit: ".to_string() + &msg); |
|
110 } |
|
111 HWProtocolMessage::Quit(None) => { |
|
112 common::remove_client(server, response, "User quit".to_string()); |
|
113 } |
|
114 HWProtocolMessage::Malformed => warn!("Malformed/unknown message"), |
111 HWProtocolMessage::Malformed => warn!("Malformed/unknown message"), |
115 HWProtocolMessage::Empty => warn!("Empty message"), |
112 HWProtocolMessage::Empty => warn!("Empty message"), |
116 _ => match server.clients[client_id].room_id { |
113 _ => { |
117 None => loggingin::handle(server, client_id, response, message), |
114 if server.anteroom.clients.contains(client_id) { |
118 Some(id) if id == server.lobby_id => { |
115 match loggingin::handle(&mut server.anteroom, client_id, response, message) { |
119 lobby::handle(server, client_id, response, message) |
116 LoginResult::Unchanged => (), |
|
117 LoginResult::Complete => { |
|
118 if let Some(client) = server.anteroom.remove_client(client_id) { |
|
119 server.add_client(client_id, client); |
|
120 } |
|
121 } |
|
122 LoginResult::Exit => { |
|
123 server.anteroom.remove_client(client_id); |
|
124 } |
|
125 } |
|
126 } else { |
|
127 match message { |
|
128 HWProtocolMessage::Quit(Some(msg)) => { |
|
129 common::remove_client(server, response, "User quit: ".to_string() + &msg); |
|
130 } |
|
131 HWProtocolMessage::Quit(None) => { |
|
132 common::remove_client(server, response, "User quit".to_string()); |
|
133 } |
|
134 _ => match server.clients[client_id].room_id { |
|
135 None => lobby::handle(server, client_id, response, message), |
|
136 Some(room_id) => { |
|
137 inroom::handle(server, client_id, response, room_id, message) |
|
138 } |
|
139 }, |
|
140 } |
120 } |
141 } |
121 Some(id) => inroom::handle(server, client_id, response, id, message), |
142 } |
122 }, |
|
123 } |
143 } |
|
144 } |
|
145 |
|
146 pub fn handle_client_accept(server: &mut HWServer, client_id: ClientId, response: &mut Response) { |
|
147 let mut salt = [0u8; 18]; |
|
148 thread_rng().fill_bytes(&mut salt); |
|
149 |
|
150 server.anteroom.add_client(client_id, encode(&salt)); |
|
151 |
|
152 response.add(HWServerMessage::Connected(utils::PROTOCOL_VERSION).send_self()); |
124 } |
153 } |
125 |
154 |
126 pub fn handle_client_loss(server: &mut HWServer, client_id: ClientId, response: &mut Response) { |
155 pub fn handle_client_loss(server: &mut HWServer, client_id: ClientId, response: &mut Response) { |
127 common::remove_client(server, response, "Connection reset".to_string()); |
156 common::remove_client(server, response, "Connection reset".to_string()); |
128 } |
157 } |