equal
deleted
inserted
replaced
16 use mio_extras::timer; |
16 use mio_extras::timer; |
17 use netbuf; |
17 use netbuf; |
18 use slab::Slab; |
18 use slab::Slab; |
19 |
19 |
20 use crate::{ |
20 use crate::{ |
21 core::{ |
21 core::{server::HwServer, types::ClientId}, |
22 server::HWServer, |
|
23 types::ClientId |
|
24 }, |
|
25 handlers, |
22 handlers, |
26 protocol::{messages::*, ProtocolDecoder}, |
23 protocol::{messages::*, ProtocolDecoder}, |
27 utils, |
24 utils, |
28 }; |
25 }; |
29 |
26 |
30 #[cfg(feature = "official-server")] |
27 #[cfg(feature = "official-server")] |
31 use super::io::{IOThread, RequestId}; |
28 use super::io::{IOThread, RequestId}; |
32 |
29 |
33 use crate::{ |
30 use crate::{ |
34 protocol::messages::HWServerMessage::Redirect, |
31 handlers::{IoResult, IoTask}, |
35 handlers::{IoResult, IoTask} |
32 protocol::messages::HwServerMessage::Redirect, |
36 }; |
33 }; |
37 |
34 |
38 #[cfg(feature = "tls-connections")] |
35 #[cfg(feature = "tls-connections")] |
39 use openssl::{ |
36 use openssl::{ |
40 error::ErrorStack, |
37 error::ErrorStack, |
140 fn read_impl<R: Read>( |
137 fn read_impl<R: Read>( |
141 decoder: &mut ProtocolDecoder, |
138 decoder: &mut ProtocolDecoder, |
142 source: &mut R, |
139 source: &mut R, |
143 id: ClientId, |
140 id: ClientId, |
144 addr: &SocketAddr, |
141 addr: &SocketAddr, |
145 ) -> NetworkResult<Vec<HWProtocolMessage>> { |
142 ) -> NetworkResult<Vec<HwProtocolMessage>> { |
146 let mut bytes_read = 0; |
143 let mut bytes_read = 0; |
147 let result = loop { |
144 let result = loop { |
148 match decoder.read_from(source) { |
145 match decoder.read_from(source) { |
149 Ok(bytes) => { |
146 Ok(bytes) => { |
150 debug!("Client {}: read {} bytes", id, bytes); |
147 debug!("Client {}: read {} bytes", id, bytes); |
173 } |
170 } |
174 }; |
171 }; |
175 result |
172 result |
176 } |
173 } |
177 |
174 |
178 pub fn read(&mut self) -> NetworkResult<Vec<HWProtocolMessage>> { |
175 pub fn read(&mut self) -> NetworkResult<Vec<HwProtocolMessage>> { |
179 match self.socket { |
176 match self.socket { |
180 ClientSocket::Plain(ref mut stream) => { |
177 ClientSocket::Plain(ref mut stream) => { |
181 NetworkClient::read_impl(&mut self.decoder, stream, self.id, &self.peer_addr) |
178 NetworkClient::read_impl(&mut self.decoder, stream, self.id, &self.peer_addr) |
182 } |
179 } |
183 #[cfg(feature = "tls-connections")] |
180 #[cfg(feature = "tls-connections")] |
307 |
304 |
308 struct TimerData(TimeoutEvent, ClientId); |
305 struct TimerData(TimeoutEvent, ClientId); |
309 |
306 |
310 pub struct NetworkLayer { |
307 pub struct NetworkLayer { |
311 listener: TcpListener, |
308 listener: TcpListener, |
312 server: HWServer, |
309 server: HwServer, |
313 clients: Slab<NetworkClient>, |
310 clients: Slab<NetworkClient>, |
314 pending: HashSet<(ClientId, NetworkClientState)>, |
311 pending: HashSet<(ClientId, NetworkClientState)>, |
315 pending_cache: Vec<(ClientId, NetworkClientState)>, |
312 pending_cache: Vec<(ClientId, NetworkClientState)>, |
316 #[cfg(feature = "tls-connections")] |
313 #[cfg(feature = "tls-connections")] |
317 ssl: ServerSsl, |
314 ssl: ServerSsl, |
433 pub fn handle_timeout(&mut self, poll: &Poll) -> io::Result<()> { |
430 pub fn handle_timeout(&mut self, poll: &Poll) -> io::Result<()> { |
434 while let Some(TimerData(event, client_id)) = self.timer.poll() { |
431 while let Some(TimerData(event, client_id)) = self.timer.poll() { |
435 match event { |
432 match event { |
436 TimeoutEvent::SendPing { probes_count } => { |
433 TimeoutEvent::SendPing { probes_count } => { |
437 if let Some(ref mut client) = self.clients.get_mut(client_id) { |
434 if let Some(ref mut client) = self.clients.get_mut(client_id) { |
438 client.send_string(&HWServerMessage::Ping.to_raw_protocol()); |
435 client.send_string(&HwServerMessage::Ping.to_raw_protocol()); |
439 client.write()?; |
436 client.write()?; |
440 let timeout = if probes_count != 0 { |
437 let timeout = if probes_count != 0 { |
441 create_ping_timeout(&mut self.timer, probes_count - 1, client_id) |
438 create_ping_timeout(&mut self.timer, probes_count - 1, client_id) |
442 } else { |
439 } else { |
443 create_drop_timeout(&mut self.timer, client_id) |
440 create_drop_timeout(&mut self.timer, client_id) |
682 context: builder.build(), |
679 context: builder.build(), |
683 } |
680 } |
684 } |
681 } |
685 |
682 |
686 pub fn build(self) -> NetworkLayer { |
683 pub fn build(self) -> NetworkLayer { |
687 let server = HWServer::new(self.clients_capacity, self.rooms_capacity); |
684 let server = HwServer::new(self.clients_capacity, self.rooms_capacity); |
688 let clients = Slab::with_capacity(self.clients_capacity); |
685 let clients = Slab::with_capacity(self.clients_capacity); |
689 let pending = HashSet::with_capacity(2 * self.clients_capacity); |
686 let pending = HashSet::with_capacity(2 * self.clients_capacity); |
690 let pending_cache = Vec::with_capacity(2 * self.clients_capacity); |
687 let pending_cache = Vec::with_capacity(2 * self.clients_capacity); |
691 let timer = timer::Builder::default().build(); |
688 let timer = timer::Builder::default().build(); |
692 |
689 |