author | unc0rr |
Tue, 24 Jan 2017 20:28:16 +0300 | |
changeset 12143 | 7e874846afe3 |
parent 12142 | 4d7d41be1993 |
child 12144 | 589a2d7d3dc5 |
permissions | -rw-r--r-- |
12127 | 1 |
use slab; |
12126 | 2 |
use mio::tcp::*; |
3 |
use mio::*; |
|
4 |
use std::io; |
|
5 |
||
6 |
use utils; |
|
12128 | 7 |
use server::client::HWClient; |
12138 | 8 |
use server::actions::Action; |
12143 | 9 |
use super::handlers; |
12126 | 10 |
|
12127 | 11 |
type Slab<T> = slab::Slab<T, Token>; |
12 |
||
12126 | 13 |
pub struct HWServer { |
14 |
listener: TcpListener, |
|
12143 | 15 |
pub clients: Slab<HWClient>, |
16 |
pub rooms: Slab<HWRoom>, |
|
17 |
pub lobby_id: Token, |
|
12126 | 18 |
} |
19 |
||
20 |
impl HWServer { |
|
21 |
pub fn new(listener: TcpListener, clients_limit: usize, rooms_limit: usize) -> HWServer { |
|
12141 | 22 |
let mut rooms = Slab::with_capacity(rooms_limit); |
23 |
let token = rooms.insert(HWRoom::new()).ok().expect("Cannot create lobby"); |
|
12126 | 24 |
HWServer { |
25 |
listener: listener, |
|
26 |
clients: Slab::with_capacity(clients_limit), |
|
12141 | 27 |
rooms: rooms, |
12143 | 28 |
lobby_id: token, |
12126 | 29 |
} |
30 |
} |
|
31 |
||
32 |
pub fn register(&self, poll: &Poll) -> io::Result<()> { |
|
33 |
poll.register(&self.listener, utils::SERVER, Ready::readable(), |
|
34 |
PollOpt::edge()) |
|
35 |
} |
|
36 |
||
37 |
pub fn accept(&mut self, poll: &Poll) -> io::Result<()> { |
|
12129 | 38 |
let (sock, addr) = self.listener.accept()?; |
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
39 |
info!("Connected: {}", addr); |
12126 | 40 |
|
12143 | 41 |
let client = HWClient::new(sock, &self.lobby_id); |
12126 | 42 |
let token = self.clients.insert(client) |
43 |
.ok().expect("could not add connection to slab"); |
|
44 |
||
12127 | 45 |
self.clients[token].register(poll, token); |
12126 | 46 |
|
47 |
Ok(()) |
|
48 |
} |
|
12127 | 49 |
|
50 |
pub fn client_readable(&mut self, poll: &Poll, |
|
51 |
token: Token) -> io::Result<()> { |
|
12138 | 52 |
let actions; |
53 |
{ |
|
54 |
actions = self.clients[token].readable(poll); |
|
55 |
} |
|
56 |
||
12139 | 57 |
self.react(token, poll, actions); |
58 |
||
12138 | 59 |
Ok(()) |
12127 | 60 |
} |
61 |
||
62 |
pub fn client_writable(&mut self, poll: &Poll, |
|
63 |
token: Token) -> io::Result<()> { |
|
12139 | 64 |
self.clients[token].writable(poll)?; |
65 |
||
66 |
Ok(()) |
|
12127 | 67 |
} |
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
68 |
|
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
69 |
pub fn client_error(&mut self, poll: &Poll, |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
70 |
token: Token) -> io::Result<()> { |
12139 | 71 |
let actions; |
72 |
{ |
|
73 |
actions = self.clients[token].error(poll); |
|
74 |
} |
|
75 |
||
76 |
self.react(token, poll, actions); |
|
77 |
||
78 |
Ok(()) |
|
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
79 |
} |
12138 | 80 |
|
12143 | 81 |
pub fn send(&mut self, token: Token, msg: &String) { |
12141 | 82 |
self.clients[token].send_string(msg); |
83 |
} |
|
84 |
||
12143 | 85 |
pub fn react(&mut self, token: Token, poll: &Poll, actions: Vec<Action>) { |
12139 | 86 |
for action in actions { |
12143 | 87 |
handlers::handle(self, token, poll, action); |
12138 | 88 |
} |
89 |
} |
|
12126 | 90 |
} |
91 |
||
12127 | 92 |
|
12143 | 93 |
pub struct HWRoom { |
94 |
pub name: String, |
|
12126 | 95 |
} |
12141 | 96 |
|
97 |
impl HWRoom { |
|
98 |
pub fn new() -> HWRoom { |
|
99 |
HWRoom { |
|
100 |
name: String::new(), |
|
101 |
} |
|
102 |
} |
|
103 |
} |