ecksport_net/
event.rs

1use tokio::sync::oneshot;
2
3use ecksport_core::{frame::MsgFlags, topic};
4
5use crate::channel;
6
7/// Flags extracted from an inbound message.
8#[derive(Copy, Clone, Debug, Eq, PartialEq)]
9pub struct PushFlags {
10    /// If this data is an unusual error condition.
11    pub err: bool,
12}
13
14impl From<&MsgFlags> for PushFlags {
15    fn from(value: &MsgFlags) -> Self {
16        Self { err: value.err }
17    }
18}
19
20/// Describes an event about channels we've received from the remote.  A single
21/// frame received may produce more than one event.
22#[derive(Clone, Debug)]
23pub enum InbEvent {
24    /// Remote created a new channel with a given topic and the opening payload.
25    NewChannel(u32, topic::Topic, PushFlags, Vec<u8>),
26
27    /// Push data on a channel from remote.
28    PushChannel(u32, PushFlags, Vec<u8>),
29
30    /// Close remote end of a channel without sending any data.  Includes if the
31    /// channel is kept alive *as of the instant of processing the frame* by us
32    /// keeping our local end open.
33    CloseChannel(u32, bool),
34
35    /// Notification on a topic from the remote.
36    Notification(topic::Topic, Vec<u8>),
37}
38
39impl InbEvent {
40    /// Returns the relating channel ID, if it's a channel event.
41    pub fn chan_id(&self) -> Option<u32> {
42        match self {
43            Self::NewChannel(id, _, _, _) => Some(*id),
44            Self::PushChannel(id, _, _) => Some(*id),
45            Self::CloseChannel(id, _) => Some(*id),
46            _ => None,
47        }
48    }
49
50    /// Returns the relating topic, if present.
51    pub fn topic(&self) -> Option<topic::Topic> {
52        match self {
53            Self::NewChannel(_, t, _, _) => Some(*t),
54            Self::Notification(t, _) => Some(*t),
55            _ => None,
56        }
57    }
58}
59
60/// Command sent to the connection worker by a channel handle or a handle to the worker.
61pub enum WorkerCommand {
62    OpenChannel(OpenChanCmd),
63    SendMsg(channel::OutbMsg),
64    CloseChannel(u32),
65    SendNotification(topic::Topic, Vec<u8>),
66}
67
68pub struct OpenChanCmd {
69    pub(super) topic: topic::Topic,
70    pub(super) init_msg: Vec<u8>,
71    pub(super) flags: MsgFlags,
72    pub(super) chh_tx: oneshot::Sender<channel::ChannelHandle>,
73}