author | unc0rr |
Sun, 15 Jan 2017 00:34:36 +0300 | |
changeset 12143 | e0bf51609062 |
parent 12142 | 193dfdcb0620 |
child 12144 | f3121d7dedec |
permissions | -rw-r--r-- |
12132 | 1 |
use slab; |
12131 | 2 |
use mio::tcp::*; |
3 |
use mio::*; |
|
4 |
use std::io::Write; |
|
5 |
use std::io; |
|
6 |
||
7 |
use utils; |
|
12133 | 8 |
use server::client::HWClient; |
12143 | 9 |
use server::actions::Action; |
10 |
use server::actions::Action::*; |
|
12131 | 11 |
|
12132 | 12 |
type Slab<T> = slab::Slab<T, Token>; |
13 |
||
12131 | 14 |
pub struct HWServer { |
15 |
listener: TcpListener, |
|
16 |
clients: Slab<HWClient>, |
|
17 |
rooms: Slab<HWRoom> |
|
18 |
} |
|
19 |
||
20 |
impl HWServer { |
|
21 |
pub fn new(listener: TcpListener, clients_limit: usize, rooms_limit: usize) -> HWServer { |
|
22 |
HWServer { |
|
23 |
listener: listener, |
|
24 |
clients: Slab::with_capacity(clients_limit), |
|
25 |
rooms: Slab::with_capacity(rooms_limit), |
|
26 |
} |
|
27 |
} |
|
28 |
||
29 |
pub fn register(&self, poll: &Poll) -> io::Result<()> { |
|
30 |
poll.register(&self.listener, utils::SERVER, Ready::readable(), |
|
31 |
PollOpt::edge()) |
|
32 |
} |
|
33 |
||
34 |
pub fn accept(&mut self, poll: &Poll) -> io::Result<()> { |
|
12134 | 35 |
let (sock, addr) = self.listener.accept()?; |
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
36 |
info!("Connected: {}", addr); |
12131 | 37 |
|
38 |
let client = HWClient::new(sock); |
|
39 |
let token = self.clients.insert(client) |
|
40 |
.ok().expect("could not add connection to slab"); |
|
41 |
||
12132 | 42 |
self.clients[token].register(poll, token); |
12131 | 43 |
|
44 |
Ok(()) |
|
45 |
} |
|
12132 | 46 |
|
47 |
pub fn client_readable(&mut self, poll: &Poll, |
|
48 |
token: Token) -> io::Result<()> { |
|
12143 | 49 |
let actions; |
50 |
{ |
|
51 |
actions = self.clients[token].readable(poll); |
|
52 |
} |
|
53 |
||
54 |
for action in actions { |
|
55 |
self.react(token, action); |
|
56 |
} |
|
57 |
Ok(()) |
|
12132 | 58 |
} |
59 |
||
60 |
pub fn client_writable(&mut self, poll: &Poll, |
|
61 |
token: Token) -> io::Result<()> { |
|
62 |
self.clients[token].writable(poll) |
|
63 |
} |
|
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
64 |
|
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
65 |
pub fn client_error(&mut self, poll: &Poll, |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
66 |
token: Token) -> io::Result<()> { |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
67 |
self.clients[token].error(poll) |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
68 |
} |
12143 | 69 |
|
70 |
fn react(&mut self, token: Token, action: Action) { |
|
71 |
match action { |
|
72 |
SendMe(msg) => self.clients[token].send_string(&msg), |
|
73 |
//_ => unimplemented!(), |
|
74 |
} |
|
75 |
} |
|
12131 | 76 |
} |
77 |
||
12132 | 78 |
|
12131 | 79 |
struct HWRoom { |
80 |
name: String |
|
81 |
} |