author | unc0rr |
Wed, 18 Jan 2017 22:15:55 +0300 | |
changeset 12144 | f3121d7dedec |
parent 12143 | e0bf51609062 |
child 12146 | 78925eff02c2 |
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::*; |
|
12144 | 11 |
use protocol::messages::HWProtocolMessage::*; |
12131 | 12 |
|
12132 | 13 |
type Slab<T> = slab::Slab<T, Token>; |
14 |
||
12131 | 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<()> { |
|
12134 | 36 |
let (sock, addr) = self.listener.accept()?; |
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
37 |
info!("Connected: {}", addr); |
12131 | 38 |
|
39 |
let client = HWClient::new(sock); |
|
40 |
let token = self.clients.insert(client) |
|
41 |
.ok().expect("could not add connection to slab"); |
|
42 |
||
12132 | 43 |
self.clients[token].register(poll, token); |
12131 | 44 |
|
45 |
Ok(()) |
|
46 |
} |
|
12132 | 47 |
|
48 |
pub fn client_readable(&mut self, poll: &Poll, |
|
49 |
token: Token) -> io::Result<()> { |
|
12143 | 50 |
let actions; |
51 |
{ |
|
52 |
actions = self.clients[token].readable(poll); |
|
53 |
} |
|
54 |
||
12144 | 55 |
self.react(token, poll, actions); |
56 |
||
12143 | 57 |
Ok(()) |
12132 | 58 |
} |
59 |
||
60 |
pub fn client_writable(&mut self, poll: &Poll, |
|
61 |
token: Token) -> io::Result<()> { |
|
12144 | 62 |
self.clients[token].writable(poll)?; |
63 |
||
64 |
Ok(()) |
|
12132 | 65 |
} |
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
66 |
|
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
67 |
pub fn client_error(&mut self, poll: &Poll, |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
68 |
token: Token) -> io::Result<()> { |
12144 | 69 |
let actions; |
70 |
{ |
|
71 |
actions = self.clients[token].error(poll); |
|
72 |
} |
|
73 |
||
74 |
self.react(token, poll, actions); |
|
75 |
||
76 |
Ok(()) |
|
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
77 |
} |
12143 | 78 |
|
12144 | 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 |
} |
|
12143 | 95 |
} |
96 |
} |
|
12131 | 97 |
} |
98 |
||
12132 | 99 |
|
12131 | 100 |
struct HWRoom { |
101 |
name: String |
|
102 |
} |