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