author | alfadur <mail@none> |
Thu, 07 Feb 2019 17:02:24 +0300 | |
changeset 14714 | 6a2e13e36b7f |
parent 14694 | 08a8605bafaf |
child 14717 | 8a45c90f4580 |
permissions | -rw-r--r-- |
13119 | 1 |
extern crate slab; |
2 |
||
13414 | 3 |
use std::{ |
13415 | 4 |
collections::HashSet, |
14478 | 5 |
io, |
6 |
io::{Error, ErrorKind, Read, Write}, |
|
7 |
mem::{replace, swap}, |
|
8 |
net::{IpAddr, Ipv4Addr, SocketAddr}, |
|
13414 | 9 |
}; |
10 |
||
14478 | 11 |
use log::*; |
13414 | 12 |
use mio::{ |
14478 | 13 |
net::{TcpListener, TcpStream}, |
14 |
Poll, PollOpt, Ready, Token, |
|
13414 | 15 |
}; |
16 |
use netbuf; |
|
13119 | 17 |
use slab::Slab; |
18 |
||
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
19 |
use super::{core::HWServer, coretypes::ClientId, handlers, io::FileServerIO}; |
13666 | 20 |
use crate::{ |
14478 | 21 |
protocol::{messages::*, ProtocolDecoder}, |
13666 | 22 |
utils, |
13414 | 23 |
}; |
13773 | 24 |
#[cfg(feature = "tls-connections")] |
25 |
use openssl::{ |
|
14478 | 26 |
error::ErrorStack, |
13773 | 27 |
ssl::{ |
14478 | 28 |
HandshakeError, MidHandshakeSslStream, Ssl, SslContext, SslContextBuilder, SslFiletype, |
29 |
SslMethod, SslOptions, SslStream, SslStreamBuilder, SslVerifyMode, |
|
13773 | 30 |
}, |
31 |
}; |
|
13414 | 32 |
|
33 |
const MAX_BYTES_PER_READ: usize = 2048; |
|
13119 | 34 |
|
13415 | 35 |
#[derive(Hash, Eq, PartialEq, Copy, Clone)] |
13414 | 36 |
pub enum NetworkClientState { |
37 |
Idle, |
|
38 |
NeedsWrite, |
|
39 |
NeedsRead, |
|
40 |
Closed, |
|
41 |
} |
|
42 |
||
43 |
type NetworkResult<T> = io::Result<(T, NetworkClientState)>; |
|
13119 | 44 |
|
13773 | 45 |
#[cfg(not(feature = "tls-connections"))] |
46 |
pub enum ClientSocket { |
|
14478 | 47 |
Plain(TcpStream), |
13773 | 48 |
} |
49 |
||
50 |
#[cfg(feature = "tls-connections")] |
|
51 |
pub enum ClientSocket { |
|
52 |
SslHandshake(Option<MidHandshakeSslStream<TcpStream>>), |
|
14478 | 53 |
SslStream(SslStream<TcpStream>), |
13773 | 54 |
} |
55 |
||
56 |
impl ClientSocket { |
|
57 |
fn inner(&self) -> &TcpStream { |
|
58 |
#[cfg(not(feature = "tls-connections"))] |
|
59 |
match self { |
|
60 |
ClientSocket::Plain(stream) => stream, |
|
61 |
} |
|
62 |
||
63 |
#[cfg(feature = "tls-connections")] |
|
64 |
match self { |
|
65 |
ClientSocket::SslHandshake(Some(builder)) => builder.get_ref(), |
|
66 |
ClientSocket::SslHandshake(None) => unreachable!(), |
|
14478 | 67 |
ClientSocket::SslStream(ssl_stream) => ssl_stream.get_ref(), |
13773 | 68 |
} |
69 |
} |
|
70 |
} |
|
71 |
||
13119 | 72 |
pub struct NetworkClient { |
73 |
id: ClientId, |
|
13773 | 74 |
socket: ClientSocket, |
13119 | 75 |
peer_addr: SocketAddr, |
76 |
decoder: ProtocolDecoder, |
|
14478 | 77 |
buf_out: netbuf::Buf, |
13119 | 78 |
} |
79 |
||
80 |
impl NetworkClient { |
|
13773 | 81 |
pub fn new(id: ClientId, socket: ClientSocket, peer_addr: SocketAddr) -> NetworkClient { |
13119 | 82 |
NetworkClient { |
14478 | 83 |
id, |
84 |
socket, |
|
85 |
peer_addr, |
|
13119 | 86 |
decoder: ProtocolDecoder::new(), |
14478 | 87 |
buf_out: netbuf::Buf::new(), |
13119 | 88 |
} |
89 |
} |
|
90 |
||
13776 | 91 |
#[cfg(feature = "tls-connections")] |
14478 | 92 |
fn handshake_impl( |
93 |
&mut self, |
|
94 |
handshake: MidHandshakeSslStream<TcpStream>, |
|
95 |
) -> io::Result<NetworkClientState> { |
|
13776 | 96 |
match handshake.handshake() { |
97 |
Ok(stream) => { |
|
98 |
self.socket = ClientSocket::SslStream(stream); |
|
14478 | 99 |
debug!( |
100 |
"TLS handshake with {} ({}) completed", |
|
101 |
self.id, self.peer_addr |
|
102 |
); |
|
13776 | 103 |
Ok(NetworkClientState::Idle) |
104 |
} |
|
105 |
Err(HandshakeError::WouldBlock(new_handshake)) => { |
|
106 |
self.socket = ClientSocket::SslHandshake(Some(new_handshake)); |
|
107 |
Ok(NetworkClientState::Idle) |
|
108 |
} |
|
13777 | 109 |
Err(HandshakeError::Failure(new_handshake)) => { |
110 |
self.socket = ClientSocket::SslHandshake(Some(new_handshake)); |
|
13776 | 111 |
debug!("TLS handshake with {} ({}) failed", self.id, self.peer_addr); |
112 |
Err(Error::new(ErrorKind::Other, "Connection failure")) |
|
113 |
} |
|
14478 | 114 |
Err(HandshakeError::SetupFailure(_)) => unreachable!(), |
13776 | 115 |
} |
116 |
} |
|
117 |
||
14478 | 118 |
fn read_impl<R: Read>( |
119 |
decoder: &mut ProtocolDecoder, |
|
120 |
source: &mut R, |
|
121 |
id: ClientId, |
|
122 |
addr: &SocketAddr, |
|
123 |
) -> NetworkResult<Vec<HWProtocolMessage>> { |
|
13414 | 124 |
let mut bytes_read = 0; |
125 |
let result = loop { |
|
13773 | 126 |
match decoder.read_from(source) { |
13414 | 127 |
Ok(bytes) => { |
13773 | 128 |
debug!("Client {}: read {} bytes", id, bytes); |
13414 | 129 |
bytes_read += bytes; |
130 |
if bytes == 0 { |
|
131 |
let result = if bytes_read == 0 { |
|
13773 | 132 |
info!("EOF for client {} ({})", id, addr); |
13414 | 133 |
(Vec::new(), NetworkClientState::Closed) |
134 |
} else { |
|
13773 | 135 |
(decoder.extract_messages(), NetworkClientState::NeedsRead) |
13414 | 136 |
}; |
137 |
break Ok(result); |
|
14478 | 138 |
} else if bytes_read >= MAX_BYTES_PER_READ { |
139 |
break Ok((decoder.extract_messages(), NetworkClientState::NeedsRead)); |
|
13414 | 140 |
} |
141 |
} |
|
142 |
Err(ref error) if error.kind() == ErrorKind::WouldBlock => { |
|
14478 | 143 |
let messages = if bytes_read == 0 { |
13414 | 144 |
Vec::new() |
145 |
} else { |
|
13773 | 146 |
decoder.extract_messages() |
13414 | 147 |
}; |
148 |
break Ok((messages, NetworkClientState::Idle)); |
|
149 |
} |
|
14478 | 150 |
Err(error) => break Err(error), |
13414 | 151 |
} |
152 |
}; |
|
13773 | 153 |
decoder.sweep(); |
13414 | 154 |
result |
155 |
} |
|
156 |
||
13773 | 157 |
pub fn read(&mut self) -> NetworkResult<Vec<HWProtocolMessage>> { |
158 |
#[cfg(not(feature = "tls-connections"))] |
|
159 |
match self.socket { |
|
14478 | 160 |
ClientSocket::Plain(ref mut stream) => { |
161 |
NetworkClient::read_impl(&mut self.decoder, stream, self.id, &self.peer_addr) |
|
162 |
} |
|
13773 | 163 |
} |
164 |
||
165 |
#[cfg(feature = "tls-connections")] |
|
166 |
match self.socket { |
|
167 |
ClientSocket::SslHandshake(ref mut handshake_opt) => { |
|
13776 | 168 |
let handshake = std::mem::replace(handshake_opt, None).unwrap(); |
169 |
Ok((Vec::new(), self.handshake_impl(handshake)?)) |
|
14478 | 170 |
} |
171 |
ClientSocket::SslStream(ref mut stream) => { |
|
13773 | 172 |
NetworkClient::read_impl(&mut self.decoder, stream, self.id, &self.peer_addr) |
14478 | 173 |
} |
13773 | 174 |
} |
175 |
} |
|
176 |
||
177 |
fn write_impl<W: Write>(buf_out: &mut netbuf::Buf, destination: &mut W) -> NetworkResult<()> { |
|
13414 | 178 |
let result = loop { |
13773 | 179 |
match buf_out.write_to(destination) { |
14478 | 180 |
Ok(bytes) if buf_out.is_empty() || bytes == 0 => { |
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
181 |
break Ok(((), NetworkClientState::Idle)); |
14478 | 182 |
} |
13415 | 183 |
Ok(_) => (), |
14478 | 184 |
Err(ref error) |
185 |
if error.kind() == ErrorKind::Interrupted |
|
186 |
|| error.kind() == ErrorKind::WouldBlock => |
|
187 |
{ |
|
13414 | 188 |
break Ok(((), NetworkClientState::NeedsWrite)); |
14478 | 189 |
} |
190 |
Err(error) => break Err(error), |
|
13414 | 191 |
} |
192 |
}; |
|
13773 | 193 |
result |
194 |
} |
|
195 |
||
196 |
pub fn write(&mut self) -> NetworkResult<()> { |
|
197 |
let result = { |
|
198 |
#[cfg(not(feature = "tls-connections"))] |
|
199 |
match self.socket { |
|
14478 | 200 |
ClientSocket::Plain(ref mut stream) => { |
13773 | 201 |
NetworkClient::write_impl(&mut self.buf_out, stream) |
14478 | 202 |
} |
13773 | 203 |
} |
204 |
||
14478 | 205 |
#[cfg(feature = "tls-connections")] |
206 |
{ |
|
13773 | 207 |
match self.socket { |
13776 | 208 |
ClientSocket::SslHandshake(ref mut handshake_opt) => { |
209 |
let handshake = std::mem::replace(handshake_opt, None).unwrap(); |
|
210 |
Ok(((), self.handshake_impl(handshake)?)) |
|
211 |
} |
|
14478 | 212 |
ClientSocket::SslStream(ref mut stream) => { |
13773 | 213 |
NetworkClient::write_impl(&mut self.buf_out, stream) |
14478 | 214 |
} |
13773 | 215 |
} |
216 |
} |
|
217 |
}; |
|
218 |
||
219 |
self.socket.inner().flush()?; |
|
13414 | 220 |
result |
221 |
} |
|
222 |
||
13119 | 223 |
pub fn send_raw_msg(&mut self, msg: &[u8]) { |
13500 | 224 |
self.buf_out.write_all(msg).unwrap(); |
13119 | 225 |
} |
226 |
||
13500 | 227 |
pub fn send_string(&mut self, msg: &str) { |
13119 | 228 |
self.send_raw_msg(&msg.as_bytes()); |
229 |
} |
|
230 |
||
13500 | 231 |
pub fn send_msg(&mut self, msg: &HWServerMessage) { |
13119 | 232 |
self.send_string(&msg.to_raw_protocol()); |
233 |
} |
|
234 |
} |
|
235 |
||
13773 | 236 |
#[cfg(feature = "tls-connections")] |
237 |
struct ServerSsl { |
|
14478 | 238 |
context: SslContext, |
13773 | 239 |
} |
240 |
||
13119 | 241 |
pub struct NetworkLayer { |
242 |
listener: TcpListener, |
|
243 |
server: HWServer, |
|
13414 | 244 |
clients: Slab<NetworkClient>, |
13415 | 245 |
pending: HashSet<(ClientId, NetworkClientState)>, |
13773 | 246 |
pending_cache: Vec<(ClientId, NetworkClientState)>, |
247 |
#[cfg(feature = "tls-connections")] |
|
14478 | 248 |
ssl: ServerSsl, |
13119 | 249 |
} |
250 |
||
251 |
impl NetworkLayer { |
|
252 |
pub fn new(listener: TcpListener, clients_limit: usize, rooms_limit: usize) -> NetworkLayer { |
|
14413 | 253 |
let server = HWServer::new(clients_limit, rooms_limit, Box::new(FileServerIO::new())); |
13119 | 254 |
let clients = Slab::with_capacity(clients_limit); |
13415 | 255 |
let pending = HashSet::with_capacity(2 * clients_limit); |
256 |
let pending_cache = Vec::with_capacity(2 * clients_limit); |
|
13773 | 257 |
|
258 |
NetworkLayer { |
|
14478 | 259 |
listener, |
260 |
server, |
|
261 |
clients, |
|
262 |
pending, |
|
263 |
pending_cache, |
|
13773 | 264 |
#[cfg(feature = "tls-connections")] |
14478 | 265 |
ssl: NetworkLayer::create_ssl_context(), |
13773 | 266 |
} |
267 |
} |
|
268 |
||
269 |
#[cfg(feature = "tls-connections")] |
|
270 |
fn create_ssl_context() -> ServerSsl { |
|
271 |
let mut builder = SslContextBuilder::new(SslMethod::tls()).unwrap(); |
|
272 |
builder.set_verify(SslVerifyMode::NONE); |
|
273 |
builder.set_read_ahead(true); |
|
14478 | 274 |
builder |
275 |
.set_certificate_file("ssl/cert.pem", SslFiletype::PEM) |
|
276 |
.unwrap(); |
|
277 |
builder |
|
278 |
.set_private_key_file("ssl/key.pem", SslFiletype::PEM) |
|
279 |
.unwrap(); |
|
13773 | 280 |
builder.set_options(SslOptions::NO_COMPRESSION); |
281 |
builder.set_cipher_list("DEFAULT:!LOW:!RC4:!EXP").unwrap(); |
|
14478 | 282 |
ServerSsl { |
283 |
context: builder.build(), |
|
284 |
} |
|
13119 | 285 |
} |
286 |
||
287 |
pub fn register_server(&self, poll: &Poll) -> io::Result<()> { |
|
14478 | 288 |
poll.register( |
289 |
&self.listener, |
|
290 |
utils::SERVER, |
|
291 |
Ready::readable(), |
|
292 |
PollOpt::edge(), |
|
293 |
) |
|
13119 | 294 |
} |
295 |
||
296 |
fn deregister_client(&mut self, poll: &Poll, id: ClientId) { |
|
297 |
let mut client_exists = false; |
|
13414 | 298 |
if let Some(ref client) = self.clients.get(id) { |
13773 | 299 |
poll.deregister(client.socket.inner()) |
13500 | 300 |
.expect("could not deregister socket"); |
13119 | 301 |
info!("client {} ({}) removed", client.id, client.peer_addr); |
302 |
client_exists = true; |
|
303 |
} |
|
304 |
if client_exists { |
|
305 |
self.clients.remove(id); |
|
306 |
} |
|
307 |
} |
|
308 |
||
14478 | 309 |
fn register_client( |
310 |
&mut self, |
|
311 |
poll: &Poll, |
|
312 |
client_socket: ClientSocket, |
|
313 |
addr: SocketAddr, |
|
14714 | 314 |
) -> ClientId { |
315 |
let entry = self.clients.vacant_entry(); |
|
316 |
let client_id = entry.key(); |
|
317 |
||
14478 | 318 |
poll.register( |
319 |
client_socket.inner(), |
|
14714 | 320 |
Token(client_id), |
14478 | 321 |
Ready::readable() | Ready::writable(), |
322 |
PollOpt::edge(), |
|
323 |
) |
|
324 |
.expect("could not register socket with event loop"); |
|
13119 | 325 |
|
14714 | 326 |
let client = NetworkClient::new(client_id, client_socket, addr); |
13119 | 327 |
info!("client {} ({}) added", client.id, client.peer_addr); |
328 |
entry.insert(client); |
|
14714 | 329 |
|
330 |
client_id |
|
13119 | 331 |
} |
332 |
||
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
333 |
fn flush_server_messages(&mut self, mut response: handlers::Response) { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
334 |
debug!("{} pending server messages", response.len()); |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
335 |
let output = response.extract_messages(&mut self.server); |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
336 |
for (clients, message) in output { |
13419 | 337 |
debug!("Message {:?} to {:?}", message, clients); |
338 |
let msg_string = message.to_raw_protocol(); |
|
339 |
for client_id in clients { |
|
340 |
if let Some(client) = self.clients.get_mut(client_id) { |
|
341 |
client.send_string(&msg_string); |
|
14478 | 342 |
self.pending |
343 |
.insert((client_id, NetworkClientState::NeedsWrite)); |
|
13414 | 344 |
} |
345 |
} |
|
346 |
} |
|
347 |
} |
|
348 |
||
13773 | 349 |
fn create_client_socket(&self, socket: TcpStream) -> io::Result<ClientSocket> { |
14478 | 350 |
#[cfg(not(feature = "tls-connections"))] |
351 |
{ |
|
13773 | 352 |
Ok(ClientSocket::Plain(socket)) |
353 |
} |
|
354 |
||
14478 | 355 |
#[cfg(feature = "tls-connections")] |
356 |
{ |
|
13773 | 357 |
let ssl = Ssl::new(&self.ssl.context).unwrap(); |
358 |
let mut builder = SslStreamBuilder::new(ssl, socket); |
|
359 |
builder.set_accept_state(); |
|
360 |
match builder.handshake() { |
|
14478 | 361 |
Ok(stream) => Ok(ClientSocket::SslStream(stream)), |
362 |
Err(HandshakeError::WouldBlock(stream)) => { |
|
363 |
Ok(ClientSocket::SslHandshake(Some(stream))) |
|
364 |
} |
|
13773 | 365 |
Err(e) => { |
366 |
debug!("OpenSSL handshake failed: {}", e); |
|
367 |
Err(Error::new(ErrorKind::Other, "Connection failure")) |
|
368 |
} |
|
369 |
} |
|
370 |
} |
|
371 |
} |
|
372 |
||
13119 | 373 |
pub fn accept_client(&mut self, poll: &Poll) -> io::Result<()> { |
374 |
let (client_socket, addr) = self.listener.accept()?; |
|
375 |
info!("Connected: {}", addr); |
|
376 |
||
14714 | 377 |
let client_id = self.register_client(poll, self.create_client_socket(client_socket)?, addr); |
378 |
||
379 |
let mut response = handlers::Response::new(client_id); |
|
380 |
||
381 |
handlers::handle_client_accept(&mut self.server, client_id, &mut response); |
|
382 |
||
383 |
if !response.is_empty() { |
|
384 |
self.flush_server_messages(response); |
|
385 |
} |
|
13119 | 386 |
|
387 |
Ok(()) |
|
388 |
} |
|
389 |
||
14478 | 390 |
fn operation_failed( |
391 |
&mut self, |
|
392 |
poll: &Poll, |
|
393 |
client_id: ClientId, |
|
394 |
error: &Error, |
|
395 |
msg: &str, |
|
396 |
) -> io::Result<()> { |
|
13414 | 397 |
let addr = if let Some(ref mut client) = self.clients.get_mut(client_id) { |
398 |
client.peer_addr |
|
399 |
} else { |
|
400 |
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0) |
|
401 |
}; |
|
402 |
debug!("{}({}): {}", msg, addr, error); |
|
403 |
self.client_error(poll, client_id) |
|
13119 | 404 |
} |
405 |
||
14478 | 406 |
pub fn client_readable(&mut self, poll: &Poll, client_id: ClientId) -> io::Result<()> { |
407 |
let messages = if let Some(ref mut client) = self.clients.get_mut(client_id) { |
|
408 |
client.read() |
|
409 |
} else { |
|
410 |
warn!("invalid readable client: {}", client_id); |
|
411 |
Ok((Vec::new(), NetworkClientState::Idle)) |
|
412 |
}; |
|
13414 | 413 |
|
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
414 |
let mut response = handlers::Response::new(client_id); |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
415 |
|
13414 | 416 |
match messages { |
417 |
Ok((messages, state)) => { |
|
418 |
for message in messages { |
|
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
419 |
debug!("Handling message {:?} for client {}", message, client_id); |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
420 |
if self.server.clients.contains(client_id) { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
421 |
handlers::handle(&mut self.server, client_id, &mut response, message); |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
422 |
} |
13414 | 423 |
} |
424 |
match state { |
|
13415 | 425 |
NetworkClientState::NeedsRead => { |
426 |
self.pending.insert((client_id, state)); |
|
14478 | 427 |
} |
428 |
NetworkClientState::Closed => self.client_error(&poll, client_id)?, |
|
13414 | 429 |
_ => {} |
430 |
}; |
|
13119 | 431 |
} |
13414 | 432 |
Err(e) => self.operation_failed( |
14478 | 433 |
poll, |
434 |
client_id, |
|
435 |
&e, |
|
436 |
"Error while reading from client socket", |
|
437 |
)?, |
|
13119 | 438 |
} |
439 |
||
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
440 |
if !response.is_empty() { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
441 |
self.flush_server_messages(response); |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
442 |
} |
13414 | 443 |
|
13119 | 444 |
if !self.server.removed_clients.is_empty() { |
13414 | 445 |
let ids: Vec<_> = self.server.removed_clients.drain(..).collect(); |
13119 | 446 |
for client_id in ids { |
447 |
self.deregister_client(poll, client_id); |
|
448 |
} |
|
449 |
} |
|
450 |
||
451 |
Ok(()) |
|
452 |
} |
|
453 |
||
14478 | 454 |
pub fn client_writable(&mut self, poll: &Poll, client_id: ClientId) -> io::Result<()> { |
455 |
let result = if let Some(ref mut client) = self.clients.get_mut(client_id) { |
|
456 |
client.write() |
|
457 |
} else { |
|
458 |
warn!("invalid writable client: {}", client_id); |
|
459 |
Ok(((), NetworkClientState::Idle)) |
|
460 |
}; |
|
13414 | 461 |
|
462 |
match result { |
|
13415 | 463 |
Ok(((), state)) if state == NetworkClientState::NeedsWrite => { |
464 |
self.pending.insert((client_id, state)); |
|
14478 | 465 |
} |
13415 | 466 |
Ok(_) => {} |
14478 | 467 |
Err(e) => { |
468 |
self.operation_failed(poll, client_id, &e, "Error while writing to client socket")? |
|
469 |
} |
|
13119 | 470 |
} |
471 |
||
472 |
Ok(()) |
|
473 |
} |
|
474 |
||
14478 | 475 |
pub fn client_error(&mut self, poll: &Poll, client_id: ClientId) -> io::Result<()> { |
13119 | 476 |
self.deregister_client(poll, client_id); |
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
477 |
let mut response = handlers::Response::new(client_id); |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
478 |
handlers::handle_client_loss(&mut self.server, client_id, &mut response); |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
479 |
self.flush_server_messages(response); |
13119 | 480 |
|
481 |
Ok(()) |
|
482 |
} |
|
13414 | 483 |
|
484 |
pub fn has_pending_operations(&self) -> bool { |
|
485 |
!self.pending.is_empty() |
|
486 |
} |
|
487 |
||
488 |
pub fn on_idle(&mut self, poll: &Poll) -> io::Result<()> { |
|
13415 | 489 |
if self.has_pending_operations() { |
13450 | 490 |
let mut cache = replace(&mut self.pending_cache, Vec::new()); |
13415 | 491 |
cache.extend(self.pending.drain()); |
492 |
for (id, state) in cache.drain(..) { |
|
493 |
match state { |
|
14478 | 494 |
NetworkClientState::NeedsRead => self.client_readable(poll, id)?, |
495 |
NetworkClientState::NeedsWrite => self.client_writable(poll, id)?, |
|
13415 | 496 |
_ => {} |
497 |
} |
|
13414 | 498 |
} |
13415 | 499 |
swap(&mut cache, &mut self.pending_cache); |
13414 | 500 |
} |
501 |
Ok(()) |
|
502 |
} |
|
13119 | 503 |
} |