veloren_network_protocol/
event.rs

1use crate::{
2    frame::OTFrame,
3    types::{Bandwidth, Prio, Promises, Sid},
4};
5use bytes::Bytes;
6
7/// used for communication with [`SendProtocol`] and [`RecvProtocol`]
8///
9/// [`SendProtocol`]: crate::SendProtocol
10/// [`RecvProtocol`]: crate::RecvProtocol
11#[derive(Debug, Clone)]
12#[cfg_attr(test, derive(PartialEq, Eq))]
13pub enum ProtocolEvent {
14    Shutdown,
15    OpenStream {
16        sid: Sid,
17        prio: Prio,
18        promises: Promises,
19        guaranteed_bandwidth: Bandwidth,
20    },
21    CloseStream {
22        sid: Sid,
23    },
24    Message {
25        data: Bytes,
26        sid: Sid,
27    },
28}
29
30impl ProtocolEvent {
31    pub(crate) fn to_frame(&self) -> OTFrame {
32        match self {
33            ProtocolEvent::Shutdown => OTFrame::Shutdown,
34            ProtocolEvent::OpenStream {
35                sid,
36                prio,
37                promises,
38                guaranteed_bandwidth,
39            } => OTFrame::OpenStream {
40                sid: *sid,
41                prio: *prio,
42                promises: *promises,
43                guaranteed_bandwidth: *guaranteed_bandwidth,
44            },
45            ProtocolEvent::CloseStream { sid } => OTFrame::CloseStream { sid: *sid },
46            ProtocolEvent::Message { .. } => {
47                unimplemented!("Event::Message to OTFrame IS NOT supported")
48            },
49        }
50    }
51}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_to_frame() {
59        assert_eq!(ProtocolEvent::Shutdown.to_frame(), OTFrame::Shutdown);
60        assert_eq!(
61            ProtocolEvent::CloseStream { sid: Sid::new(42) }.to_frame(),
62            OTFrame::CloseStream { sid: Sid::new(42) }
63        );
64    }
65
66    #[test]
67    #[should_panic]
68    fn test_msg_buffer_panic() {
69        let _ = ProtocolEvent::Message {
70            data: Bytes::new(),
71            sid: Sid::new(23),
72        }
73        .to_frame();
74    }
75}