summaryrefslogtreecommitdiff
path: root/src/event.rs
blob: bbba5866c494b62162b84a48e4238ca188d940ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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,
        }
    }
}