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