13 type Slab<T> = slab::Slab<T, Token>; |
13 type Slab<T> = slab::Slab<T, Token>; |
14 |
14 |
15 pub struct HWServer { |
15 pub struct HWServer { |
16 listener: TcpListener, |
16 listener: TcpListener, |
17 clients: Slab<HWClient>, |
17 clients: Slab<HWClient>, |
18 rooms: Slab<HWRoom> |
18 rooms: Slab<HWRoom>, |
|
19 lobbyId: Token, |
19 } |
20 } |
20 |
21 |
21 impl HWServer { |
22 impl HWServer { |
22 pub fn new(listener: TcpListener, clients_limit: usize, rooms_limit: usize) -> HWServer { |
23 pub fn new(listener: TcpListener, clients_limit: usize, rooms_limit: usize) -> HWServer { |
|
24 let mut rooms = Slab::with_capacity(rooms_limit); |
|
25 let token = rooms.insert(HWRoom::new()).ok().expect("Cannot create lobby"); |
23 HWServer { |
26 HWServer { |
24 listener: listener, |
27 listener: listener, |
25 clients: Slab::with_capacity(clients_limit), |
28 clients: Slab::with_capacity(clients_limit), |
26 rooms: Slab::with_capacity(rooms_limit), |
29 rooms: rooms, |
|
30 lobbyId: token, |
27 } |
31 } |
28 } |
32 } |
29 |
33 |
30 pub fn register(&self, poll: &Poll) -> io::Result<()> { |
34 pub fn register(&self, poll: &Poll) -> io::Result<()> { |
31 poll.register(&self.listener, utils::SERVER, Ready::readable(), |
35 poll.register(&self.listener, utils::SERVER, Ready::readable(), |
34 |
38 |
35 pub fn accept(&mut self, poll: &Poll) -> io::Result<()> { |
39 pub fn accept(&mut self, poll: &Poll) -> io::Result<()> { |
36 let (sock, addr) = self.listener.accept()?; |
40 let (sock, addr) = self.listener.accept()?; |
37 info!("Connected: {}", addr); |
41 info!("Connected: {}", addr); |
38 |
42 |
39 let client = HWClient::new(sock); |
43 let client = HWClient::new(sock, &self.lobbyId); |
40 let token = self.clients.insert(client) |
44 let token = self.clients.insert(client) |
41 .ok().expect("could not add connection to slab"); |
45 .ok().expect("could not add connection to slab"); |
42 |
46 |
43 self.clients[token].register(poll, token); |
47 self.clients[token].register(poll, token); |
44 |
48 |
74 self.react(token, poll, actions); |
78 self.react(token, poll, actions); |
75 |
79 |
76 Ok(()) |
80 Ok(()) |
77 } |
81 } |
78 |
82 |
|
83 fn send(&mut self, token: Token, msg: &String) { |
|
84 self.clients[token].send_string(msg); |
|
85 } |
|
86 |
79 fn react(&mut self, token: Token, poll: &Poll, actions: Vec<Action>) { |
87 fn react(&mut self, token: Token, poll: &Poll, actions: Vec<Action>) { |
80 for action in actions { |
88 for action in actions { |
81 match action { |
89 match action { |
82 SendMe(msg) => self.clients[token].send_string(&msg), |
90 SendMe(msg) => self.send(token, &msg), |
83 ByeClient(msg) => { |
91 ByeClient(msg) => { |
84 self.react(token, poll, vec![ |
92 self.react(token, poll, vec![ |
85 SendMe(Bye(&msg).to_raw_protocol()), |
93 SendMe(Bye(&msg).to_raw_protocol()), |
86 RemoveClient, |
94 RemoveClient, |
87 ]); |
95 ]); |
88 }, |
96 }, |
|
97 SetNick(nick) => { |
|
98 self.send(token, &Nick(&nick).to_raw_protocol()); |
|
99 self.clients[token].nick = nick; |
|
100 } |
89 RemoveClient => { |
101 RemoveClient => { |
90 self.clients[token].deregister(poll); |
102 self.clients[token].deregister(poll); |
91 self.clients.remove(token); |
103 self.clients.remove(token); |
92 }, |
104 }, |
93 //_ => unimplemented!(), |
105 //_ => unimplemented!(), |