diff options
Diffstat (limited to 'src/event.rs')
| -rw-r--r-- | src/event.rs | 88 |
1 files changed, 33 insertions, 55 deletions
diff --git a/src/event.rs b/src/event.rs index bbba586..95d5a12 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1,94 +1,72 @@ -use std::{io, net::SocketAddr, time::Instant}; +use std::{cell::Cell, time::Instant}; -use getset::{Getters, Setters}; +use getset::Getters; + +use crate::event::{ + connection::ConnectionEvent, disconnect::DisconnectEvent, received::PacketReceivedEvent, + sent::PacketSentEvent, +}; pub mod handler; pub mod listener; -pub enum EventKind<Packet, Uid = u64> +pub mod connection; +pub mod disconnect; +pub mod received; +pub mod sent; + +pub enum EventKind<Packet, Uid> where Uid: PartialEq, { - Connection(ConnectionEvent<Uid>), + Connection(ConnectionEvent), PacketReceived(PacketReceivedEvent<Packet>), PacketSent(PacketSentEvent<Packet>), Disconnect(DisconnectEvent<Uid>), } -// TODO : identification -// TODO : reason type #[derive(Getters)] -pub struct ConnectionEvent<Uid> +pub struct Event<Packet, Uid> where Uid: PartialEq, { #[getset(get = "pub")] - peer_addr: SocketAddr, - #[getset(get = "pub")] - local_addr: SocketAddr, - #[getset(get = "pub")] - connection_id: Uid, -} -#[derive(Getters)] -pub struct PacketReceivedEvent<Packet> { - #[getset(get = "pub")] - packet: Packet, -} -#[derive(Getters)] -pub struct PacketSentEvent<Packet> { - #[getset(get = "pub")] - packet: Packet, -} - -// TODO : better error type for protocol violation -pub type ProtocolViolation = String; - -pub enum DisconnectReason { - Normal, - Error(io::Error), - ProtocolError(ProtocolViolation), - Timeout, - ServerShutdown, -} + instant: Instant, -#[derive(Getters)] -pub struct DisconnectEvent<Uid> -where - Uid: PartialEq, -{ #[getset(get = "pub")] connection_id: Uid, + canceled: Cell<bool>, #[getset(get = "pub")] - reason: DisconnectReason, + kind: EventKind<Packet, Uid>, } -#[derive(Getters, Setters)] -pub struct Event<Packet, Uid = u64> +impl<Packet, Uid> Event<Packet, Uid> where Uid: PartialEq, { - #[getset(get = "pub")] - instant: Instant, - #[getset(get = "pub", set = "pub")] - canceled: bool, - #[getset(get = "pub")] - kind: EventKind<Packet, Uid>, -} - -impl<Packet> Event<Packet> { - pub fn new(kind: EventKind<Packet>) -> Self { + pub fn new(connection_id: Uid, kind: EventKind<Packet, Uid>) -> Self { Self { + connection_id, instant: Instant::now(), - canceled: false, + canceled: Cell::new(true), kind, } } - pub fn differed(when: Instant, kind: EventKind<Packet>) -> Self { + pub fn differed(connection_id: Uid, when: Instant, kind: EventKind<Packet, Uid>) -> Self { Self { + connection_id, instant: when, - canceled: false, + canceled: Cell::new(true), kind, } } + + pub fn canceled(&self) -> bool { + self.canceled.get() + } + + pub fn cancel(&self) { + self.canceled.set(true); + } } |
