author | Wuzzy <Wuzzy2@mail.ru> |
Thu, 18 Jul 2019 01:46:40 +0200 | |
changeset 15255 | 8dd3602a4500 |
parent 15181 | f6115638aa92 |
permissions | -rw-r--r-- |
13124 | 1 |
extern crate slab; |
2 |
||
13419 | 3 |
use std::{ |
13420 | 4 |
collections::HashSet, |
14462 | 5 |
io, |
6 |
io::{Error, ErrorKind, Read, Write}, |
|
7 |
mem::{replace, swap}, |
|
8 |
net::{IpAddr, Ipv4Addr, SocketAddr}, |
|
13419 | 9 |
}; |
10 |
||
14462 | 11 |
use log::*; |
13419 | 12 |
use mio::{ |
14462 | 13 |
net::{TcpListener, TcpStream}, |
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
14 |
Evented, Poll, PollOpt, Ready, Token, |
13419 | 15 |
}; |
14808 | 16 |
use mio_extras::timer; |
13419 | 17 |
use netbuf; |
13124 | 18 |
use slab::Slab; |
19 |
||
13671 | 20 |
use crate::{ |
15080 | 21 |
core::{server::HwServer, types::ClientId}, |
15079 | 22 |
handlers, |
15107 | 23 |
handlers::{IoResult, IoTask}, |
15108
823052e66611
check for account existence before asking passwords
alfadur
parents:
15107
diff
changeset
|
24 |
protocol::{messages::HwServerMessage::Redirect, messages::*, ProtocolDecoder}, |
13671 | 25 |
utils, |
13419 | 26 |
}; |
14784
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
27 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
28 |
#[cfg(feature = "official-server")] |
15103 | 29 |
use super::io::{IoThread, RequestId}; |
14784
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
30 |
|
13804 | 31 |
#[cfg(feature = "tls-connections")] |
32 |
use openssl::{ |
|
14462 | 33 |
error::ErrorStack, |
13804 | 34 |
ssl::{ |
14462 | 35 |
HandshakeError, MidHandshakeSslStream, Ssl, SslContext, SslContextBuilder, SslFiletype, |
36 |
SslMethod, SslOptions, SslStream, SslStreamBuilder, SslVerifyMode, |
|
13804 | 37 |
}, |
38 |
}; |
|
14808 | 39 |
use std::time::Duration; |
13419 | 40 |
|
41 |
const MAX_BYTES_PER_READ: usize = 2048; |
|
14808 | 42 |
const SEND_PING_TIMEOUT: Duration = Duration::from_secs(30); |
14812 | 43 |
const DROP_CLIENT_TIMEOUT: Duration = Duration::from_secs(30); |
44 |
const PING_PROBES_COUNT: u8 = 2; |
|
13124 | 45 |
|
13420 | 46 |
#[derive(Hash, Eq, PartialEq, Copy, Clone)] |
13419 | 47 |
pub enum NetworkClientState { |
48 |
Idle, |
|
49 |
NeedsWrite, |
|
50 |
NeedsRead, |
|
51 |
Closed, |
|
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
52 |
#[cfg(feature = "tls-connections")] |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
53 |
Connected, |
13419 | 54 |
} |
55 |
||
56 |
type NetworkResult<T> = io::Result<(T, NetworkClientState)>; |
|
13124 | 57 |
|
13804 | 58 |
pub enum ClientSocket { |
14462 | 59 |
Plain(TcpStream), |
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
60 |
#[cfg(feature = "tls-connections")] |
13804 | 61 |
SslHandshake(Option<MidHandshakeSslStream<TcpStream>>), |
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
62 |
#[cfg(feature = "tls-connections")] |
14462 | 63 |
SslStream(SslStream<TcpStream>), |
13804 | 64 |
} |
65 |
||
66 |
impl ClientSocket { |
|
67 |
fn inner(&self) -> &TcpStream { |
|
68 |
match self { |
|
69 |
ClientSocket::Plain(stream) => stream, |
|
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
70 |
#[cfg(feature = "tls-connections")] |
13804 | 71 |
ClientSocket::SslHandshake(Some(builder)) => builder.get_ref(), |
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
72 |
#[cfg(feature = "tls-connections")] |
13804 | 73 |
ClientSocket::SslHandshake(None) => unreachable!(), |
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
74 |
#[cfg(feature = "tls-connections")] |
14462 | 75 |
ClientSocket::SslStream(ssl_stream) => ssl_stream.get_ref(), |
13804 | 76 |
} |
77 |
} |
|
78 |
} |
|
79 |
||
13124 | 80 |
pub struct NetworkClient { |
81 |
id: ClientId, |
|
13804 | 82 |
socket: ClientSocket, |
13124 | 83 |
peer_addr: SocketAddr, |
84 |
decoder: ProtocolDecoder, |
|
14462 | 85 |
buf_out: netbuf::Buf, |
14808 | 86 |
timeout: timer::Timeout, |
15179
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
87 |
pending_close: bool, |
13124 | 88 |
} |
89 |
||
90 |
impl NetworkClient { |
|
14808 | 91 |
pub fn new( |
92 |
id: ClientId, |
|
93 |
socket: ClientSocket, |
|
94 |
peer_addr: SocketAddr, |
|
95 |
timeout: timer::Timeout, |
|
96 |
) -> NetworkClient { |
|
13124 | 97 |
NetworkClient { |
14462 | 98 |
id, |
99 |
socket, |
|
100 |
peer_addr, |
|
13124 | 101 |
decoder: ProtocolDecoder::new(), |
14462 | 102 |
buf_out: netbuf::Buf::new(), |
14808 | 103 |
timeout, |
15179
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
104 |
pending_close: false, |
13124 | 105 |
} |
106 |
} |
|
107 |
||
13807 | 108 |
#[cfg(feature = "tls-connections")] |
14462 | 109 |
fn handshake_impl( |
110 |
&mut self, |
|
111 |
handshake: MidHandshakeSslStream<TcpStream>, |
|
112 |
) -> io::Result<NetworkClientState> { |
|
13807 | 113 |
match handshake.handshake() { |
114 |
Ok(stream) => { |
|
115 |
self.socket = ClientSocket::SslStream(stream); |
|
14462 | 116 |
debug!( |
117 |
"TLS handshake with {} ({}) completed", |
|
118 |
self.id, self.peer_addr |
|
119 |
); |
|
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
120 |
Ok(NetworkClientState::Connected) |
13807 | 121 |
} |
122 |
Err(HandshakeError::WouldBlock(new_handshake)) => { |
|
123 |
self.socket = ClientSocket::SslHandshake(Some(new_handshake)); |
|
124 |
Ok(NetworkClientState::Idle) |
|
125 |
} |
|
13808 | 126 |
Err(HandshakeError::Failure(new_handshake)) => { |
127 |
self.socket = ClientSocket::SslHandshake(Some(new_handshake)); |
|
13807 | 128 |
debug!("TLS handshake with {} ({}) failed", self.id, self.peer_addr); |
129 |
Err(Error::new(ErrorKind::Other, "Connection failure")) |
|
130 |
} |
|
14462 | 131 |
Err(HandshakeError::SetupFailure(_)) => unreachable!(), |
13807 | 132 |
} |
133 |
} |
|
134 |
||
14462 | 135 |
fn read_impl<R: Read>( |
136 |
decoder: &mut ProtocolDecoder, |
|
137 |
source: &mut R, |
|
138 |
id: ClientId, |
|
139 |
addr: &SocketAddr, |
|
15080 | 140 |
) -> NetworkResult<Vec<HwProtocolMessage>> { |
13419 | 141 |
let mut bytes_read = 0; |
142 |
let result = loop { |
|
13804 | 143 |
match decoder.read_from(source) { |
13419 | 144 |
Ok(bytes) => { |
13804 | 145 |
debug!("Client {}: read {} bytes", id, bytes); |
13419 | 146 |
bytes_read += bytes; |
147 |
if bytes == 0 { |
|
148 |
let result = if bytes_read == 0 { |
|
13804 | 149 |
info!("EOF for client {} ({})", id, addr); |
13419 | 150 |
(Vec::new(), NetworkClientState::Closed) |
151 |
} else { |
|
13804 | 152 |
(decoder.extract_messages(), NetworkClientState::NeedsRead) |
13419 | 153 |
}; |
154 |
break Ok(result); |
|
14462 | 155 |
} else if bytes_read >= MAX_BYTES_PER_READ { |
156 |
break Ok((decoder.extract_messages(), NetworkClientState::NeedsRead)); |
|
13419 | 157 |
} |
158 |
} |
|
159 |
Err(ref error) if error.kind() == ErrorKind::WouldBlock => { |
|
14462 | 160 |
let messages = if bytes_read == 0 { |
13419 | 161 |
Vec::new() |
162 |
} else { |
|
13804 | 163 |
decoder.extract_messages() |
13419 | 164 |
}; |
165 |
break Ok((messages, NetworkClientState::Idle)); |
|
166 |
} |
|
14462 | 167 |
Err(error) => break Err(error), |
13419 | 168 |
} |
169 |
}; |
|
170 |
result |
|
171 |
} |
|
172 |
||
15080 | 173 |
pub fn read(&mut self) -> NetworkResult<Vec<HwProtocolMessage>> { |
13804 | 174 |
match self.socket { |
14462 | 175 |
ClientSocket::Plain(ref mut stream) => { |
176 |
NetworkClient::read_impl(&mut self.decoder, stream, self.id, &self.peer_addr) |
|
177 |
} |
|
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
178 |
#[cfg(feature = "tls-connections")] |
13804 | 179 |
ClientSocket::SslHandshake(ref mut handshake_opt) => { |
13807 | 180 |
let handshake = std::mem::replace(handshake_opt, None).unwrap(); |
181 |
Ok((Vec::new(), self.handshake_impl(handshake)?)) |
|
14462 | 182 |
} |
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
183 |
#[cfg(feature = "tls-connections")] |
14462 | 184 |
ClientSocket::SslStream(ref mut stream) => { |
13804 | 185 |
NetworkClient::read_impl(&mut self.decoder, stream, self.id, &self.peer_addr) |
14462 | 186 |
} |
13804 | 187 |
} |
188 |
} |
|
189 |
||
15179
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
190 |
fn write_impl<W: Write>( |
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
191 |
buf_out: &mut netbuf::Buf, |
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
192 |
destination: &mut W, |
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
193 |
close_on_empty: bool, |
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
194 |
) -> NetworkResult<()> { |
13419 | 195 |
let result = loop { |
13804 | 196 |
match buf_out.write_to(destination) { |
14462 | 197 |
Ok(bytes) if buf_out.is_empty() || bytes == 0 => { |
15179
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
198 |
let status = if buf_out.is_empty() && close_on_empty { |
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
199 |
NetworkClientState::Closed |
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
200 |
} else { |
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
201 |
NetworkClientState::Idle |
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
202 |
}; |
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
203 |
break Ok(((), status)); |
14462 | 204 |
} |
13420 | 205 |
Ok(_) => (), |
14462 | 206 |
Err(ref error) |
207 |
if error.kind() == ErrorKind::Interrupted |
|
208 |
|| error.kind() == ErrorKind::WouldBlock => |
|
209 |
{ |
|
13419 | 210 |
break Ok(((), NetworkClientState::NeedsWrite)); |
14462 | 211 |
} |
212 |
Err(error) => break Err(error), |
|
13419 | 213 |
} |
214 |
}; |
|
13804 | 215 |
result |
216 |
} |
|
217 |
||
218 |
pub fn write(&mut self) -> NetworkResult<()> { |
|
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
219 |
let result = match self.socket { |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
220 |
ClientSocket::Plain(ref mut stream) => { |
15179
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
221 |
NetworkClient::write_impl(&mut self.buf_out, stream, self.pending_close) |
13804 | 222 |
} |
14462 | 223 |
#[cfg(feature = "tls-connections")] |
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
224 |
ClientSocket::SslHandshake(ref mut handshake_opt) => { |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
225 |
let handshake = std::mem::replace(handshake_opt, None).unwrap(); |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
226 |
Ok(((), self.handshake_impl(handshake)?)) |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
227 |
} |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
228 |
#[cfg(feature = "tls-connections")] |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
229 |
ClientSocket::SslStream(ref mut stream) => { |
15179
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
230 |
NetworkClient::write_impl(&mut self.buf_out, stream, self.pending_close) |
13804 | 231 |
} |
232 |
}; |
|
233 |
||
234 |
self.socket.inner().flush()?; |
|
13419 | 235 |
result |
236 |
} |
|
237 |
||
13124 | 238 |
pub fn send_raw_msg(&mut self, msg: &[u8]) { |
13529 | 239 |
self.buf_out.write_all(msg).unwrap(); |
13124 | 240 |
} |
241 |
||
13529 | 242 |
pub fn send_string(&mut self, msg: &str) { |
13124 | 243 |
self.send_raw_msg(&msg.as_bytes()); |
244 |
} |
|
14808 | 245 |
|
246 |
pub fn replace_timeout(&mut self, timeout: timer::Timeout) -> timer::Timeout { |
|
247 |
replace(&mut self.timeout, timeout) |
|
248 |
} |
|
15179
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
249 |
|
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
250 |
pub fn has_pending_sends(&self) -> bool { |
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
251 |
!self.buf_out.is_empty() |
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
252 |
} |
13124 | 253 |
} |
254 |
||
13804 | 255 |
#[cfg(feature = "tls-connections")] |
256 |
struct ServerSsl { |
|
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
257 |
listener: TcpListener, |
14462 | 258 |
context: SslContext, |
13804 | 259 |
} |
260 |
||
14784
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
261 |
#[cfg(feature = "official-server")] |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
262 |
pub struct IoLayer { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
263 |
next_request_id: RequestId, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
264 |
request_queue: Vec<(RequestId, ClientId)>, |
15103 | 265 |
io_thread: IoThread, |
14784
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
266 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
267 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
268 |
#[cfg(feature = "official-server")] |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
269 |
impl IoLayer { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
270 |
fn new() -> Self { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
271 |
Self { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
272 |
next_request_id: 0, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
273 |
request_queue: vec![], |
15103 | 274 |
io_thread: IoThread::new(), |
14784
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
275 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
276 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
277 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
278 |
fn send(&mut self, client_id: ClientId, task: IoTask) { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
279 |
let request_id = self.next_request_id; |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
280 |
self.next_request_id += 1; |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
281 |
self.request_queue.push((request_id, client_id)); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
282 |
self.io_thread.send(request_id, task); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
283 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
284 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
285 |
fn try_recv(&mut self) -> Option<(ClientId, IoResult)> { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
286 |
let (request_id, result) = self.io_thread.try_recv()?; |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
287 |
if let Some(index) = self |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
288 |
.request_queue |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
289 |
.iter() |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
290 |
.position(|(id, _)| *id == request_id) |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
291 |
{ |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
292 |
let (_, client_id) = self.request_queue.swap_remove(index); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
293 |
Some((client_id, result)) |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
294 |
} else { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
295 |
None |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
296 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
297 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
298 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
299 |
fn cancel(&mut self, client_id: ClientId) { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
300 |
let mut index = 0; |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
301 |
while index < self.request_queue.len() { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
302 |
if self.request_queue[index].1 == client_id { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
303 |
self.request_queue.swap_remove(index); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
304 |
} else { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
305 |
index += 1; |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
306 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
307 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
308 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
309 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
310 |
|
14808 | 311 |
enum TimeoutEvent { |
14812 | 312 |
SendPing { probes_count: u8 }, |
14808 | 313 |
DropClient, |
314 |
} |
|
315 |
||
316 |
struct TimerData(TimeoutEvent, ClientId); |
|
317 |
||
13124 | 318 |
pub struct NetworkLayer { |
319 |
listener: TcpListener, |
|
15080 | 320 |
server: HwServer, |
13419 | 321 |
clients: Slab<NetworkClient>, |
13420 | 322 |
pending: HashSet<(ClientId, NetworkClientState)>, |
13804 | 323 |
pending_cache: Vec<(ClientId, NetworkClientState)>, |
324 |
#[cfg(feature = "tls-connections")] |
|
14462 | 325 |
ssl: ServerSsl, |
14784
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
326 |
#[cfg(feature = "official-server")] |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
327 |
io: IoLayer, |
14808 | 328 |
timer: timer::Timer<TimerData>, |
329 |
} |
|
330 |
||
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
331 |
fn register_read<E: Evented>(poll: &Poll, evented: &E, token: mio::Token) -> io::Result<()> { |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
332 |
poll.register(evented, token, Ready::readable(), PollOpt::edge()) |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
333 |
} |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
334 |
|
14812 | 335 |
fn create_ping_timeout( |
336 |
timer: &mut timer::Timer<TimerData>, |
|
337 |
probes_count: u8, |
|
338 |
client_id: ClientId, |
|
339 |
) -> timer::Timeout { |
|
14808 | 340 |
timer.set_timeout( |
341 |
SEND_PING_TIMEOUT, |
|
14812 | 342 |
TimerData(TimeoutEvent::SendPing { probes_count }, client_id), |
14808 | 343 |
) |
344 |
} |
|
345 |
||
346 |
fn create_drop_timeout(timer: &mut timer::Timer<TimerData>, client_id: ClientId) -> timer::Timeout { |
|
347 |
timer.set_timeout( |
|
348 |
DROP_CLIENT_TIMEOUT, |
|
349 |
TimerData(TimeoutEvent::DropClient, client_id), |
|
350 |
) |
|
13124 | 351 |
} |
352 |
||
353 |
impl NetworkLayer { |
|
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
354 |
pub fn register(&self, poll: &Poll) -> io::Result<()> { |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
355 |
register_read(poll, &self.listener, utils::SERVER_TOKEN)?; |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
356 |
#[cfg(feature = "tls-connections")] |
14840 | 357 |
register_read(poll, &self.ssl.listener, utils::SECURE_SERVER_TOKEN)?; |
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
358 |
register_read(poll, &self.timer, utils::TIMER_TOKEN)?; |
14808 | 359 |
|
14784
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
360 |
#[cfg(feature = "official-server")] |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
361 |
self.io.io_thread.register_rx(poll, utils::IO_TOKEN)?; |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
362 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
363 |
Ok(()) |
13124 | 364 |
} |
365 |
||
15180 | 366 |
fn deregister_client(&mut self, poll: &Poll, id: ClientId, is_error: bool) { |
15179
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
367 |
if let Some(ref mut client) = self.clients.get_mut(id) { |
13804 | 368 |
poll.deregister(client.socket.inner()) |
13529 | 369 |
.expect("could not deregister socket"); |
15180 | 370 |
if client.has_pending_sends() && !is_error { |
15179
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
371 |
info!( |
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
372 |
"client {} ({}) pending removal", |
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
373 |
client.id, client.peer_addr |
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
374 |
); |
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
375 |
client.pending_close = true; |
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
376 |
poll.register( |
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
377 |
client.socket.inner(), |
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
378 |
Token(id), |
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
379 |
Ready::writable(), |
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
380 |
PollOpt::edge(), |
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
381 |
) |
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
382 |
.unwrap_or_else(|_| { |
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
383 |
self.clients.remove(id); |
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
384 |
}); |
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
385 |
} else { |
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
386 |
info!("client {} ({}) removed", client.id, client.peer_addr); |
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
387 |
self.clients.remove(id); |
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
388 |
} |
14784
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
389 |
#[cfg(feature = "official-server")] |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
390 |
self.io.cancel(id); |
13124 | 391 |
} |
392 |
} |
|
393 |
||
14462 | 394 |
fn register_client( |
395 |
&mut self, |
|
396 |
poll: &Poll, |
|
397 |
client_socket: ClientSocket, |
|
398 |
addr: SocketAddr, |
|
14921 | 399 |
) -> io::Result<ClientId> { |
14698 | 400 |
let entry = self.clients.vacant_entry(); |
401 |
let client_id = entry.key(); |
|
402 |
||
14462 | 403 |
poll.register( |
404 |
client_socket.inner(), |
|
14698 | 405 |
Token(client_id), |
14462 | 406 |
Ready::readable() | Ready::writable(), |
407 |
PollOpt::edge(), |
|
14921 | 408 |
)?; |
13124 | 409 |
|
14808 | 410 |
let client = NetworkClient::new( |
411 |
client_id, |
|
412 |
client_socket, |
|
413 |
addr, |
|
14812 | 414 |
create_ping_timeout(&mut self.timer, PING_PROBES_COUNT - 1, client_id), |
14808 | 415 |
); |
13124 | 416 |
info!("client {} ({}) added", client.id, client.peer_addr); |
417 |
entry.insert(client); |
|
14698 | 418 |
|
14921 | 419 |
Ok(client_id) |
13124 | 420 |
} |
421 |
||
14784
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
422 |
fn handle_response(&mut self, mut response: handlers::Response, poll: &Poll) { |
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
423 |
if response.is_empty() { |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
424 |
return; |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
425 |
} |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
426 |
|
14677
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
427 |
debug!("{} pending server messages", response.len()); |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
428 |
let output = response.extract_messages(&mut self.server); |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
429 |
for (clients, message) in output { |
13424 | 430 |
debug!("Message {:?} to {:?}", message, clients); |
431 |
let msg_string = message.to_raw_protocol(); |
|
432 |
for client_id in clients { |
|
433 |
if let Some(client) = self.clients.get_mut(client_id) { |
|
434 |
client.send_string(&msg_string); |
|
14462 | 435 |
self.pending |
436 |
.insert((client_id, NetworkClientState::NeedsWrite)); |
|
13419 | 437 |
} |
438 |
} |
|
439 |
} |
|
14701 | 440 |
|
441 |
for client_id in response.extract_removed_clients() { |
|
15180 | 442 |
self.deregister_client(poll, client_id, false); |
14701 | 443 |
} |
14784
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
444 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
445 |
#[cfg(feature = "official-server")] |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
446 |
{ |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
447 |
let client_id = response.client_id(); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
448 |
for task in response.extract_io_tasks() { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
449 |
self.io.send(client_id, task); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
450 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
451 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
452 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
453 |
|
14808 | 454 |
pub fn handle_timeout(&mut self, poll: &Poll) -> io::Result<()> { |
455 |
while let Some(TimerData(event, client_id)) = self.timer.poll() { |
|
456 |
match event { |
|
14812 | 457 |
TimeoutEvent::SendPing { probes_count } => { |
14808 | 458 |
if let Some(ref mut client) = self.clients.get_mut(client_id) { |
15080 | 459 |
client.send_string(&HwServerMessage::Ping.to_raw_protocol()); |
14808 | 460 |
client.write()?; |
14812 | 461 |
let timeout = if probes_count != 0 { |
462 |
create_ping_timeout(&mut self.timer, probes_count - 1, client_id) |
|
463 |
} else { |
|
464 |
create_drop_timeout(&mut self.timer, client_id) |
|
465 |
}; |
|
466 |
client.replace_timeout(timeout); |
|
14808 | 467 |
} |
468 |
} |
|
469 |
TimeoutEvent::DropClient => { |
|
15180 | 470 |
if let Some(ref mut client) = self.clients.get_mut(client_id) { |
471 |
client.send_string( |
|
472 |
&HwServerMessage::Bye("Ping timeout".to_string()).to_raw_protocol(), |
|
473 |
); |
|
474 |
client.write(); |
|
475 |
} |
|
14808 | 476 |
self.operation_failed( |
477 |
poll, |
|
478 |
client_id, |
|
479 |
&ErrorKind::TimedOut.into(), |
|
480 |
"No ping response", |
|
481 |
)?; |
|
482 |
} |
|
483 |
} |
|
484 |
} |
|
485 |
Ok(()) |
|
486 |
} |
|
487 |
||
14784
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
488 |
#[cfg(feature = "official-server")] |
15107 | 489 |
pub fn handle_io_result(&mut self, poll: &Poll) -> io::Result<()> { |
490 |
while let Some((client_id, result)) = self.io.try_recv() { |
|
491 |
debug!("Handling io result {:?} for client {}", result, client_id); |
|
14784
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
492 |
let mut response = handlers::Response::new(client_id); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
493 |
handlers::handle_io_result(&mut self.server, client_id, &mut response, result); |
15107 | 494 |
self.handle_response(response, poll); |
14784
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14702
diff
changeset
|
495 |
} |
14921 | 496 |
Ok(()) |
13419 | 497 |
} |
498 |
||
13804 | 499 |
fn create_client_socket(&self, socket: TcpStream) -> io::Result<ClientSocket> { |
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
500 |
Ok(ClientSocket::Plain(socket)) |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
501 |
} |
13804 | 502 |
|
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
503 |
#[cfg(feature = "tls-connections")] |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
504 |
fn create_client_secure_socket(&self, socket: TcpStream) -> io::Result<ClientSocket> { |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
505 |
let ssl = Ssl::new(&self.ssl.context).unwrap(); |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
506 |
let mut builder = SslStreamBuilder::new(ssl, socket); |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
507 |
builder.set_accept_state(); |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
508 |
match builder.handshake() { |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
509 |
Ok(stream) => Ok(ClientSocket::SslStream(stream)), |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
510 |
Err(HandshakeError::WouldBlock(stream)) => Ok(ClientSocket::SslHandshake(Some(stream))), |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
511 |
Err(e) => { |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
512 |
debug!("OpenSSL handshake failed: {}", e); |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
513 |
Err(Error::new(ErrorKind::Other, "Connection failure")) |
13804 | 514 |
} |
515 |
} |
|
516 |
} |
|
517 |
||
14840 | 518 |
fn init_client(&mut self, poll: &Poll, client_id: ClientId) { |
519 |
let mut response = handlers::Response::new(client_id); |
|
14852 | 520 |
|
521 |
if let ClientSocket::Plain(_) = self.clients[client_id].socket { |
|
522 |
#[cfg(feature = "tls-connections")] |
|
523 |
response.add(Redirect(self.ssl.listener.local_addr().unwrap().port()).send_self()) |
|
524 |
} |
|
14840 | 525 |
|
15181 | 526 |
handlers::handle_client_accept( |
527 |
&mut self.server, |
|
528 |
client_id, |
|
529 |
&mut response, |
|
530 |
self.clients[client_id].peer_addr.ip().is_loopback(), |
|
531 |
); |
|
14840 | 532 |
self.handle_response(response, poll); |
533 |
} |
|
534 |
||
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
535 |
pub fn accept_client(&mut self, poll: &Poll, server_token: mio::Token) -> io::Result<()> { |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
536 |
match server_token { |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
537 |
utils::SERVER_TOKEN => { |
14875 | 538 |
let (client_socket, addr) = self.listener.accept()?; |
539 |
info!("Connected(plaintext): {}", addr); |
|
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
540 |
let client_id = |
14921 | 541 |
self.register_client(poll, self.create_client_socket(client_socket)?, addr)?; |
14840 | 542 |
self.init_client(poll, client_id); |
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
543 |
} |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
544 |
#[cfg(feature = "tls-connections")] |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
545 |
utils::SECURE_SERVER_TOKEN => { |
14875 | 546 |
let (client_socket, addr) = self.ssl.listener.accept()?; |
547 |
info!("Connected(TLS): {}", addr); |
|
14921 | 548 |
self.register_client(poll, self.create_client_secure_socket(client_socket)?, addr)?; |
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
549 |
} |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
550 |
_ => unreachable!(), |
14698 | 551 |
} |
13124 | 552 |
|
553 |
Ok(()) |
|
554 |
} |
|
555 |
||
14462 | 556 |
fn operation_failed( |
557 |
&mut self, |
|
558 |
poll: &Poll, |
|
559 |
client_id: ClientId, |
|
560 |
error: &Error, |
|
561 |
msg: &str, |
|
562 |
) -> io::Result<()> { |
|
13419 | 563 |
let addr = if let Some(ref mut client) = self.clients.get_mut(client_id) { |
564 |
client.peer_addr |
|
565 |
} else { |
|
566 |
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0) |
|
567 |
}; |
|
568 |
debug!("{}({}): {}", msg, addr, error); |
|
569 |
self.client_error(poll, client_id) |
|
13124 | 570 |
} |
571 |
||
14462 | 572 |
pub fn client_readable(&mut self, poll: &Poll, client_id: ClientId) -> io::Result<()> { |
573 |
let messages = if let Some(ref mut client) = self.clients.get_mut(client_id) { |
|
14812 | 574 |
let timeout = client.replace_timeout(create_ping_timeout( |
575 |
&mut self.timer, |
|
576 |
PING_PROBES_COUNT - 1, |
|
577 |
client_id, |
|
578 |
)); |
|
14808 | 579 |
self.timer.cancel_timeout(&timeout); |
14462 | 580 |
client.read() |
581 |
} else { |
|
582 |
warn!("invalid readable client: {}", client_id); |
|
583 |
Ok((Vec::new(), NetworkClientState::Idle)) |
|
584 |
}; |
|
13419 | 585 |
|
14676
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
586 |
let mut response = handlers::Response::new(client_id); |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
587 |
|
13419 | 588 |
match messages { |
589 |
Ok((messages, state)) => { |
|
590 |
for message in messages { |
|
14676
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
591 |
debug!("Handling message {:?} for client {}", message, client_id); |
14801 | 592 |
handlers::handle(&mut self.server, client_id, &mut response, message); |
13419 | 593 |
} |
594 |
match state { |
|
13420 | 595 |
NetworkClientState::NeedsRead => { |
596 |
self.pending.insert((client_id, state)); |
|
14462 | 597 |
} |
598 |
NetworkClientState::Closed => self.client_error(&poll, client_id)?, |
|
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
599 |
#[cfg(feature = "tls-connections")] |
14840 | 600 |
NetworkClientState::Connected => self.init_client(poll, client_id), |
13419 | 601 |
_ => {} |
602 |
}; |
|
13124 | 603 |
} |
13419 | 604 |
Err(e) => self.operation_failed( |
14462 | 605 |
poll, |
606 |
client_id, |
|
607 |
&e, |
|
608 |
"Error while reading from client socket", |
|
609 |
)?, |
|
13124 | 610 |
} |
611 |
||
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
612 |
self.handle_response(response, poll); |
13124 | 613 |
|
614 |
Ok(()) |
|
615 |
} |
|
616 |
||
14462 | 617 |
pub fn client_writable(&mut self, poll: &Poll, client_id: ClientId) -> io::Result<()> { |
618 |
let result = if let Some(ref mut client) = self.clients.get_mut(client_id) { |
|
619 |
client.write() |
|
620 |
} else { |
|
621 |
warn!("invalid writable client: {}", client_id); |
|
622 |
Ok(((), NetworkClientState::Idle)) |
|
623 |
}; |
|
13419 | 624 |
|
625 |
match result { |
|
13420 | 626 |
Ok(((), state)) if state == NetworkClientState::NeedsWrite => { |
627 |
self.pending.insert((client_id, state)); |
|
14462 | 628 |
} |
15179
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
629 |
Ok(((), state)) if state == NetworkClientState::Closed => { |
15180 | 630 |
self.deregister_client(poll, client_id, false); |
15179
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
631 |
} |
e705ac360785
pin removed clients until the output buffer is flushed
alfadur
parents:
15178
diff
changeset
|
632 |
Ok(_) => (), |
14462 | 633 |
Err(e) => { |
634 |
self.operation_failed(poll, client_id, &e, "Error while writing to client socket")? |
|
635 |
} |
|
13124 | 636 |
} |
637 |
||
638 |
Ok(()) |
|
639 |
} |
|
640 |
||
14462 | 641 |
pub fn client_error(&mut self, poll: &Poll, client_id: ClientId) -> io::Result<()> { |
15180 | 642 |
let pending_close = self.clients[client_id].pending_close; |
643 |
self.deregister_client(poll, client_id, true); |
|
644 |
||
645 |
if !pending_close { |
|
646 |
let mut response = handlers::Response::new(client_id); |
|
647 |
handlers::handle_client_loss(&mut self.server, client_id, &mut response); |
|
648 |
self.handle_response(response, poll); |
|
649 |
} |
|
13124 | 650 |
|
651 |
Ok(()) |
|
652 |
} |
|
13419 | 653 |
|
654 |
pub fn has_pending_operations(&self) -> bool { |
|
655 |
!self.pending.is_empty() |
|
656 |
} |
|
657 |
||
658 |
pub fn on_idle(&mut self, poll: &Poll) -> io::Result<()> { |
|
13420 | 659 |
if self.has_pending_operations() { |
13483 | 660 |
let mut cache = replace(&mut self.pending_cache, Vec::new()); |
13420 | 661 |
cache.extend(self.pending.drain()); |
662 |
for (id, state) in cache.drain(..) { |
|
663 |
match state { |
|
14462 | 664 |
NetworkClientState::NeedsRead => self.client_readable(poll, id)?, |
665 |
NetworkClientState::NeedsWrite => self.client_writable(poll, id)?, |
|
13420 | 666 |
_ => {} |
667 |
} |
|
13419 | 668 |
} |
13420 | 669 |
swap(&mut cache, &mut self.pending_cache); |
13419 | 670 |
} |
671 |
Ok(()) |
|
672 |
} |
|
13124 | 673 |
} |
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
674 |
|
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
675 |
pub struct NetworkLayerBuilder { |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
676 |
listener: Option<TcpListener>, |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
677 |
secure_listener: Option<TcpListener>, |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
678 |
clients_capacity: usize, |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
679 |
rooms_capacity: usize, |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
680 |
} |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
681 |
|
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
682 |
impl Default for NetworkLayerBuilder { |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
683 |
fn default() -> Self { |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
684 |
Self { |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
685 |
clients_capacity: 1024, |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
686 |
rooms_capacity: 512, |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
687 |
listener: None, |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
688 |
secure_listener: None, |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
689 |
} |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
690 |
} |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
691 |
} |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
692 |
|
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
693 |
impl NetworkLayerBuilder { |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
694 |
pub fn with_listener(self, listener: TcpListener) -> Self { |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
695 |
Self { |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
696 |
listener: Some(listener), |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
697 |
..self |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
698 |
} |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
699 |
} |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
700 |
|
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
701 |
pub fn with_secure_listener(self, listener: TcpListener) -> Self { |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
702 |
Self { |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
703 |
secure_listener: Some(listener), |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
704 |
..self |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
705 |
} |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
706 |
} |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
707 |
|
14840 | 708 |
#[cfg(feature = "tls-connections")] |
709 |
fn create_ssl_context(listener: TcpListener) -> ServerSsl { |
|
710 |
let mut builder = SslContextBuilder::new(SslMethod::tls()).unwrap(); |
|
711 |
builder.set_verify(SslVerifyMode::NONE); |
|
712 |
builder.set_read_ahead(true); |
|
713 |
builder |
|
714 |
.set_certificate_file("ssl/cert.pem", SslFiletype::PEM) |
|
715 |
.expect("Cannot find certificate file"); |
|
716 |
builder |
|
717 |
.set_private_key_file("ssl/key.pem", SslFiletype::PEM) |
|
718 |
.expect("Cannot find private key file"); |
|
719 |
builder.set_options(SslOptions::NO_COMPRESSION); |
|
720 |
builder.set_cipher_list("DEFAULT:!LOW:!RC4:!EXP").unwrap(); |
|
721 |
ServerSsl { |
|
722 |
listener, |
|
723 |
context: builder.build(), |
|
724 |
} |
|
725 |
} |
|
726 |
||
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
727 |
pub fn build(self) -> NetworkLayer { |
15080 | 728 |
let server = HwServer::new(self.clients_capacity, self.rooms_capacity); |
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
729 |
let clients = Slab::with_capacity(self.clients_capacity); |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
730 |
let pending = HashSet::with_capacity(2 * self.clients_capacity); |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
731 |
let pending_cache = Vec::with_capacity(2 * self.clients_capacity); |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
732 |
let timer = timer::Builder::default().build(); |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
733 |
|
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
734 |
NetworkLayer { |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
735 |
listener: self.listener.expect("No listener provided"), |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
736 |
server, |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
737 |
clients, |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
738 |
pending, |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
739 |
pending_cache, |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
740 |
#[cfg(feature = "tls-connections")] |
14840 | 741 |
ssl: Self::create_ssl_context( |
14835
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
742 |
self.secure_listener.expect("No secure listener provided"), |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
743 |
), |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
744 |
#[cfg(feature = "official-server")] |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
745 |
io: IoLayer::new(), |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
746 |
timer, |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
747 |
} |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
748 |
} |
8ddb5842fe0b
allow running plaintext and tls servers in parallel
alfadur
parents:
14812
diff
changeset
|
749 |
} |