author | Wuzzy <Wuzzy2@mail.ru> |
Fri, 27 Oct 2017 05:05:37 +0200 | |
changeset 12783 | 7849398ec0fa |
parent 12148 | 8591375271b8 |
child 12852 | bd35cb2302b3 |
permissions | -rw-r--r-- |
12127 | 1 |
use slab; |
12126 | 2 |
use mio::tcp::*; |
3 |
use mio::*; |
|
4 |
use std::io; |
|
5 |
||
6 |
use utils; |
|
12144 | 7 |
use super::client::HWClient; |
8 |
use super::actions; |
|
12126 | 9 |
|
12127 | 10 |
type Slab<T> = slab::Slab<T, Token>; |
11 |
||
12126 | 12 |
pub struct HWServer { |
13 |
listener: TcpListener, |
|
12143 | 14 |
pub clients: Slab<HWClient>, |
15 |
pub rooms: Slab<HWRoom>, |
|
16 |
pub lobby_id: Token, |
|
12126 | 17 |
} |
18 |
||
19 |
impl HWServer { |
|
20 |
pub fn new(listener: TcpListener, clients_limit: usize, rooms_limit: usize) -> HWServer { |
|
12141 | 21 |
let mut rooms = Slab::with_capacity(rooms_limit); |
22 |
let token = rooms.insert(HWRoom::new()).ok().expect("Cannot create lobby"); |
|
12126 | 23 |
HWServer { |
24 |
listener: listener, |
|
25 |
clients: Slab::with_capacity(clients_limit), |
|
12141 | 26 |
rooms: rooms, |
12143 | 27 |
lobby_id: token, |
12126 | 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<()> { |
|
12129 | 37 |
let (sock, addr) = self.listener.accept()?; |
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
38 |
info!("Connected: {}", addr); |
12126 | 39 |
|
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12144
diff
changeset
|
40 |
let client = HWClient::new(sock); |
12126 | 41 |
let token = self.clients.insert(client) |
42 |
.ok().expect("could not add connection to slab"); |
|
43 |
||
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12144
diff
changeset
|
44 |
self.clients[token].id = token; |
12127 | 45 |
self.clients[token].register(poll, token); |
12126 | 46 |
|
47 |
Ok(()) |
|
48 |
} |
|
12127 | 49 |
|
50 |
pub fn client_readable(&mut self, poll: &Poll, |
|
51 |
token: Token) -> io::Result<()> { |
|
12138 | 52 |
let actions; |
53 |
{ |
|
54 |
actions = self.clients[token].readable(poll); |
|
55 |
} |
|
56 |
||
12139 | 57 |
self.react(token, poll, actions); |
58 |
||
12138 | 59 |
Ok(()) |
12127 | 60 |
} |
61 |
||
62 |
pub fn client_writable(&mut self, poll: &Poll, |
|
63 |
token: Token) -> io::Result<()> { |
|
12139 | 64 |
self.clients[token].writable(poll)?; |
65 |
||
66 |
Ok(()) |
|
12127 | 67 |
} |
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
68 |
|
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
69 |
pub fn client_error(&mut self, poll: &Poll, |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
70 |
token: Token) -> io::Result<()> { |
12139 | 71 |
let actions; |
72 |
{ |
|
73 |
actions = self.clients[token].error(poll); |
|
74 |
} |
|
75 |
||
76 |
self.react(token, poll, actions); |
|
77 |
||
78 |
Ok(()) |
|
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
79 |
} |
12138 | 80 |
|
12143 | 81 |
pub fn send(&mut self, token: Token, msg: &String) { |
12141 | 82 |
self.clients[token].send_string(msg); |
83 |
} |
|
84 |
||
12144 | 85 |
pub fn react(&mut self, token: Token, poll: &Poll, actions: Vec<actions::Action>) { |
12139 | 86 |
for action in actions { |
12144 | 87 |
actions::run_action(self, token, poll, action); |
12138 | 88 |
} |
89 |
} |
|
12126 | 90 |
} |
91 |
||
12127 | 92 |
|
12143 | 93 |
pub struct HWRoom { |
12147 | 94 |
pub id: Token, |
12143 | 95 |
pub name: String, |
12147 | 96 |
pub password: Option<String>, |
12148 | 97 |
pub protocol_number: u32, |
12147 | 98 |
pub ready_players_number: u8, |
12126 | 99 |
} |
12141 | 100 |
|
101 |
impl HWRoom { |
|
102 |
pub fn new() -> HWRoom { |
|
103 |
HWRoom { |
|
12147 | 104 |
id: Token(0), |
12141 | 105 |
name: String::new(), |
12147 | 106 |
password: None, |
12148 | 107 |
protocol_number: 0, |
12147 | 108 |
ready_players_number: 0, |
12141 | 109 |
} |
110 |
} |
|
111 |
} |