author | Wuzzy <almikes@aol.com> |
Sun, 16 Apr 2017 03:24:17 +0200 | |
changeset 12272 | dad24eb53873 |
parent 12153 | 8591375271b8 |
child 12857 | bd35cb2302b3 |
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 |
|
12151
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12149
diff
changeset
|
40 |
let client = HWClient::new(sock); |
12131 | 41 |
let token = self.clients.insert(client) |
42 |
.ok().expect("could not add connection to slab"); |
|
43 |
||
12151
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12149
diff
changeset
|
44 |
self.clients[token].id = token; |
12132 | 45 |
self.clients[token].register(poll, token); |
12131 | 46 |
|
47 |
Ok(()) |
|
48 |
} |
|
12132 | 49 |
|
50 |
pub fn client_readable(&mut self, poll: &Poll, |
|
51 |
token: Token) -> io::Result<()> { |
|
12143 | 52 |
let actions; |
53 |
{ |
|
54 |
actions = self.clients[token].readable(poll); |
|
55 |
} |
|
56 |
||
12144 | 57 |
self.react(token, poll, actions); |
58 |
||
12143 | 59 |
Ok(()) |
12132 | 60 |
} |
61 |
||
62 |
pub fn client_writable(&mut self, poll: &Poll, |
|
63 |
token: Token) -> io::Result<()> { |
|
12144 | 64 |
self.clients[token].writable(poll)?; |
65 |
||
66 |
Ok(()) |
|
12132 | 67 |
} |
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
68 |
|
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
69 |
pub fn client_error(&mut self, poll: &Poll, |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
70 |
token: Token) -> io::Result<()> { |
12144 | 71 |
let actions; |
72 |
{ |
|
73 |
actions = self.clients[token].error(poll); |
|
74 |
} |
|
75 |
||
76 |
self.react(token, poll, actions); |
|
77 |
||
78 |
Ok(()) |
|
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
79 |
} |
12143 | 80 |
|
12148 | 81 |
pub fn send(&mut self, token: Token, msg: &String) { |
12146 | 82 |
self.clients[token].send_string(msg); |
83 |
} |
|
84 |
||
12149 | 85 |
pub fn react(&mut self, token: Token, poll: &Poll, actions: Vec<actions::Action>) { |
12144 | 86 |
for action in actions { |
12149 | 87 |
actions::run_action(self, token, poll, action); |
12143 | 88 |
} |
89 |
} |
|
12131 | 90 |
} |
91 |
||
12132 | 92 |
|
12148 | 93 |
pub struct HWRoom { |
12152 | 94 |
pub id: Token, |
12148 | 95 |
pub name: String, |
12152 | 96 |
pub password: Option<String>, |
12153 | 97 |
pub protocol_number: u32, |
12152 | 98 |
pub ready_players_number: u8, |
12131 | 99 |
} |
12146 | 100 |
|
101 |
impl HWRoom { |
|
102 |
pub fn new() -> HWRoom { |
|
103 |
HWRoom { |
|
12152 | 104 |
id: Token(0), |
12146 | 105 |
name: String::new(), |
12152 | 106 |
password: None, |
12153 | 107 |
protocol_number: 0, |
12152 | 108 |
ready_players_number: 0, |
12146 | 109 |
} |
110 |
} |
|
111 |
} |