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