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