1 use slab; |
1 use slab; |
2 use mio::tcp::*; |
2 use mio::net::*; |
3 use mio::*; |
3 use mio::*; |
4 use std::io; |
4 use std::io; |
5 |
5 |
6 use utils; |
6 use utils; |
7 use super::client::HWClient; |
7 use super::client::HWClient; |
8 use super::actions; |
8 use super::actions; |
9 |
9 |
10 type Slab<T> = slab::Slab<T, Token>; |
10 type Slab<T> = slab::Slab<T>; |
11 |
11 |
12 pub struct HWServer { |
12 pub struct HWServer { |
13 listener: TcpListener, |
13 listener: TcpListener, |
14 pub clients: Slab<HWClient>, |
14 pub clients: Slab<HWClient>, |
15 pub rooms: Slab<HWRoom>, |
15 pub rooms: Slab<HWRoom>, |
16 pub lobby_id: Token, |
16 pub lobby_id: usize, |
17 } |
17 } |
18 |
18 |
19 impl HWServer { |
19 impl HWServer { |
20 pub fn new(listener: TcpListener, clients_limit: usize, rooms_limit: usize) -> HWServer { |
20 pub fn new(listener: TcpListener, clients_limit: usize, rooms_limit: usize) -> HWServer { |
21 let mut rooms = Slab::with_capacity(rooms_limit); |
21 let mut rooms = Slab::with_capacity(rooms_limit); |
22 let token = rooms.insert(HWRoom::new()).ok().expect("Cannot create lobby"); |
22 let token = rooms.insert(HWRoom::new()); |
23 HWServer { |
23 HWServer { |
24 listener: listener, |
24 listener: listener, |
25 clients: Slab::with_capacity(clients_limit), |
25 clients: Slab::with_capacity(clients_limit), |
26 rooms: rooms, |
26 rooms: rooms, |
27 lobby_id: token, |
27 lobby_id: token, |
36 pub fn accept(&mut self, poll: &Poll) -> io::Result<()> { |
36 pub fn accept(&mut self, poll: &Poll) -> io::Result<()> { |
37 let (sock, addr) = self.listener.accept()?; |
37 let (sock, addr) = self.listener.accept()?; |
38 info!("Connected: {}", addr); |
38 info!("Connected: {}", addr); |
39 |
39 |
40 let client = HWClient::new(sock); |
40 let client = HWClient::new(sock); |
41 let token = self.clients.insert(client) |
41 let token = self.clients.insert(client); |
42 .ok().expect("could not add connection to slab"); |
|
43 |
42 |
44 self.clients[token].id = token; |
43 self.clients[token].id = token; |
45 self.clients[token].register(poll, token); |
44 self.clients[token].register(poll, Token(token)); |
46 |
45 |
47 Ok(()) |
46 Ok(()) |
48 } |
47 } |
49 |
48 |
50 pub fn client_readable(&mut self, poll: &Poll, |
49 pub fn client_readable(&mut self, poll: &Poll, |
51 token: Token) -> io::Result<()> { |
50 token: usize) -> io::Result<()> { |
52 let actions; |
51 let actions; |
53 { |
52 { |
54 actions = self.clients[token].readable(poll); |
53 actions = self.clients[token].readable(poll); |
55 } |
54 } |
56 |
55 |
58 |
57 |
59 Ok(()) |
58 Ok(()) |
60 } |
59 } |
61 |
60 |
62 pub fn client_writable(&mut self, poll: &Poll, |
61 pub fn client_writable(&mut self, poll: &Poll, |
63 token: Token) -> io::Result<()> { |
62 token: usize) -> io::Result<()> { |
64 self.clients[token].writable(poll)?; |
63 self.clients[token].writable(poll)?; |
65 |
64 |
66 Ok(()) |
65 Ok(()) |
67 } |
66 } |
68 |
67 |
69 pub fn client_error(&mut self, poll: &Poll, |
68 pub fn client_error(&mut self, poll: &Poll, |
70 token: Token) -> io::Result<()> { |
69 token: usize) -> io::Result<()> { |
71 let actions; |
70 let actions; |
72 { |
71 { |
73 actions = self.clients[token].error(poll); |
72 actions = self.clients[token].error(poll); |
74 } |
73 } |
75 |
74 |
76 self.react(token, poll, actions); |
75 self.react(token, poll, actions); |
77 |
76 |
78 Ok(()) |
77 Ok(()) |
79 } |
78 } |
80 |
79 |
81 pub fn send(&mut self, token: Token, msg: &String) { |
80 pub fn send(&mut self, token: usize, msg: &String) { |
82 self.clients[token].send_string(msg); |
81 self.clients[token].send_string(msg); |
83 } |
82 } |
84 |
83 |
85 pub fn react(&mut self, token: Token, poll: &Poll, actions: Vec<actions::Action>) { |
84 pub fn react(&mut self, token: usize, poll: &Poll, actions: Vec<actions::Action>) { |
86 for action in actions { |
85 for action in actions { |
87 actions::run_action(self, token, poll, action); |
86 actions::run_action(self, token, poll, action); |
88 } |
87 } |
89 } |
88 } |
90 } |
89 } |
91 |
90 |
92 |
91 |
93 pub struct HWRoom { |
92 pub struct HWRoom { |
94 pub id: Token, |
93 pub id: usize, |
95 pub name: String, |
94 pub name: String, |
96 pub password: Option<String>, |
95 pub password: Option<String>, |
97 pub protocol_number: u32, |
96 pub protocol_number: u32, |
98 pub ready_players_number: u8, |
97 pub ready_players_number: u8, |
99 } |
98 } |
100 |
99 |
101 impl HWRoom { |
100 impl HWRoom { |
102 pub fn new() -> HWRoom { |
101 pub fn new() -> HWRoom { |
103 HWRoom { |
102 HWRoom { |
104 id: Token(0), |
103 id: 0, |
105 name: String::new(), |
104 name: String::new(), |
106 password: None, |
105 password: None, |
107 protocol_number: 0, |
106 protocol_number: 0, |
108 ready_players_number: 0, |
107 ready_players_number: 0, |
109 } |
108 } |