summaryrefslogtreecommitdiff
path: root/src/event.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/event.rs')
-rw-r--r--src/event.rs94
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,
+ }
+ }
+}