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