ecksport_net/
shared_state.rs

1//! State shared between the connection handle, channel handles, and the
2//! connection worker.
3
4use std::collections::*;
5use std::sync::atomic::{AtomicBool, Ordering};
6use std::sync::Arc;
7
8use ecksport_core::peer::PeerData;
9use ecksport_core::topic;
10use tokio::sync::RwLock;
11
12use crate::channel_state::Creator;
13
14pub struct ConnSharedState {
15    pub(crate) protocol: topic::Topic,
16    pub(crate) peer_data: PeerData,
17    pub(crate) initiator: Creator,
18    pub(crate) force_closed: AtomicBool,
19    pub(crate) chan_shared: RwLock<HashMap<u32, Arc<ChanSharedState>>>,
20}
21
22impl ConnSharedState {
23    pub(crate) fn new(protocol: topic::Topic, peer_data: PeerData, initiator: Creator) -> Self {
24        Self {
25            protocol,
26            peer_data,
27            initiator,
28            force_closed: AtomicBool::new(false),
29            chan_shared: RwLock::new(HashMap::new()),
30        }
31    }
32
33    pub fn protocol(&self) -> topic::Topic {
34        self.protocol
35    }
36
37    pub fn peer_data(&self) -> &PeerData {
38        &self.peer_data
39    }
40
41    /// The connection initiator.
42    pub fn initiator(&self) -> Creator {
43        self.initiator
44    }
45
46    /// Called when the owning `WorkerHandle` is dropped so that we'll eventually clean up.
47    pub(crate) fn set_dropped(&self) {
48        self.force_closed.store(true, Ordering::Relaxed);
49    }
50
51    pub fn is_force_closed(&self) -> bool {
52        self.force_closed.load(Ordering::Relaxed)
53    }
54}
55
56pub struct ChanSharedState {
57    protocol: topic::Topic,
58    topic: topic::Topic,
59    dropped: AtomicBool,
60}
61
62impl ChanSharedState {
63    pub fn new(protocol: topic::Topic, topic: topic::Topic) -> Self {
64        Self {
65            protocol,
66            topic,
67            dropped: AtomicBool::new(false),
68        }
69    }
70
71    pub fn protocol(&self) -> topic::Topic {
72        self.protocol
73    }
74
75    pub fn topic(&self) -> topic::Topic {
76        self.topic
77    }
78
79    /// Marks the channel entry as dropped so that we know to clean it up later.
80    pub fn set_dropped(&self) {
81        self.dropped.store(true, Ordering::Relaxed);
82    }
83}