rtc/peer_connection/event/data_channel_event.rs
1//! Data channel event types.
2//!
3//! Events related to RTCDataChannel lifecycle and buffering state changes.
4
5use crate::data_channel::RTCDataChannelId;
6
7/// Events that can be emitted by an RTCDataChannel.
8///
9/// These events track the lifecycle and buffer state of data channels.
10/// Applications should poll these events from `RTCPeerConnectionEvent::OnDataChannel`
11/// to manage data channel connections and implement flow control.
12///
13/// # Lifecycle Events
14///
15/// Data channels go through the following lifecycle:
16/// 1. `OnOpen` - Channel is ready for data transmission
17/// 2. `OnClosing` - Channel is starting to close
18/// 3. `OnClose` - Channel is fully closed
19/// 4. `OnError` - An error occurred
20///
21/// # Buffer Events
22///
23/// - `OnBufferedAmountLow` - Buffer dropped below low-water mark (safe to send more)
24/// - `OnBufferedAmountHigh` - Buffer exceeded high-water mark (should pause sending)
25///
26/// # Examples
27///
28/// ## Handling data channel events in event loop
29///
30/// ```
31/// use rtc::peer_connection::event::{RTCPeerConnectionEvent, RTCDataChannelEvent};
32///
33/// # fn example(event: RTCPeerConnectionEvent) {
34/// match event {
35/// RTCPeerConnectionEvent::OnDataChannel(dc_event) => {
36/// match dc_event {
37/// RTCDataChannelEvent::OnOpen(channel_id) => {
38/// println!("Data channel opened: {:?}", channel_id);
39/// }
40/// RTCDataChannelEvent::OnClose(channel_id) => {
41/// println!("Data channel closed: {:?}", channel_id);
42/// }
43/// RTCDataChannelEvent::OnError(channel_id) => {
44/// eprintln!("Data channel error: {:?}", channel_id);
45/// }
46/// _ => {}
47/// }
48/// }
49/// _ => {}
50/// }
51/// # }
52/// ```
53///
54/// ## Implementing flow control with buffer events
55///
56/// ```
57/// use rtc::peer_connection::event::{RTCPeerConnectionEvent, RTCDataChannelEvent};
58///
59/// # fn example(event: RTCPeerConnectionEvent, paused: &mut bool) {
60/// match event {
61/// RTCPeerConnectionEvent::OnDataChannel(dc_event) => {
62/// match dc_event {
63/// RTCDataChannelEvent::OnBufferedAmountLow(channel_id) => {
64/// println!("Buffer low - resume sending");
65/// *paused = false;
66/// }
67/// RTCDataChannelEvent::OnBufferedAmountHigh(channel_id) => {
68/// println!("Buffer high - pause sending");
69/// *paused = true;
70/// }
71/// _ => {}
72/// }
73/// }
74/// _ => {}
75/// }
76/// # }
77/// ```
78///
79/// # See Also
80///
81/// - [W3C RTCDataChannel](https://www.w3.org/TR/webrtc/#rtcdatachannel)
82#[allow(clippy::enum_variant_names)]
83#[derive(Debug, Clone)]
84pub enum RTCDataChannelEvent {
85 /// Data channel has opened and is ready to send/receive data.
86 ///
87 /// This event is fired when the data channel transitions to the "open" state.
88 /// Data can now be sent through the channel.
89 OnOpen(RTCDataChannelId),
90
91 /// An error occurred on the data channel.
92 ///
93 /// This event is fired when an error is encountered. The channel may still
94 /// be usable depending on the error type.
95 OnError(RTCDataChannelId),
96
97 /// Data channel is closing.
98 ///
99 /// This event is fired when the channel begins the closing process.
100 /// The channel is transitioning to the "closing" state.
101 OnClosing(RTCDataChannelId),
102
103 /// Data channel has closed.
104 ///
105 /// This event is fired when the channel is fully closed and no longer usable.
106 /// No more data can be sent or received.
107 OnClose(RTCDataChannelId),
108
109 /// Buffered amount dropped below the low-water mark.
110 ///
111 /// This event is fired when the amount of buffered outgoing data drops
112 /// below the threshold set by `set_buffered_amount_low_threshold()`.
113 /// This indicates it's safe to send more data without causing excessive buffering.
114 ///
115 /// Use this event to implement flow control and prevent memory exhaustion.
116 OnBufferedAmountLow(RTCDataChannelId),
117
118 /// Buffered amount exceeded the high-water mark (implementation-specific).
119 ///
120 /// This is a non-standard event that can be used to detect when too much
121 /// data is being buffered. Applications should pause sending when this fires.
122 OnBufferedAmountHigh(RTCDataChannelId),
123}
124
125impl Default for RTCDataChannelEvent {
126 fn default() -> Self {
127 Self::OnOpen(Default::default())
128 }
129}