Skip to content

Commit

Permalink
Improve DCEP
Browse files Browse the repository at this point in the history
  • Loading branch information
LVala committed Aug 20, 2024
1 parent aa03162 commit 2133705
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
35 changes: 14 additions & 21 deletions lib/ex_webrtc/sctp_transport/dcep.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,37 @@ defmodule ExWebRTC.SCTPTransport.DCEP do
defmodule DataChannelOpen do
@moduledoc false

@enforce_keys [:reliability, :order, :label, :protocol, :priority]
defstruct @enforce_keys ++ [:max_rtx, :lifetime]
@enforce_keys [:reliability, :order, :label, :protocol, :priority, :param]
defstruct @enforce_keys

def decode(
<<ch_type::8, priority::16, param::32, label_len::16, proto_len::16, rest::binary>>
) do
with {:ok, reliability, order} <- to_channel_type(ch_type),
params <- %{reliability: reliability, order: order},
params <- to_reliability_param(params, param),
<<label::binary-size(label_len), rest::binary>> <- rest,
<<protocol::binary-size(proto_len)>> <- rest do
params
|> Map.merge(%{label: label, protocol: protocol, priority: priority})
|> then(&{:ok, struct!(__MODULE__, &1)})
dca =
%__MODULE__{
reliability: reliability,
order: order,
param: param,
label: label,
protocol: protocol,
priority: priority
}

{:ok, dca}
else
_other -> :error
end
end

def encode(%__MODULE__{} = dco) do
ch_type = from_channel_type(dco.reliability, dco.order)
param = from_reliability_param(dco)
label_len = byte_size(dco.label)
proto_len = byte_size(dco.protocol)

<<0x03::8, ch_type::8, dco.priority::16, param::32, label_len::16, proto_len::16,
<<0x03::8, ch_type::8, dco.priority::16, dco.param::32, label_len::16, proto_len::16,
dco.label::binary-size(label_len), dco.protocol::binary-size(proto_len)>>
end

Expand All @@ -61,18 +66,6 @@ defmodule ExWebRTC.SCTPTransport.DCEP do
defp from_channel_type(:rexmit, :unordered), do: 0x81
defp from_channel_type(:timed, :ordered), do: 0x02
defp from_channel_type(:timed, :unordered), do: 0x82

defp to_reliability_param(%{reliability: :reliable} = params, _param), do: params

defp to_reliability_param(%{reliability: :rexmit} = params, param),
do: Map.put(params, :max_rtx, param)

defp to_reliability_param(%{reliability: :timed} = params, param),
do: Map.put(params, :lifetime, param)

defp from_reliability_param(%__MODULE__{reliability: :rexmit, max_rtx: val}), do: val
defp from_reliability_param(%__MODULE__{reliability: :timed, lifetime: val}), do: val
defp from_reliability_param(%__MODULE__{reliability: :reliable}), do: 0
end

def decode(<<0x03::8, rest::binary>>), do: DataChannelOpen.decode(rest)
Expand Down
3 changes: 1 addition & 2 deletions test/ex_webrtc/sctp_transport/dcep_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ defmodule ExWebRTC.SCTPTransport.DCEPTest do
label: "hello",
protocol: "webrtc",
priority: 5,
max_rtx: nil,
lifetime: 100
param: 100
}

@encoded_dca <<2>>
Expand Down

0 comments on commit 2133705

Please sign in to comment.