author | unc0rr |
Wed, 18 Jan 2017 22:54:02 +0300 | |
changeset 12146 | 78925eff02c2 |
parent 12144 | f3121d7dedec |
child 12147 | 4d7d41be1993 |
permissions | -rw-r--r-- |
12132 | 1 |
use slab; |
12131 | 2 |
use mio::tcp::*; |
3 |
use mio::*; |
|
4 |
use std::io::Write; |
|
5 |
use std::io; |
|
6 |
||
7 |
use utils; |
|
12133 | 8 |
use server::client::HWClient; |
12143 | 9 |
use server::actions::Action; |
10 |
use server::actions::Action::*; |
|
12144 | 11 |
use protocol::messages::HWProtocolMessage::*; |
12131 | 12 |
|
12132 | 13 |
type Slab<T> = slab::Slab<T, Token>; |
14 |
||
12131 | 15 |
pub struct HWServer { |
16 |
listener: TcpListener, |
|
17 |
clients: Slab<HWClient>, |
|
12146 | 18 |
rooms: Slab<HWRoom>, |
19 |
lobbyId: Token, |
|
12131 | 20 |
} |
21 |
||
22 |
impl HWServer { |
|
23 |
pub fn new(listener: TcpListener, clients_limit: usize, rooms_limit: usize) -> HWServer { |
|
12146 | 24 |
let mut rooms = Slab::with_capacity(rooms_limit); |
25 |
let token = rooms.insert(HWRoom::new()).ok().expect("Cannot create lobby"); |
|
12131 | 26 |
HWServer { |
27 |
listener: listener, |
|
28 |
clients: Slab::with_capacity(clients_limit), |
|
12146 | 29 |
rooms: rooms, |
30 |
lobbyId: token, |
|
12131 | 31 |
} |
32 |
} |
|
33 |
||
34 |
pub fn register(&self, poll: &Poll) -> io::Result<()> { |
|
35 |
poll.register(&self.listener, utils::SERVER, Ready::readable(), |
|
36 |
PollOpt::edge()) |
|
37 |
} |
|
38 |
||
39 |
pub fn accept(&mut self, poll: &Poll) -> io::Result<()> { |
|
12134 | 40 |
let (sock, addr) = self.listener.accept()?; |
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
41 |
info!("Connected: {}", addr); |
12131 | 42 |
|
12146 | 43 |
let client = HWClient::new(sock, &self.lobbyId); |
12131 | 44 |
let token = self.clients.insert(client) |
45 |
.ok().expect("could not add connection to slab"); |
|
46 |
||
12132 | 47 |
self.clients[token].register(poll, token); |
12131 | 48 |
|
49 |
Ok(()) |
|
50 |
} |
|
12132 | 51 |
|
52 |
pub fn client_readable(&mut self, poll: &Poll, |
|
53 |
token: Token) -> io::Result<()> { |
|
12143 | 54 |
let actions; |
55 |
{ |
|
56 |
actions = self.clients[token].readable(poll); |
|
57 |
} |
|
58 |
||
12144 | 59 |
self.react(token, poll, actions); |
60 |
||
12143 | 61 |
Ok(()) |
12132 | 62 |
} |
63 |
||
64 |
pub fn client_writable(&mut self, poll: &Poll, |
|
65 |
token: Token) -> io::Result<()> { |
|
12144 | 66 |
self.clients[token].writable(poll)?; |
67 |
||
68 |
Ok(()) |
|
12132 | 69 |
} |
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
70 |
|
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
71 |
pub fn client_error(&mut self, poll: &Poll, |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
72 |
token: Token) -> io::Result<()> { |
12144 | 73 |
let actions; |
74 |
{ |
|
75 |
actions = self.clients[token].error(poll); |
|
76 |
} |
|
77 |
||
78 |
self.react(token, poll, actions); |
|
79 |
||
80 |
Ok(()) |
|
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
81 |
} |
12143 | 82 |
|
12146 | 83 |
fn send(&mut self, token: Token, msg: &String) { |
84 |
self.clients[token].send_string(msg); |
|
85 |
} |
|
86 |
||
12144 | 87 |
fn react(&mut self, token: Token, poll: &Poll, actions: Vec<Action>) { |
88 |
for action in actions { |
|
89 |
match action { |
|
12146 | 90 |
SendMe(msg) => self.send(token, &msg), |
12144 | 91 |
ByeClient(msg) => { |
92 |
self.react(token, poll, vec![ |
|
93 |
SendMe(Bye(&msg).to_raw_protocol()), |
|
94 |
RemoveClient, |
|
95 |
]); |
|
96 |
}, |
|
12146 | 97 |
SetNick(nick) => { |
98 |
self.send(token, &Nick(&nick).to_raw_protocol()); |
|
99 |
self.clients[token].nick = nick; |
|
100 |
} |
|
12144 | 101 |
RemoveClient => { |
102 |
self.clients[token].deregister(poll); |
|
103 |
self.clients.remove(token); |
|
104 |
}, |
|
105 |
//_ => unimplemented!(), |
|
106 |
} |
|
12143 | 107 |
} |
108 |
} |
|
12131 | 109 |
} |
110 |
||
12132 | 111 |
|
12131 | 112 |
struct HWRoom { |
113 |
name: String |
|
114 |
} |
|
12146 | 115 |
|
116 |
impl HWRoom { |
|
117 |
pub fn new() -> HWRoom { |
|
118 |
HWRoom { |
|
119 |
name: String::new(), |
|
120 |
} |
|
121 |
} |
|
122 |
} |