Skip to content

Commit 71f56b3

Browse files
committed
Forward data received on a faild pair and re-schedule this pair
1 parent c0d55fa commit 71f56b3

File tree

2 files changed

+66
-15
lines changed

2 files changed

+66
-15
lines changed

lib/ex_ice/priv/ice_agent.ex

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -600,20 +600,7 @@ defmodule ExICE.Priv.ICEAgent do
600600
%CandidatePair{} =
601601
pair = Checklist.find_pair(ice_agent.checklist, local_cand.base.id, remote_cand.id)
602602

603-
case pair.state do
604-
:succeeded ->
605-
pair = Map.fetch!(ice_agent.checklist, pair.discovered_pair_id)
606-
handle_data_message(ice_agent, pair, packet)
607-
608-
:failed ->
609-
Logger.error("Received data on failed pair! Ignoring. Pair id: #{pair.id}")
610-
ice_agent
611-
612-
_other ->
613-
# We might receive data on a pair that we haven't checked
614-
# on our side yet.
615-
handle_data_message(ice_agent, pair, packet)
616-
end
603+
handle_data_message(ice_agent, pair, packet)
617604
end
618605
end
619606

@@ -972,7 +959,39 @@ defmodule ExICE.Priv.ICEAgent do
972959
end
973960
end
974961

962+
defp handle_data_message(ice_agent, %{state: :succeeded} = pair, packet) do
963+
# take final pair as local candidate might be srflx
964+
pair = Map.fetch!(ice_agent.checklist, pair.discovered_pair_id)
965+
do_handle_data_message(ice_agent, pair, packet)
966+
end
967+
968+
defp handle_data_message(ice_agent, %{state: :failed} = pair, packet)
969+
when ice_agent.state in [:checking, :connected] do
970+
Logger.debug("""
971+
Received data on failed pair. Rescheduling pair for conn check. Pair id: #{pair.id}\
972+
""")
973+
974+
ice_agent = do_handle_data_message(ice_agent, pair, packet)
975+
976+
# re-schedule pair
977+
pair = %{pair | state: :waiting}
978+
ice_agent = put_in(ice_agent.checklist[pair.id], pair)
979+
update_ta_timer(ice_agent)
980+
end
981+
982+
defp handle_data_message(ice_agent, %{state: :failed} = pair, _packet) do
983+
raise """
984+
Received data on failed pair in state: #{ice_agent.state}. Pair id: #{pair.id}.
985+
This should never happen.
986+
"""
987+
end
988+
989+
# We might receive data on a pair that we haven't check on our side yet.
975990
defp handle_data_message(ice_agent, pair, packet) do
991+
do_handle_data_message(ice_agent, pair, packet)
992+
end
993+
994+
defp do_handle_data_message(ice_agent, pair, packet) do
976995
pair = %CandidatePair{pair | last_seen: now()}
977996
ice_agent = put_in(ice_agent.checklist[pair.id], pair)
978997

test/priv/ice_agent_test.exs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ defmodule ExICE.Priv.ICEAgentTest do
114114
end
115115
end
116116

117-
test "don't add pairs with srflx local candidate to the checklist" do
117+
test "doesn't add pairs with srflx local candidate to the checklist" do
118118
ice_agent =
119119
ICEAgent.new(
120120
controlling_process: self(),
@@ -150,6 +150,38 @@ defmodule ExICE.Priv.ICEAgentTest do
150150
assert pair.local_cand_id == host_cand.base.id
151151
end
152152

153+
test "forwards data received on a faild pair and re-schedules" do
154+
remote_cand = ExICE.Candidate.new(:host, address: {192, 168, 0, 3}, port: 8445)
155+
156+
ice_agent =
157+
ICEAgent.new(
158+
controlling_process: self(),
159+
role: :controlling,
160+
transport_module: Transport.Mock,
161+
if_discovery_module: IfDiscovery.Mock
162+
)
163+
|> ICEAgent.set_remote_credentials("someufrag", "somepwd")
164+
|> ICEAgent.gather_candidates()
165+
|> ICEAgent.add_remote_candidate(remote_cand)
166+
167+
[socket] = ice_agent.sockets
168+
[host_cand] = Map.values(ice_agent.local_cands)
169+
170+
pair = CandidatePair.new(host_cand, remote_cand, ice_agent.role, :failed)
171+
172+
ice_agent = %{ice_agent | checklist: %{pair.id => pair}}
173+
174+
ice_agent =
175+
ICEAgent.handle_udp(ice_agent, socket, remote_cand.address, remote_cand.port, "some data")
176+
177+
# assert that data has been passed
178+
assert_receive {:ex_ice, _pid, {:data, "some data"}}
179+
180+
# assert that pair is re-scheduled
181+
assert [pair] = Map.values(ice_agent.checklist)
182+
assert pair.state == :waiting
183+
end
184+
153185
describe "sends keepalives" do
154186
setup do
155187
remote_cand = ExICE.Candidate.new(:host, address: {192, 168, 0, 2}, port: 8445)

0 commit comments

Comments
 (0)