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