author | unc0rr |
Wed, 18 Jan 2017 22:15:55 +0300 | |
changeset 12139 | f3121d7dedec |
parent 12138 | e0bf51609062 |
child 12141 | 78925eff02c2 |
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::*; |
|
12139 | 11 |
use protocol::messages::HWProtocolMessage::*; |
12126 | 12 |
|
12127 | 13 |
type Slab<T> = slab::Slab<T, Token>; |
14 |
||
12126 | 15 |
pub struct HWServer { |
16 |
listener: TcpListener, |
|
17 |
clients: Slab<HWClient>, |
|
18 |
rooms: Slab<HWRoom> |
|
19 |
} |
|
20 |
||
21 |
impl HWServer { |
|
22 |
pub fn new(listener: TcpListener, clients_limit: usize, rooms_limit: usize) -> HWServer { |
|
23 |
HWServer { |
|
24 |
listener: listener, |
|
25 |
clients: Slab::with_capacity(clients_limit), |
|
26 |
rooms: Slab::with_capacity(rooms_limit), |
|
27 |
} |
|
28 |
} |
|
29 |
||
30 |
pub fn register(&self, poll: &Poll) -> io::Result<()> { |
|
31 |
poll.register(&self.listener, utils::SERVER, Ready::readable(), |
|
32 |
PollOpt::edge()) |
|
33 |
} |
|
34 |
||
35 |
pub fn accept(&mut self, poll: &Poll) -> io::Result<()> { |
|
12129 | 36 |
let (sock, addr) = self.listener.accept()?; |
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
37 |
info!("Connected: {}", addr); |
12126 | 38 |
|
39 |
let client = HWClient::new(sock); |
|
40 |
let token = self.clients.insert(client) |
|
41 |
.ok().expect("could not add connection to slab"); |
|
42 |
||
12127 | 43 |
self.clients[token].register(poll, token); |
12126 | 44 |
|
45 |
Ok(()) |
|
46 |
} |
|
12127 | 47 |
|
48 |
pub fn client_readable(&mut self, poll: &Poll, |
|
49 |
token: Token) -> io::Result<()> { |
|
12138 | 50 |
let actions; |
51 |
{ |
|
52 |
actions = self.clients[token].readable(poll); |
|
53 |
} |
|
54 |
||
12139 | 55 |
self.react(token, poll, actions); |
56 |
||
12138 | 57 |
Ok(()) |
12127 | 58 |
} |
59 |
||
60 |
pub fn client_writable(&mut self, poll: &Poll, |
|
61 |
token: Token) -> io::Result<()> { |
|
12139 | 62 |
self.clients[token].writable(poll)?; |
63 |
||
64 |
Ok(()) |
|
12127 | 65 |
} |
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
66 |
|
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
67 |
pub fn client_error(&mut self, poll: &Poll, |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
68 |
token: Token) -> io::Result<()> { |
12139 | 69 |
let actions; |
70 |
{ |
|
71 |
actions = self.clients[token].error(poll); |
|
72 |
} |
|
73 |
||
74 |
self.react(token, poll, actions); |
|
75 |
||
76 |
Ok(()) |
|
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
77 |
} |
12138 | 78 |
|
12139 | 79 |
fn react(&mut self, token: Token, poll: &Poll, actions: Vec<Action>) { |
80 |
for action in actions { |
|
81 |
match action { |
|
82 |
SendMe(msg) => self.clients[token].send_string(&msg), |
|
83 |
ByeClient(msg) => { |
|
84 |
self.react(token, poll, vec![ |
|
85 |
SendMe(Bye(&msg).to_raw_protocol()), |
|
86 |
RemoveClient, |
|
87 |
]); |
|
88 |
}, |
|
89 |
RemoveClient => { |
|
90 |
self.clients[token].deregister(poll); |
|
91 |
self.clients.remove(token); |
|
92 |
}, |
|
93 |
//_ => unimplemented!(), |
|
94 |
} |
|
12138 | 95 |
} |
96 |
} |
|
12126 | 97 |
} |
98 |
||
12127 | 99 |
|
12126 | 100 |
struct HWRoom { |
101 |
name: String |
|
102 |
} |