disconnect plumbing
This commit is contained in:
parent
94095c21e1
commit
a7cb592471
src/common
@ -6,7 +6,7 @@ use std::net::{SocketAddr, Ipv4Addr};
|
|||||||
use std::sync::mpsc::TryRecvError;
|
use std::sync::mpsc::TryRecvError;
|
||||||
use mio::tcp::TcpListener;
|
use mio::tcp::TcpListener;
|
||||||
use mio::{Events, Poll, Token, Ready, PollOpt};
|
use mio::{Events, Poll, Token, Ready, PollOpt};
|
||||||
use log::warn;
|
use log::{info, warn};
|
||||||
|
|
||||||
use crate::common::client::Client;
|
use crate::common::client::Client;
|
||||||
use crate::common::serverstate::{SendServerPacket, RecvServerPacket, ClientId};
|
use crate::common::serverstate::{SendServerPacket, RecvServerPacket, ClientId};
|
||||||
@ -48,6 +48,7 @@ pub enum ClientAction<S> {
|
|||||||
pub enum ClientPoolAction<R> {
|
pub enum ClientPoolAction<R> {
|
||||||
NewClient(ClientId),
|
NewClient(ClientId),
|
||||||
Packet(ClientId, R),
|
Packet(ClientId, R),
|
||||||
|
Disconnect(ClientId),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -127,7 +128,6 @@ impl<S, R> ClientPool<S, R> where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,7 +146,6 @@ impl<S, R> ClientPool<S, R> where
|
|||||||
Token(0) => self.new_client(),
|
Token(0) => self.new_client(),
|
||||||
Token(1) => self.packet_to_send(),
|
Token(1) => self.packet_to_send(),
|
||||||
_ => {
|
_ => {
|
||||||
|
|
||||||
let client_id = match self.client_ids.get(&event.token()) {
|
let client_id = match self.client_ids.get(&event.token()) {
|
||||||
Some(client_id) => client_id,
|
Some(client_id) => client_id,
|
||||||
None => continue,
|
None => continue,
|
||||||
@ -164,12 +163,15 @@ impl<S, R> ClientPool<S, R> where
|
|||||||
match client_read(&self.sender, client) {
|
match client_read(&self.sender, client) {
|
||||||
Ok(()) =>{},
|
Ok(()) =>{},
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
warn!("pkt err: {:?}", err);
|
|
||||||
match err {
|
match err {
|
||||||
PacketNetworkError::ClientDisconnected => {
|
PacketNetworkError::ClientDisconnected => {
|
||||||
|
info!("client {:?} disconnected", client_id);
|
||||||
self.poll.deregister(&client.socket).unwrap();
|
self.poll.deregister(&client.socket).unwrap();
|
||||||
|
self.sender.send(ClientPoolAction::Disconnect(*client_id)).unwrap();
|
||||||
|
},
|
||||||
|
_ => {
|
||||||
|
warn!("pkt err: {:?}", err);
|
||||||
},
|
},
|
||||||
_ => {},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,8 +19,8 @@ fn recv_from_clientpool<STATE, S, R, E>(state: &mut STATE,
|
|||||||
Ok(incoming) => {
|
Ok(incoming) => {
|
||||||
match incoming {
|
match incoming {
|
||||||
ClientPoolAction::NewClient(client_id) => {
|
ClientPoolAction::NewClient(client_id) => {
|
||||||
for s in state.on_connect(client_id).into_iter() {
|
for action in state.on_connect(client_id).into_iter() {
|
||||||
match s {
|
match action {
|
||||||
OnConnect::Cipher((in_cipher, out_cipher)) => {
|
OnConnect::Cipher((in_cipher, out_cipher)) => {
|
||||||
pool_send.send(ClientAction::EncryptionKeys(client_id, in_cipher, out_cipher)).unwrap();
|
pool_send.send(ClientAction::EncryptionKeys(client_id, in_cipher, out_cipher)).unwrap();
|
||||||
}
|
}
|
||||||
@ -34,8 +34,8 @@ fn recv_from_clientpool<STATE, S, R, E>(state: &mut STATE,
|
|||||||
let to_send = state.handle(client_id, &pkt);
|
let to_send = state.handle(client_id, &pkt);
|
||||||
match to_send {
|
match to_send {
|
||||||
Ok(pkts) => {
|
Ok(pkts) => {
|
||||||
for p in pkts {
|
for pkt in pkts {
|
||||||
pool_send.send(ClientAction::Packet(p.0, p.1)).unwrap();
|
pool_send.send(ClientAction::Packet(pkt.0, pkt.1)).unwrap();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
@ -43,6 +43,11 @@ fn recv_from_clientpool<STATE, S, R, E>(state: &mut STATE,
|
|||||||
warn!("[handler error]: {:?} {:?}", client_id, err);
|
warn!("[handler error]: {:?} {:?}", client_id, err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
ClientPoolAction::Disconnect(client_id) => {
|
||||||
|
for pkt in state.on_disconnect(client_id) {
|
||||||
|
pool_send.send(ClientAction::Packet(pkt.0, pkt.1)).unwrap();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user