Skip to content

Commit

Permalink
Simplify start of DTLS handshake
Browse files Browse the repository at this point in the history
  • Loading branch information
LVala committed Nov 3, 2023
1 parent 11e2947 commit 89bdd5d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 19 deletions.
2 changes: 1 addition & 1 deletion examples/example.exs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ defmodule Peer do
end
end

mode = :active
mode = :passive
{:ok, pid} = Peer.start_link(mode)
ref = Process.monitor(pid)

Expand Down
2 changes: 1 addition & 1 deletion examples/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const start_connection = async (ws) => {
}
};

const mode = "passive"
const mode = "active"

const ws = new WebSocket("ws://127.0.0.1:4000/websocket");
ws.onclose = event => console.log("WebSocket was closed", event);
Expand Down
22 changes: 5 additions & 17 deletions lib/ex_webrtc/dtls_transport.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ defmodule ExWebRTC.DTLSTransport do
%__MODULE__{dtls | client: client}
end

def start(%{ice_state: ice_state} = dtls, :active) do
def start(dtls, :active) do
{:ok, client} =
ExDTLS.start_link(
client_mode: true,
Expand All @@ -55,22 +55,15 @@ defmodule ExWebRTC.DTLSTransport do
cert: dtls.cert
)

dtls = %__MODULE__{dtls | client: client}

case ice_state do
state when state in [:active, :connected] ->
start_handshake(dtls)
dtls

_other ->
%__MODULE__{dtls | should_start: true}
end
# we assume that ICE in not in connected state yet
%__MODULE__{dtls | client: client, should_start: true}
end

def update_ice_state(dtls, :connected) do
dtls =
if dtls.should_start do
start_handshake(dtls)
{:ok, packets} = ExDTLS.do_handshake(dtls.client)
:ok = ICEAgent.send_data(dtls.ice_agent, packets)
%__MODULE__{dtls | should_start: false}
else
dtls
Expand Down Expand Up @@ -132,9 +125,4 @@ defmodule ExWebRTC.DTLSTransport do
dtls
end
end

defp start_handshake(dtls) do
{:ok, packets} = ExDTLS.do_handshake(dtls.client)
:ok = ICEAgent.send_data(dtls.ice_agent, packets)
end
end
5 changes: 5 additions & 0 deletions lib/ex_webrtc/peer_connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,11 @@ defmodule ExWebRTC.PeerConnection do
end
end

@impl true
def handle_call({:add_ice_candidate, _}, _from, %{current_remote_desc: nil} = state) do
{:reply, {:error, :no_remote_description}, state}
end

@impl true
def handle_call({:add_ice_candidate, candidate}, _from, state) do
with "candidate:" <> attr <- candidate.candidate do
Expand Down

0 comments on commit 89bdd5d

Please sign in to comment.