diff options
| author | zirkonya <zirkonya@iridium.lan> | 2026-08-25 18:41:28 +0200 |
|---|---|---|
| committer | zirkonya <zirkonya@iridium.lan> | 2026-08-25 18:41:28 +0200 |
| commit | 8bae725a803ee00bf5bc86b9fc0be6853b95115e (patch) | |
| tree | 1a62d1f61191fb3993a1e3d81f34653fe7a10991 /src/event.rs | |
| parent | 7297bbb864778814ed59e99d0d6b05853704f06d (diff) | |
Add getset ; prepare event and backend
Diffstat (limited to 'src/event.rs')
| -rw-r--r-- | src/event.rs | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/src/event.rs b/src/event.rs new file mode 100644 index 0000000..bbba586 --- /dev/null +++ b/src/event.rs @@ -0,0 +1,94 @@ +use std::{io, net::SocketAddr, time::Instant}; + +use getset::{Getters, Setters}; + +pub mod handler; +pub mod listener; + +pub enum EventKind<Packet, Uid = u64> +where + Uid: PartialEq, +{ + Connection(ConnectionEvent<Uid>), + PacketReceived(PacketReceivedEvent<Packet>), + PacketSent(PacketSentEvent<Packet>), + Disconnect(DisconnectEvent<Uid>), +} + +// TODO : identification +// TODO : reason type +#[derive(Getters)] +pub struct ConnectionEvent<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, +} + +#[derive(Getters)] +pub struct DisconnectEvent<Uid> +where + Uid: PartialEq, +{ + #[getset(get = "pub")] + connection_id: Uid, + #[getset(get = "pub")] + reason: DisconnectReason, +} + +#[derive(Getters, Setters)] +pub struct Event<Packet, Uid = u64> +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 { + Self { + instant: Instant::now(), + canceled: false, + kind, + } + } + + pub fn differed(when: Instant, kind: EventKind<Packet>) -> Self { + Self { + instant: when, + canceled: false, + kind, + } + } +} |
