Skip to content

Commit 03475ad

Browse files
author
yngrtc
committed
refactor DataChannelMessageParams #23
1 parent 2f741fc commit 03475ad

File tree

3 files changed

+50
-57
lines changed

3 files changed

+50
-57
lines changed

src/handler/datachannel.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::messages::{
55
use datachannel::message::{message_channel_ack::*, message_channel_open::*, message_type::*, *};
66
use log::{debug, error, warn};
77
use retty::channel::{Handler, InboundContext, InboundHandler, OutboundContext, OutboundHandler};
8+
use sctp::ReliabilityType;
89
use shared::error::Result;
910
use shared::marshal::*;
1011

@@ -46,7 +47,9 @@ impl InboundHandler for DataChannelInbound {
4647
message.stream_id,
4748
message.data_message_type);
4849

49-
let _open = DataChannelOpen::unmarshal(&mut buf)?;
50+
let data_channel_open = DataChannelOpen::unmarshal(&mut buf)?;
51+
let (unordered, reliability_type) =
52+
get_reliability_params(data_channel_open.channel_type);
5053

5154
let payload = Message::DataChannelAck(DataChannelAck {}).marshal()?;
5255
Ok((
@@ -59,12 +62,12 @@ impl InboundHandler for DataChannelInbound {
5962
association_handle: message.association_handle,
6063
stream_id: message.stream_id,
6164
data_message_type: DataChannelMessageType::Control,
62-
params: DataChannelMessageParams::Outbound {
63-
ordered: true,
64-
reliable: true,
65-
max_rtx_count: 0,
66-
max_rtx_millis: 0,
67-
},
65+
params: Some(DataChannelMessageParams {
66+
unordered,
67+
reliability_type,
68+
reliability_parameter: data_channel_open
69+
.reliability_parameter,
70+
}),
6871
payload,
6972
}),
7073
))
@@ -138,12 +141,7 @@ impl OutboundHandler for DataChannelOutbound {
138141
association_handle: message.association_handle,
139142
stream_id: message.stream_id,
140143
data_message_type: DataChannelMessageType::Text,
141-
params: DataChannelMessageParams::Outbound {
142-
ordered: true,
143-
reliable: true,
144-
max_rtx_count: 0,
145-
max_rtx_millis: 0,
146-
},
144+
params: None,
147145
payload,
148146
})),
149147
});
@@ -183,3 +181,16 @@ impl Handler for DataChannelHandler {
183181
)
184182
}
185183
}
184+
185+
fn get_reliability_params(channel_type: ChannelType) -> (bool, ReliabilityType) {
186+
let (unordered, reliability_type) = match channel_type {
187+
ChannelType::Reliable => (false, ReliabilityType::Reliable),
188+
ChannelType::ReliableUnordered => (true, ReliabilityType::Reliable),
189+
ChannelType::PartialReliableRexmit => (false, ReliabilityType::Rexmit),
190+
ChannelType::PartialReliableRexmitUnordered => (true, ReliabilityType::Rexmit),
191+
ChannelType::PartialReliableTimed => (false, ReliabilityType::Timed),
192+
ChannelType::PartialReliableTimedUnordered => (true, ReliabilityType::Timed),
193+
};
194+
195+
(unordered, reliability_type)
196+
}

