rtc_datachannel/data_channel/
mod.rs1#[cfg(test)]
14mod data_channel_test;
15
16use crate::message::{
17 message_channel_ack::*, message_channel_close::*, message_channel_open::*,
18 message_channel_threshold::*, *,
19};
20use bytes::{Buf, BytesMut};
21use log::debug;
22use sctp::{PayloadProtocolIdentifier, ReliabilityType};
23use shared::error::{Error, Result};
24use shared::marshal::*;
25use std::collections::VecDeque;
26
27const RECEIVE_MTU: usize = 8192;
28
29#[derive(Eq, PartialEq, Default, Clone, Debug)]
31pub struct DataChannelConfig {
32 pub channel_type: ChannelType,
34 pub negotiated: bool,
39 pub priority: u16,
41 pub reliability_parameter: u32,
44 pub label: String,
46 pub protocol: String,
48}
49
50#[derive(Debug, Default, Clone)]
52pub struct DataChannelMessage {
53 pub association_handle: usize,
55 pub stream_id: u16,
57 pub ppi: PayloadProtocolIdentifier,
60 pub payload: BytesMut,
62
63 pub negotiated: bool,
69}
70
71#[derive(Debug, Default, Clone)]
73pub struct DataChannel {
74 config: DataChannelConfig,
75 association_handle: usize,
76 stream_id: u16,
77
78 read_outs: VecDeque<DataChannelMessage>,
79 write_outs: VecDeque<DataChannelMessage>,
80
81 messages_sent: usize,
83 messages_received: usize,
84 bytes_sent: usize,
85 bytes_received: usize,
86}
87
88impl DataChannel {
89 fn new(config: DataChannelConfig, association_handle: usize, stream_id: u16) -> Self {
90 Self {
91 config,
92 association_handle,
93 stream_id,
94 read_outs: VecDeque::new(),
95 write_outs: VecDeque::new(),
96 ..Default::default()
97 }
98 }
99
100 pub fn dial(
102 config: DataChannelConfig,
103 association_handle: usize,
104 stream_id: u16,
105 ) -> Result<Self> {
106 let mut data_channel = DataChannel::new(config.clone(), association_handle, stream_id);
107
108 let msg = Message::DataChannelOpen(DataChannelOpen {
116 channel_type: config.channel_type,
117 priority: config.priority,
118 reliability_parameter: config.reliability_parameter,
119 label: config.label.bytes().collect(),
120 protocol: config.protocol.bytes().collect(),
121 })
122 .marshal()?;
123
124 data_channel.write_outs.push_back(DataChannelMessage {
125 association_handle,
126 stream_id,
127 ppi: PayloadProtocolIdentifier::Dcep,
128 payload: msg,
129 negotiated: config.negotiated,
130 });
131
132 Ok(data_channel)
133 }
134
135 pub fn accept(
137 mut config: DataChannelConfig,
138 association_handle: usize,
139 stream_id: u16,
140 ppi: PayloadProtocolIdentifier,
141 buf: &[u8],
142 ) -> Result<Self> {
143 if ppi != PayloadProtocolIdentifier::Dcep {
144 return Err(Error::InvalidPayloadProtocolIdentifier(ppi as u8));
145 }
146
147 let mut read_buf = buf;
148 let msg = Message::unmarshal(&mut read_buf)?;
149
150 if let Message::DataChannelOpen(dco) = msg {
151 config.channel_type = dco.channel_type;
152 config.priority = dco.priority;
153 config.reliability_parameter = dco.reliability_parameter;
154 config.label = String::from_utf8(dco.label)?;
155 config.protocol = String::from_utf8(dco.protocol)?;
156 } else {
157 return Err(Error::InvalidMessageType(msg.message_type() as u8));
158 };
159
160 let mut data_channel = DataChannel::new(config, association_handle, stream_id);
161
162 data_channel.write_data_channel_ack()?;
163
164 Ok(data_channel)
165 }
166
167 pub fn messages_sent(&self) -> usize {
169 self.messages_sent
170 }
171
172 pub fn messages_received(&self) -> usize {
174 self.messages_received
175 }
176
177 pub fn bytes_sent(&self) -> usize {
179 self.bytes_sent
180 }
181
182 pub fn bytes_received(&self) -> usize {
184 self.bytes_received
185 }
186
187 pub fn association_handle(&self) -> usize {
189 self.association_handle
190 }
191
192 pub fn stream_identifier(&self) -> u16 {
194 self.stream_id
195 }
196
197 pub fn config(&self) -> &DataChannelConfig {
199 &self.config
200 }
201
202 fn handle_dcep<B>(&mut self, data: &mut B) -> Result<()>
203 where
204 B: Buf,
205 {
206 let msg = Message::unmarshal(data)?;
207
208 match msg {
209 Message::DataChannelOpen(_) => {
210 debug!("Received DATA_CHANNEL_OPEN");
213 self.write_data_channel_ack()?;
214 }
215 Message::DataChannelAck(_) => {
216 debug!("Received DATA_CHANNEL_ACK");
217 }
218 _ => {
219 return Err(Error::InvalidMessageType(msg.message_type() as u8));
220 }
221 };
222
223 Ok(())
224 }
225
226 fn write_data_channel_ack(&mut self) -> Result<()> {
227 let ack = Message::DataChannelAck(DataChannelAck {}).marshal()?;
228 self.write_outs.push_back(DataChannelMessage {
229 association_handle: self.association_handle,
230 stream_id: self.stream_id,
231 ppi: PayloadProtocolIdentifier::Dcep,
232 payload: ack,
233 negotiated: false,
234 });
235 Ok(())
236 }
237
238 fn write_data_channel_close(&mut self) -> Result<()> {
239 let close = Message::DataChannelClose(DataChannelClose {}).marshal()?;
240 self.write_outs.push_back(DataChannelMessage {
241 association_handle: self.association_handle,
242 stream_id: self.stream_id,
243 ppi: PayloadProtocolIdentifier::Dcep,
244 payload: close,
245 negotiated: false,
246 });
247 Ok(())
248 }
249
250 fn write_data_channel_high_threshold(&mut self, threshold: u32) -> Result<()> {
251 let low_threshold =
252 Message::DataChannelThreshold(DataChannelThreshold::High(threshold)).marshal()?;
253 self.write_outs.push_back(DataChannelMessage {
254 association_handle: self.association_handle,
255 stream_id: self.stream_id,
256 ppi: PayloadProtocolIdentifier::Dcep,
257 payload: low_threshold,
258 negotiated: false,
259 });
260 Ok(())
261 }
262
263 fn write_data_channel_low_threshold(&mut self, threshold: u32) -> Result<()> {
264 let low_threshold =
265 Message::DataChannelThreshold(DataChannelThreshold::Low(threshold)).marshal()?;
266 self.write_outs.push_back(DataChannelMessage {
267 association_handle: self.association_handle,
268 stream_id: self.stream_id,
269 ppi: PayloadProtocolIdentifier::Dcep,
270 payload: low_threshold,
271 negotiated: false,
272 });
273 Ok(())
274 }
275
276 pub fn set_buffered_amount_high_threshold(&mut self, threshold: u32) -> Result<()> {
279 self.write_data_channel_high_threshold(threshold)
280 }
281
282 pub fn set_buffered_amount_low_threshold(&mut self, threshold: u32) -> Result<()> {
285 self.write_data_channel_low_threshold(threshold)
286 }
287
288 pub fn get_reliability_params(channel_type: ChannelType) -> (bool, ReliabilityType) {
300 match channel_type {
301 ChannelType::Reliable => (false, ReliabilityType::Reliable),
302 ChannelType::ReliableUnordered => (true, ReliabilityType::Reliable),
303 ChannelType::PartialReliableRexmit => (false, ReliabilityType::Rexmit),
304 ChannelType::PartialReliableRexmitUnordered => (true, ReliabilityType::Rexmit),
305 ChannelType::PartialReliableTimed => (false, ReliabilityType::Timed),
306 ChannelType::PartialReliableTimedUnordered => (true, ReliabilityType::Timed),
307 }
308 }
309
310 pub fn get_channel_type_and_reliability_parameter(
315 ordered: bool,
316 max_retransmits: Option<u16>,
317 max_packet_life_time: Option<u16>,
318 ) -> (ChannelType, u32) {
319 let channel_type;
320 let reliability_parameter;
321
322 match (max_retransmits, max_packet_life_time) {
323 (None, None) => {
324 reliability_parameter = 0u32;
325 if ordered {
326 channel_type = ChannelType::Reliable;
327 } else {
328 channel_type = ChannelType::ReliableUnordered;
329 }
330 }
331
332 (Some(max_retransmits), _) => {
333 reliability_parameter = max_retransmits as u32;
334 if ordered {
335 channel_type = ChannelType::PartialReliableRexmit;
336 } else {
337 channel_type = ChannelType::PartialReliableRexmitUnordered;
338 }
339 }
340
341 (None, Some(max_packet_lifetime)) => {
342 reliability_parameter = max_packet_lifetime as u32;
343 if ordered {
344 channel_type = ChannelType::PartialReliableTimed;
345 } else {
346 channel_type = ChannelType::PartialReliableTimedUnordered;
347 }
348 }
349 }
350
351 (channel_type, reliability_parameter)
352 }
353
354 pub fn get_data_channel_message(is_string: bool, data: BytesMut) -> DataChannelMessage {
359 let ppi = match (is_string, data.len()) {
367 (false, 0) => PayloadProtocolIdentifier::BinaryEmpty,
368 (false, _) => PayloadProtocolIdentifier::Binary,
369 (true, 0) => PayloadProtocolIdentifier::StringEmpty,
370 (true, _) => PayloadProtocolIdentifier::String,
371 };
372
373 if data.is_empty() {
374 DataChannelMessage {
375 ppi,
376 payload: BytesMut::from(&[0][..]),
377 ..Default::default()
378 }
379 } else {
380 DataChannelMessage {
381 ppi,
382 payload: data,
383 ..Default::default()
384 }
385 }
386 }
387}
388
389impl sansio::Protocol<DataChannelMessage, DataChannelMessage, ()> for DataChannel {
390 type Rout = DataChannelMessage;
391 type Wout = DataChannelMessage;
392 type Eout = ();
393 type Error = Error;
394 type Time = ();
395
396 fn handle_read(&mut self, msg: DataChannelMessage) -> Result<()> {
399 self.messages_received += 1;
400 self.bytes_received += msg.payload.len();
401
402 if msg.ppi == PayloadProtocolIdentifier::Dcep {
403 let mut data_buf = &msg.payload[..];
404 self.handle_dcep(&mut data_buf)
405 } else {
406 self.read_outs.push_back(msg);
407 Ok(())
408 }
409 }
410
411 fn poll_read(&mut self) -> Option<DataChannelMessage> {
412 self.read_outs.pop_front()
413 }
414
415 fn handle_write(&mut self, mut msg: DataChannelMessage) -> Result<()> {
417 self.messages_sent += 1;
418 self.bytes_sent += msg.payload.len();
419
420 msg.association_handle = self.association_handle;
421 msg.stream_id = self.stream_id;
422 self.write_outs.push_back(msg);
423
424 Ok(())
425 }
426
427 fn poll_write(&mut self) -> Option<DataChannelMessage> {
429 self.write_outs.pop_front()
430 }
431
432 fn close(&mut self) -> Result<()> {
434 self.write_data_channel_close()
446 }
447}