author | unc0rr |
Sun, 15 Jan 2017 00:34:36 +0300 | |
changeset 12138 | e0bf51609062 |
parent 12137 | 193dfdcb0620 |
child 12139 | f3121d7dedec |
permissions | -rw-r--r-- |
12127 | 1 |
use slab; |
12126 | 2 |
use mio::tcp::*; |
3 |
use mio::*; |
|
4 |
use std::io::Write; |
|
5 |
use std::io; |
|
6 |
||
7 |
use utils; |
|
12128 | 8 |
use server::client::HWClient; |
12138 | 9 |
use server::actions::Action; |
10 |
use server::actions::Action::*; |
|
12126 | 11 |
|
12127 | 12 |
type Slab<T> = slab::Slab<T, Token>; |
13 |
||
12126 | 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<()> { |
|
12129 | 35 |
let (sock, addr) = self.listener.accept()?; |
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
36 |
info!("Connected: {}", addr); |
12126 | 37 |
|
38 |
let client = HWClient::new(sock); |
|
39 |
let token = self.clients.insert(client) |
|
40 |
.ok().expect("could not add connection to slab"); |
|
41 |
||
12127 | 42 |
self.clients[token].register(poll, token); |
12126 | 43 |
|
44 |
Ok(()) |
|
45 |
} |
|
12127 | 46 |
|
47 |
pub fn client_readable(&mut self, poll: &Poll, |
|
48 |
token: Token) -> io::Result<()> { |
|
12138 | 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(()) |
|
12127 | 58 |
} |
59 |
||
60 |
pub fn client_writable(&mut self, poll: &Poll, |
|
61 |
token: Token) -> io::Result<()> { |
|
62 |
self.clients[token].writable(poll) |
|
63 |
} |
|
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
64 |
|
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
65 |
pub fn client_error(&mut self, poll: &Poll, |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
66 |
token: Token) -> io::Result<()> { |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
67 |
self.clients[token].error(poll) |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
68 |
} |
12138 | 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 |
} |
|
12126 | 76 |
} |
77 |
||
12127 | 78 |
|
12126 | 79 |
struct HWRoom { |
80 |
name: String |
|
81 |
} |