From bb41e396aca01fee9f81785681fac0a79d0fd9d8 Mon Sep 17 00:00:00 2001 From: zirkonya Date: Thu, 27 Aug 2026 09:45:12 +0200 Subject: split event and enhance context --- src/event.rs | 88 +++++++++++++++++++++++------------------------------------- 1 file changed, 33 insertions(+), 55 deletions(-) (limited to 'src/event.rs') 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 +pub mod connection; +pub mod disconnect; +pub mod received; +pub mod sent; + +pub enum EventKind where Uid: PartialEq, { - Connection(ConnectionEvent), + Connection(ConnectionEvent), PacketReceived(PacketReceivedEvent), PacketSent(PacketSentEvent), Disconnect(DisconnectEvent), } -// TODO : identification -// TODO : reason type #[derive(Getters)] -pub struct ConnectionEvent +pub struct Event 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 { - #[getset(get = "pub")] - packet: Packet, -} -#[derive(Getters)] -pub struct PacketSentEvent { - #[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 -where - Uid: PartialEq, -{ #[getset(get = "pub")] connection_id: Uid, + canceled: Cell, #[getset(get = "pub")] - reason: DisconnectReason, + kind: EventKind, } -#[derive(Getters, Setters)] -pub struct Event +impl Event where Uid: PartialEq, { - #[getset(get = "pub")] - instant: Instant, - #[getset(get = "pub", set = "pub")] - canceled: bool, - #[getset(get = "pub")] - kind: EventKind, -} - -impl Event { - pub fn new(kind: EventKind) -> Self { + pub fn new(connection_id: Uid, kind: EventKind) -> Self { Self { + connection_id, instant: Instant::now(), - canceled: false, + canceled: Cell::new(true), kind, } } - pub fn differed(when: Instant, kind: EventKind) -> Self { + pub fn differed(connection_id: Uid, when: Instant, kind: EventKind) -> 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); + } } -- cgit v1.2.3