bitfold_protocol/
channel.rs

1use super::packet::{DeliveryGuarantee, OrderingGuarantee};
2
3/// Represents a communication channel with independent ordering/sequencing.
4/// Each peer can have multiple channels for different types of traffic.
5#[derive(Debug)]
6pub struct Channel {
7    /// Channel identifier
8    id: u8,
9    /// Delivery guarantee for this channel
10    delivery: DeliveryGuarantee,
11    /// Ordering guarantee for this channel
12    ordering: OrderingGuarantee,
13}
14
15impl Channel {
16    /// Creates a new channel with specified guarantees.
17    pub fn new(id: u8, delivery: DeliveryGuarantee, ordering: OrderingGuarantee) -> Self {
18        Self { id, delivery, ordering }
19    }
20
21    /// Creates an unreliable, unordered channel (like UDP).
22    pub fn unreliable(id: u8) -> Self {
23        Self::new(id, DeliveryGuarantee::Unreliable, OrderingGuarantee::None)
24    }
25
26    /// Creates an unreliable, sequenced channel (drops old packets).
27    pub fn unreliable_sequenced(id: u8) -> Self {
28        Self::new(id, DeliveryGuarantee::Unreliable, OrderingGuarantee::Sequenced(Some(id)))
29    }
30
31    /// Creates a reliable, unordered channel.
32    pub fn reliable_unordered(id: u8) -> Self {
33        Self::new(id, DeliveryGuarantee::Reliable, OrderingGuarantee::None)
34    }
35
36    /// Creates a reliable, ordered channel (like TCP).
37    pub fn reliable_ordered(id: u8) -> Self {
38        Self::new(id, DeliveryGuarantee::Reliable, OrderingGuarantee::Ordered(Some(id)))
39    }
40
41    /// Returns the channel ID.
42    pub fn id(&self) -> u8 {
43        self.id
44    }
45
46    /// Returns the delivery guarantee.
47    pub fn delivery(&self) -> DeliveryGuarantee {
48        self.delivery
49    }
50
51    /// Returns the ordering guarantee.
52    pub fn ordering(&self) -> OrderingGuarantee {
53        self.ordering
54    }
55}
56
57/// Manages multiple channels for a connection.
58/// Supports up to 255 channels per peer.
59#[derive(Debug)]
60pub struct ChannelManager {
61    /// Array of channels (indexed by channel ID)
62    channels: Vec<Channel>,
63}
64
65impl ChannelManager {
66    /// Creates a new channel manager with the specified number of channels.
67    pub fn new(channel_count: u8) -> Self {
68        let mut channels = Vec::with_capacity(channel_count as usize);
69
70        // Default: all channels are unreliable unordered
71        for id in 0..channel_count {
72            channels.push(Channel::unreliable(id));
73        }
74
75        Self { channels }
76    }
77
78    /// Creates a channel manager with default configuration (1 reliable ordered channel).
79    pub fn default_channels() -> Self {
80        let mut manager = Self::new(1);
81        manager.channels[0] = Channel::reliable_ordered(0);
82        manager
83    }
84
85    /// Sets the configuration for a specific channel.
86    pub fn configure_channel(
87        &mut self,
88        id: u8,
89        delivery: DeliveryGuarantee,
90        ordering: OrderingGuarantee,
91    ) {
92        if let Some(channel) = self.channels.get_mut(id as usize) {
93            channel.delivery = delivery;
94            channel.ordering = ordering;
95        }
96    }
97
98    /// Gets a reference to a channel by ID.
99    pub fn get_channel(&self, id: u8) -> Option<&Channel> {
100        self.channels.get(id as usize)
101    }
102
103    /// Returns the total number of channels.
104    pub fn channel_count(&self) -> u8 {
105        self.channels.len() as u8
106    }
107
108    /// Returns an iterator over all channels.
109    pub fn channels(&self) -> impl Iterator<Item = &Channel> {
110        self.channels.iter()
111    }
112}
113
114#[cfg(test)]
115mod tests {
116    use super::*;
117
118    #[test]
119    fn test_channel_creation() {
120        let ch = Channel::unreliable(0);
121        assert_eq!(ch.id(), 0);
122        assert_eq!(ch.delivery(), DeliveryGuarantee::Unreliable);
123        assert_eq!(ch.ordering(), OrderingGuarantee::None);
124
125        let ch = Channel::reliable_ordered(1);
126        assert_eq!(ch.id(), 1);
127        assert_eq!(ch.delivery(), DeliveryGuarantee::Reliable);
128        match ch.ordering() {
129            OrderingGuarantee::Ordered(_) => (),
130            _ => panic!("Expected ordered"),
131        }
132    }
133
134    #[test]
135    fn test_channel_manager() {
136        let manager = ChannelManager::new(4);
137        assert_eq!(manager.channel_count(), 4);
138
139        let ch0 = manager.get_channel(0).unwrap();
140        assert_eq!(ch0.id(), 0);
141
142        let ch3 = manager.get_channel(3).unwrap();
143        assert_eq!(ch3.id(), 3);
144
145        assert!(manager.get_channel(4).is_none());
146    }
147
148    #[test]
149    fn test_channel_presets() {
150        let unreliable = Channel::unreliable(0);
151        assert_eq!(unreliable.delivery(), DeliveryGuarantee::Unreliable);
152        assert_eq!(unreliable.ordering(), OrderingGuarantee::None);
153
154        let sequenced = Channel::unreliable_sequenced(1);
155        assert_eq!(sequenced.delivery(), DeliveryGuarantee::Unreliable);
156        match sequenced.ordering() {
157            OrderingGuarantee::Sequenced(_) => (),
158            _ => panic!("Expected sequenced"),
159        }
160
161        let reliable = Channel::reliable_unordered(2);
162        assert_eq!(reliable.delivery(), DeliveryGuarantee::Reliable);
163        assert_eq!(reliable.ordering(), OrderingGuarantee::None);
164
165        let ordered = Channel::reliable_ordered(3);
166        assert_eq!(ordered.delivery(), DeliveryGuarantee::Reliable);
167        match ordered.ordering() {
168            OrderingGuarantee::Ordered(_) => (),
169            _ => panic!("Expected ordered"),
170        }
171    }
172
173    #[test]
174    fn test_default_channels() {
175        let manager = ChannelManager::default_channels();
176        assert_eq!(manager.channel_count(), 1);
177
178        let ch = manager.get_channel(0).unwrap();
179        assert_eq!(ch.delivery(), DeliveryGuarantee::Reliable);
180        match ch.ordering() {
181            OrderingGuarantee::Ordered(_) => (),
182            _ => panic!("Expected ordered"),
183        }
184    }
185}