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