author | unc0rr |
Mon, 23 Jan 2017 23:43:29 +0300 | |
changeset 12142 | 4d7d41be1993 |
parent 12141 | 78925eff02c2 |
child 12143 | 7e874846afe3 |
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::*; |
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
12 |
use protocol::messages::HWServerMessage; |
12126 | 13 |
|
12127 | 14 |
type Slab<T> = slab::Slab<T, Token>; |
15 |
||
12126 | 16 |
pub struct HWServer { |
17 |
listener: TcpListener, |
|
18 |
clients: Slab<HWClient>, |
|
12141 | 19 |
rooms: Slab<HWRoom>, |
20 |
lobbyId: Token, |
|
12126 | 21 |
} |
22 |
||
23 |
impl HWServer { |
|
24 |
pub fn new(listener: TcpListener, clients_limit: usize, rooms_limit: usize) -> HWServer { |
|
12141 | 25 |
let mut rooms = Slab::with_capacity(rooms_limit); |
26 |
let token = rooms.insert(HWRoom::new()).ok().expect("Cannot create lobby"); |
|
12126 | 27 |
HWServer { |
28 |
listener: listener, |
|
29 |
clients: Slab::with_capacity(clients_limit), |
|
12141 | 30 |
rooms: rooms, |
31 |
lobbyId: token, |
|
12126 | 32 |
} |
33 |
} |
|
34 |
||
35 |
pub fn register(&self, poll: &Poll) -> io::Result<()> { |
|
36 |
poll.register(&self.listener, utils::SERVER, Ready::readable(), |
|
37 |
PollOpt::edge()) |
|
38 |
} |
|
39 |
||
40 |
pub fn accept(&mut self, poll: &Poll) -> io::Result<()> { |
|
12129 | 41 |
let (sock, addr) = self.listener.accept()?; |
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
42 |
info!("Connected: {}", addr); |
12126 | 43 |
|
12141 | 44 |
let client = HWClient::new(sock, &self.lobbyId); |
12126 | 45 |
let token = self.clients.insert(client) |
46 |
.ok().expect("could not add connection to slab"); |
|
47 |
||
12127 | 48 |
self.clients[token].register(poll, token); |
12126 | 49 |
|
50 |
Ok(()) |
|
51 |
} |
|
12127 | 52 |
|
53 |
pub fn client_readable(&mut self, poll: &Poll, |
|
54 |
token: Token) -> io::Result<()> { |
|
12138 | 55 |
let actions; |
56 |
{ |
|
57 |
actions = self.clients[token].readable(poll); |
|
58 |
} |
|
59 |
||
12139 | 60 |
self.react(token, poll, actions); |
61 |
||
12138 | 62 |
Ok(()) |
12127 | 63 |
} |
64 |
||
65 |
pub fn client_writable(&mut self, poll: &Poll, |
|
66 |
token: Token) -> io::Result<()> { |
|
12139 | 67 |
self.clients[token].writable(poll)?; |
68 |
||
69 |
Ok(()) |
|
12127 | 70 |
} |
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
71 |
|
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
72 |
pub fn client_error(&mut self, poll: &Poll, |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
73 |
token: Token) -> io::Result<()> { |
12139 | 74 |
let actions; |
75 |
{ |
|
76 |
actions = self.clients[token].error(poll); |
|
77 |
} |
|
78 |
||
79 |
self.react(token, poll, actions); |
|
80 |
||
81 |
Ok(()) |
|
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
82 |
} |
12138 | 83 |
|
12141 | 84 |
fn send(&mut self, token: Token, msg: &String) { |
85 |
self.clients[token].send_string(msg); |
|
86 |
} |
|
87 |
||
12139 | 88 |
fn react(&mut self, token: Token, poll: &Poll, actions: Vec<Action>) { |
89 |
for action in actions { |
|
90 |
match action { |
|
12141 | 91 |
SendMe(msg) => self.send(token, &msg), |
12139 | 92 |
ByeClient(msg) => { |
93 |
self.react(token, poll, vec![ |
|
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
94 |
SendMe(HWServerMessage::Bye(&msg).to_raw_protocol()), |
12139 | 95 |
RemoveClient, |
96 |
]); |
|
97 |
}, |
|
98 |
RemoveClient => { |
|
99 |
self.clients[token].deregister(poll); |
|
100 |
self.clients.remove(token); |
|
101 |
}, |
|
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
102 |
ReactProtocolMessage(msg) => match msg { |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
103 |
Ping => self.react(token, poll, vec![SendMe(HWServerMessage::Pong.to_raw_protocol())]), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
104 |
Quit(Some(msg)) => self.react(token, poll, vec![ByeClient("User quit: ".to_string() + &msg)]), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
105 |
Quit(None) => self.react(token, poll, vec![ByeClient("User quit".to_string())]), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
106 |
Nick(nick) => if self.clients[token].nick.len() == 0 { |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
107 |
self.send(token, &HWServerMessage::Nick(&nick).to_raw_protocol()); |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
108 |
self.clients[token].nick = nick; |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
109 |
}, |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
110 |
Malformed => warn!("Malformed/unknown message"), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
111 |
Empty => warn!("Empty message"), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
112 |
_ => unimplemented!(), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
113 |
} |
12139 | 114 |
//_ => unimplemented!(), |
115 |
} |
|
12138 | 116 |
} |
117 |
} |
|
12126 | 118 |
} |
119 |
||
12127 | 120 |
|
12126 | 121 |
struct HWRoom { |
122 |
name: String |
|
123 |
} |
|
12141 | 124 |
|
125 |
impl HWRoom { |
|
126 |
pub fn new() -> HWRoom { |
|
127 |
HWRoom { |
|
128 |
name: String::new(), |
|
129 |
} |
|
130 |
} |
|
131 |
} |