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