author | unc0rr |
Wed, 06 Dec 2017 23:46:17 +0100 | |
changeset 12857 | bd35cb2302b3 |
parent 12153 | 8591375271b8 |
child 13124 | 1e39b8749072 |
permissions | -rw-r--r-- |
12132 | 1 |
use slab; |
12857 | 2 |
use mio::net::*; |
12131 | 3 |
use mio::*; |
4 |
use std::io; |
|
5 |
||
6 |
use utils; |
|
12149 | 7 |
use super::client::HWClient; |
8 |
use super::actions; |
|
12131 | 9 |
|
12857 | 10 |
type Slab<T> = slab::Slab<T>; |
12132 | 11 |
|
12131 | 12 |
pub struct HWServer { |
13 |
listener: TcpListener, |
|
12148 | 14 |
pub clients: Slab<HWClient>, |
15 |
pub rooms: Slab<HWRoom>, |
|
12857 | 16 |
pub lobby_id: usize, |
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); |
12857 | 22 |
let token = rooms.insert(HWRoom::new()); |
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); |
12857 | 41 |
let token = self.clients.insert(client); |
12131 | 42 |
|
12151
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12149
diff
changeset
|
43 |
self.clients[token].id = token; |
12857 | 44 |
self.clients[token].register(poll, Token(token)); |
12131 | 45 |
|
46 |
Ok(()) |
|
47 |
} |
|
12132 | 48 |
|
49 |
pub fn client_readable(&mut self, poll: &Poll, |
|
12857 | 50 |
token: usize) -> 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, |
|
12857 | 62 |
token: usize) -> 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, |
12857 | 69 |
token: usize) -> 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 |
|
12857 | 80 |
pub fn send(&mut self, token: usize, msg: &String) { |
12146 | 81 |
self.clients[token].send_string(msg); |
82 |
} |
|
83 |
||
12857 | 84 |
pub fn react(&mut self, token: usize, 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 { |
12857 | 93 |
pub id: usize, |
12148 | 94 |
pub name: String, |
12152 | 95 |
pub password: Option<String>, |
12153 | 96 |
pub protocol_number: u32, |
12152 | 97 |
pub ready_players_number: u8, |
12131 | 98 |
} |
12146 | 99 |
|
100 |
impl HWRoom { |
|
101 |
pub fn new() -> HWRoom { |
|
102 |
HWRoom { |
|
12857 | 103 |
id: 0, |
12146 | 104 |
name: String::new(), |
12152 | 105 |
password: None, |
12153 | 106 |
protocol_number: 0, |
12152 | 107 |
ready_players_number: 0, |
12146 | 108 |
} |
109 |
} |
|
110 |
} |