1 use slab; |
1 use slab; |
2 use mio::tcp::*; |
2 use mio::tcp::*; |
3 use mio::*; |
3 use mio::*; |
4 use std::io::Write; |
|
5 use std::io; |
4 use std::io; |
6 |
5 |
7 use utils; |
6 use utils; |
8 use server::client::HWClient; |
7 use server::client::HWClient; |
9 use server::actions::Action; |
8 use server::actions::Action; |
10 use server::actions::Action::*; |
9 use super::handlers; |
11 use protocol::messages::HWProtocolMessage::*; |
|
12 use protocol::messages::HWServerMessage; |
|
13 |
10 |
14 type Slab<T> = slab::Slab<T, Token>; |
11 type Slab<T> = slab::Slab<T, Token>; |
15 |
12 |
16 pub struct HWServer { |
13 pub struct HWServer { |
17 listener: TcpListener, |
14 listener: TcpListener, |
18 clients: Slab<HWClient>, |
15 pub clients: Slab<HWClient>, |
19 rooms: Slab<HWRoom>, |
16 pub rooms: Slab<HWRoom>, |
20 lobbyId: Token, |
17 pub lobby_id: Token, |
21 } |
18 } |
22 |
19 |
23 impl HWServer { |
20 impl HWServer { |
24 pub fn new(listener: TcpListener, clients_limit: usize, rooms_limit: usize) -> HWServer { |
21 pub fn new(listener: TcpListener, clients_limit: usize, rooms_limit: usize) -> HWServer { |
25 let mut rooms = Slab::with_capacity(rooms_limit); |
22 let mut rooms = Slab::with_capacity(rooms_limit); |
26 let token = rooms.insert(HWRoom::new()).ok().expect("Cannot create lobby"); |
23 let token = rooms.insert(HWRoom::new()).ok().expect("Cannot create lobby"); |
27 HWServer { |
24 HWServer { |
28 listener: listener, |
25 listener: listener, |
29 clients: Slab::with_capacity(clients_limit), |
26 clients: Slab::with_capacity(clients_limit), |
30 rooms: rooms, |
27 rooms: rooms, |
31 lobbyId: token, |
28 lobby_id: token, |
32 } |
29 } |
33 } |
30 } |
34 |
31 |
35 pub fn register(&self, poll: &Poll) -> io::Result<()> { |
32 pub fn register(&self, poll: &Poll) -> io::Result<()> { |
36 poll.register(&self.listener, utils::SERVER, Ready::readable(), |
33 poll.register(&self.listener, utils::SERVER, Ready::readable(), |
39 |
36 |
40 pub fn accept(&mut self, poll: &Poll) -> io::Result<()> { |
37 pub fn accept(&mut self, poll: &Poll) -> io::Result<()> { |
41 let (sock, addr) = self.listener.accept()?; |
38 let (sock, addr) = self.listener.accept()?; |
42 info!("Connected: {}", addr); |
39 info!("Connected: {}", addr); |
43 |
40 |
44 let client = HWClient::new(sock, &self.lobbyId); |
41 let client = HWClient::new(sock, &self.lobby_id); |
45 let token = self.clients.insert(client) |
42 let token = self.clients.insert(client) |
46 .ok().expect("could not add connection to slab"); |
43 .ok().expect("could not add connection to slab"); |
47 |
44 |
48 self.clients[token].register(poll, token); |
45 self.clients[token].register(poll, token); |
49 |
46 |
79 self.react(token, poll, actions); |
76 self.react(token, poll, actions); |
80 |
77 |
81 Ok(()) |
78 Ok(()) |
82 } |
79 } |
83 |
80 |
84 fn send(&mut self, token: Token, msg: &String) { |
81 pub fn send(&mut self, token: Token, msg: &String) { |
85 self.clients[token].send_string(msg); |
82 self.clients[token].send_string(msg); |
86 } |
83 } |
87 |
84 |
88 fn react(&mut self, token: Token, poll: &Poll, actions: Vec<Action>) { |
85 pub fn react(&mut self, token: Token, poll: &Poll, actions: Vec<Action>) { |
89 for action in actions { |
86 for action in actions { |
90 match action { |
87 handlers::handle(self, token, poll, action); |
91 SendMe(msg) => self.send(token, &msg), |
|
92 ByeClient(msg) => { |
|
93 self.react(token, poll, vec![ |
|
94 SendMe(HWServerMessage::Bye(&msg).to_raw_protocol()), |
|
95 RemoveClient, |
|
96 ]); |
|
97 }, |
|
98 RemoveClient => { |
|
99 self.clients[token].deregister(poll); |
|
100 self.clients.remove(token); |
|
101 }, |
|
102 ReactProtocolMessage(msg) => match msg { |
|
103 Ping => self.react(token, poll, vec![SendMe(HWServerMessage::Pong.to_raw_protocol())]), |
|
104 Quit(Some(msg)) => self.react(token, poll, vec![ByeClient("User quit: ".to_string() + &msg)]), |
|
105 Quit(None) => self.react(token, poll, vec![ByeClient("User quit".to_string())]), |
|
106 Nick(nick) => if self.clients[token].nick.len() == 0 { |
|
107 self.send(token, &HWServerMessage::Nick(&nick).to_raw_protocol()); |
|
108 self.clients[token].nick = nick; |
|
109 }, |
|
110 Malformed => warn!("Malformed/unknown message"), |
|
111 Empty => warn!("Empty message"), |
|
112 _ => unimplemented!(), |
|
113 } |
|
114 //_ => unimplemented!(), |
|
115 } |
|
116 } |
88 } |
117 } |
89 } |
118 } |
90 } |
119 |
91 |
120 |
92 |
121 struct HWRoom { |
93 pub struct HWRoom { |
122 name: String |
94 pub name: String, |
123 } |
95 } |
124 |
96 |
125 impl HWRoom { |
97 impl HWRoom { |
126 pub fn new() -> HWRoom { |
98 pub fn new() -> HWRoom { |
127 HWRoom { |
99 HWRoom { |