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