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