author | alfadur |
Thu, 14 Jun 2018 12:31:15 -0400 | |
changeset 13419 | 28b314ad566d |
parent 13124 | 1e39b8749072 |
child 13421 | cdf69667593b |
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; |
|
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
7 |
use super::client::*; |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
8 |
use super::room::*; |
12149 | 9 |
use super::actions; |
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
10 |
use protocol::messages::*; |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
11 |
use super::handlers; |
12131 | 12 |
|
12857 | 13 |
type Slab<T> = slab::Slab<T>; |
12132 | 14 |
|
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
15 |
pub enum Destination { |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
16 |
ToSelf(ClientId), |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
17 |
ToOthers(ClientId) |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
18 |
} |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
19 |
|
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
20 |
pub struct PendingMessage(pub Destination, pub HWServerMessage); |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
21 |
|
12131 | 22 |
pub struct HWServer { |
12148 | 23 |
pub clients: Slab<HWClient>, |
24 |
pub rooms: Slab<HWRoom>, |
|
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
25 |
pub lobby_id: RoomId, |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
26 |
pub output: Vec<PendingMessage>, |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
27 |
pub removed_clients: Vec<ClientId>, |
12131 | 28 |
} |
29 |
||
30 |
impl HWServer { |
|
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
31 |
pub fn new(clients_limit: usize, rooms_limit: usize) -> HWServer { |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
32 |
let rooms = Slab::with_capacity(rooms_limit); |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
33 |
let clients = Slab::with_capacity(clients_limit); |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
34 |
let mut server = HWServer { |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
35 |
clients, rooms, |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
36 |
lobby_id: 0, |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
37 |
output: vec![], |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
38 |
removed_clients: vec![] |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
39 |
}; |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
40 |
server.lobby_id = server.add_room(); |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
41 |
server |
12131 | 42 |
} |
43 |
||
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
44 |
pub fn add_client(&mut self) -> ClientId { |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
45 |
let key: ClientId; |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
46 |
{ |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
47 |
let entry = self.clients.vacant_entry(); |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
48 |
key = entry.key(); |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
49 |
let client = HWClient::new(entry.key()); |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
50 |
entry.insert(client); |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
51 |
} |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
52 |
self.send_self(key, HWServerMessage::Connected(utils::PROTOCOL_VERSION)); |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
53 |
key |
12131 | 54 |
} |
12132 | 55 |
|
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
56 |
pub fn client_lost(&mut self, client_id: ClientId) { |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
57 |
actions::run_action(self, client_id, |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
58 |
actions::Action::ByeClient("Connection reset".to_string())); |
12132 | 59 |
} |
60 |
||
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
61 |
pub fn add_room(&mut self) -> RoomId { |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
62 |
let entry = self.rooms.vacant_entry(); |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
63 |
let key = entry.key(); |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
64 |
let room = HWRoom::new(entry.key()); |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
65 |
entry.insert(room); |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
66 |
key |
12132 | 67 |
} |
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
68 |
|
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
69 |
pub fn handle_msg(&mut self, client_id: ClientId, msg: HWProtocolMessage) { |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
70 |
handlers::handle(self, client_id, msg); |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
71 |
} |
12144 | 72 |
|
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
73 |
pub fn send_self(&mut self, client_id: ClientId, msg: HWServerMessage) { |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
74 |
self.output.push(PendingMessage( |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
75 |
Destination::ToSelf(client_id), msg)); |
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
76 |
} |
12143 | 77 |
|
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
78 |
pub fn send_others(&mut self, client_id: ClientId, msg: HWServerMessage) { |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
79 |
self.output.push(PendingMessage( |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
80 |
Destination::ToOthers(client_id), msg)); |
12146 | 81 |
} |
82 |
||
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
83 |
pub fn react(&mut self, client_id: ClientId, actions: Vec<actions::Action>) { |
12144 | 84 |
for action in actions { |
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
85 |
actions::run_action(self, client_id, action); |
12143 | 86 |
} |
87 |
} |
|
12131 | 88 |
} |