src/handler/sctp.rs

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use retty::channel::{Handler, InboundContext, InboundHandler, OutboundContext, O
99
use retty::transport::TransportContext;
1010
use sctp::{
1111
AssociationEvent, AssociationHandle, DatagramEvent, EndpointEvent, Event, Payload,
12-
PayloadProtocolIdentifier, ReliabilityType, StreamEvent, Transmit,
12+
PayloadProtocolIdentifier, StreamEvent, Transmit,
1313
};
1414
use shared::error::{Error, Result};
1515
use std::cell::RefCell;
@@ -124,9 +124,7 @@ impl InboundHandler for SctpInbound {
124124
association_handle: ch.0,
125125
stream_id: id,
126126
data_message_type: to_data_message_type(chunks.ppi),
127-
params: DataChannelMessageParams::Inbound {
128-
seq_num: chunks.ssn,
129-
},
127+
params: None,
130128
payload: BytesMut::from(&self.internal_buffer[0..n]),
131129
}));
132130
}
@@ -315,33 +313,26 @@ impl OutboundHandler for SctpOutbound {
315313
if let Some(conn) =
316314
sctp_associations.get_mut(&AssociationHandle(message.association_handle))
317315
{
318-
if let DataChannelMessageParams::Outbound {
319-
ordered,
320-
reliable,
321-
max_rtx_count,
322-
max_rtx_millis,
323-
} = message.params
316+
let mut stream = conn.stream(message.stream_id)?;
317+
if let Some(DataChannelMessageParams {
318+
unordered,
319+
reliability_type,
320+
reliability_parameter,
321+
}) = message.params
324322
{
325-
let mut stream = conn.stream(message.stream_id)?;
326-
let (rel_type, rel_val) = if !reliable {
327-
if max_rtx_millis == 0 {
328-
(ReliabilityType::Rexmit, max_rtx_count)
329-
} else {
330-
(ReliabilityType::Timed, max_rtx_millis)
331-
}
332-
} else {
333-
(ReliabilityType::Reliable, 0)
334-
};
335-
336-
stream.set_reliability_params(!ordered, rel_type, rel_val)?;
337-
stream.write_with_ppi(
338-
&message.payload,
339-
to_ppid(message.data_message_type, message.payload.len()),
323+
stream.set_reliability_params(
324+
unordered,
325+
reliability_type,
326+
reliability_parameter,
340327
)?;
328+
}
329+
stream.write_with_ppi(
330+
&message.payload,
331+
to_ppid(message.data_message_type, message.payload.len()),
332+
)?;
341333

342-
while let Some(x) = conn.poll_transmit(msg.now) {
343-
transmits.extend(split_transmit(x));
344-
}
334+
while let Some(x) = conn.poll_transmit(msg.now) {
335+
transmits.extend(split_transmit(x));
345336
}
346337
} else {
347338
return Err(Error::ErrAssociationNotExisted);
@@ -447,10 +438,6 @@ fn to_ppid(message_type: DataChannelMessageType, length: usize) -> PayloadProtoc
447438
PayloadProtocolIdentifier::BinaryEmpty
448439
}
449440
}
450-
_ =>
451-
// case DataMessageType::CONTROL: // TODO: remove DataMessageType::NONE once DcSctp is landed
452-
{
453-
PayloadProtocolIdentifier::Dcep
454-
}
441+
_ => PayloadProtocolIdentifier::Dcep,
455442
}
456443
}

src/messages.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use bytes::BytesMut;
22
use retty::transport::TransportContext;
3+
use sctp::ReliabilityType;
34
use std::time::Instant;
45

56
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
@@ -11,16 +12,10 @@ pub(crate) enum DataChannelMessageType {
1112
}
1213

1314
#[derive(Debug)]
14-
pub(crate) enum DataChannelMessageParams {
15-
Inbound {
16-
seq_num: u16,
17-
},
18-
Outbound {
19-
ordered: bool,
20-
reliable: bool,
21-
max_rtx_count: u32,
22-
max_rtx_millis: u32,
23-
},
15+
pub(crate) struct DataChannelMessageParams {
16+
pub(crate) unordered: bool,
17+
pub(crate) reliability_type: ReliabilityType,
18+
pub(crate) reliability_parameter: u32,
2419
}
2520

2621
#[derive(Debug, Clone, Eq, PartialEq)]
@@ -35,7 +30,7 @@ pub struct DataChannelMessage {
3530
pub(crate) association_handle: usize,
3631
pub(crate) stream_id: u16,
3732
pub(crate) data_message_type: DataChannelMessageType,
38-
pub(crate) params: DataChannelMessageParams,
33+
pub(crate) params: Option<DataChannelMessageParams>,
3934
pub(crate) payload: BytesMut,
4035
}
4136

0 commit comments

Comments
 (0)