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