@@ -250,8 +250,9 @@ defmodule ExICE.Priv.ICEAgent do
250
250
end
251
251
252
252
@ spec set_remote_credentials ( t ( ) , binary ( ) , binary ( ) ) :: t ( )
253
- def set_remote_credentials ( % __MODULE__ { state: :failed } = ice_agent , _ , _ ) do
254
- Logger . debug ( "Tried to set remote credentials in failed state. ICE restart needed. Ignoring." )
253
+ def set_remote_credentials ( % __MODULE__ { state: state } = ice_agent , _ , _ )
254
+ when state in [ :failed , :closed ] do
255
+ Logger . debug ( "Tried to set remote credentials in state #{ state } . Ignoring." )
255
256
ice_agent
256
257
end
257
258
@@ -286,8 +287,8 @@ defmodule ExICE.Priv.ICEAgent do
286
287
end
287
288
288
289
@ spec gather_candidates ( t ( ) ) :: t ( )
289
- def gather_candidates ( % __MODULE__ { state: :failed } = ice_agent ) do
290
- Logger . warning ( "Can't gather candidates in state failed. ICE restart needed . Ignoring." )
290
+ def gather_candidates ( % __MODULE__ { state: state } = ice_agent ) when state in [ :failed , :closed ] do
291
+ Logger . warning ( "Can't gather candidates in state #{ state } . Ignoring." )
291
292
ice_agent
292
293
end
293
294
@@ -364,9 +365,10 @@ defmodule ExICE.Priv.ICEAgent do
364
365
end
365
366
366
367
@ spec add_remote_candidate ( t ( ) , Candidate . t ( ) ) :: t ( )
367
- def add_remote_candidate ( % __MODULE__ { state: :failed } = ice_agent , _ ) do
368
+ def add_remote_candidate ( % __MODULE__ { state: state } = ice_agent , _ )
369
+ when state in [ :failed , :closed ] do
368
370
# Completed state will be caught by the next clause
369
- Logger . debug ( "Can't add remote candidate in state failed. ICE restart needed . Ignoring." )
371
+ Logger . debug ( "Can't add remote candidate in state #{ state } . Ignoring." )
370
372
ice_agent
371
373
end
372
374
@@ -455,7 +457,7 @@ defmodule ExICE.Priv.ICEAgent do
455
457
end
456
458
457
459
@ spec end_of_candidates ( t ( ) ) :: t ( )
458
- def end_of_candidates ( % __MODULE__ { state: :failed } = ice_agent ) do
460
+ def end_of_candidates ( % __MODULE__ { state: state } = ice_agent ) when state in [ :failed , :closed ] do
459
461
Logger . debug ( "Can't set end-of-candidates flag in state failed. Ignoring." )
460
462
ice_agent
461
463
end
@@ -537,12 +539,22 @@ defmodule ExICE.Priv.ICEAgent do
537
539
end
538
540
539
541
@ spec restart ( t ( ) ) :: t ( )
542
+ def restart ( % __MODULE__ { state: :closed } = ice_agent ) do
543
+ Logger . debug ( "Can't restart ICE in state closed. Ignoring." )
544
+ ice_agent
545
+ end
546
+
540
547
def restart ( ice_agent ) do
541
548
Logger . debug ( "Restarting ICE" )
542
549
do_restart ( ice_agent )
543
550
end
544
551
545
552
@ spec handle_ta_timeout ( t ( ) ) :: t ( )
553
+ def handle_ta_timeout ( % __MODULE__ { state: :closed } = ice_agent ) do
554
+ Logger . debug ( "Ta timer fired in closed state. Ignoring." )
555
+ ice_agent
556
+ end
557
+
546
558
def handle_ta_timeout ( % __MODULE__ { state: state } = ice_agent )
547
559
when state in [ :completed , :failed ] do
548
560
Logger . warning ( """
@@ -694,6 +706,11 @@ defmodule ExICE.Priv.ICEAgent do
694
706
end
695
707
696
708
@ spec handle_tr_rtx_timeout ( t ( ) , integer ( ) ) :: t ( )
709
+ def handle_tr_rtx_timeout ( % __MODULE__ { state: :closed } = ice_agent , _ ) do
710
+ Logger . debug ( "Transaction rtx timer fired in state closed. Ignoring." )
711
+ ice_agent
712
+ end
713
+
697
714
def handle_tr_rtx_timeout ( ice_agent , t_id ) when is_map_key ( ice_agent . conn_checks , t_id ) do
698
715
# Mark transaction id as ready to be retransmitted.
699
716
# We will do this in handle_ta_timeout as it has to be paced.
@@ -725,8 +742,9 @@ defmodule ExICE.Priv.ICEAgent do
725
742
end
726
743
727
744
@ spec handle_eoc_timeout ( t ( ) ) :: t ( )
728
- def handle_eoc_timeout ( % __MODULE__ { state: :failed } = ice_agent ) do
729
- Logger . debug ( "EOC timer fired but we are in the failed state. Ignoring." )
745
+ def handle_eoc_timeout ( % __MODULE__ { state: state } = ice_agent )
746
+ when state in [ :failed , :closed ] do
747
+ Logger . debug ( "EOC timer fired but we are in the #{ state } state. Ignoring." )
730
748
% { ice_agent | eoc_timer: nil }
731
749
end
732
750
@@ -742,6 +760,11 @@ defmodule ExICE.Priv.ICEAgent do
742
760
end
743
761
744
762
@ spec handle_pair_timeout ( t ( ) ) :: t ( )
763
+ def handle_pair_timeout ( % __MODULE__ { state: :closed } = ice_agent ) do
764
+ Logger . debug ( "Pair timer fired in closed state. Ignoring." )
765
+ ice_agent
766
+ end
767
+
745
768
def handle_pair_timeout ( ice_agent ) do
746
769
start_pair_timer ( )
747
770
@@ -792,6 +815,11 @@ defmodule ExICE.Priv.ICEAgent do
792
815
end
793
816
794
817
@ spec handle_keepalive_timeout ( t ( ) , integer ( ) ) :: t ( )
818
+ def handle_keepalive_timeout ( % __MODULE__ { state: :closed } = ice_agent , _ ) do
819
+ Logger . debug ( "Keepalive timer fired in closed state. Ignoring." )
820
+ ice_agent
821
+ end
822
+
795
823
def handle_keepalive_timeout ( % __MODULE__ { selected_pair_id: id } = ice_agent , id ) do
796
824
# if pair was selected, send keepalives only on that pair
797
825
s_pair = Map . fetch! ( ice_agent . checklist , id )
@@ -842,7 +870,8 @@ defmodule ExICE.Priv.ICEAgent do
842
870
:inet . port_number ( ) ,
843
871
binary ( )
844
872
) :: t ( )
845
- def handle_udp ( % { state: :failed } = ice_agent , _socket , _src_ip , _src_port , _packet ) do
873
+ def handle_udp ( % { state: state } = ice_agent , _socket , _src_ip , _src_port , _packet )
874
+ when state in [ :failed , :closed ] do
846
875
ice_agent
847
876
end
848
877
@@ -868,6 +897,11 @@ defmodule ExICE.Priv.ICEAgent do
868
897
end
869
898
870
899
@ spec handle_ex_turn_msg ( t ( ) , reference ( ) , ExTURN.Client . notification_message ( ) ) :: t ( )
900
+ def handle_ex_turn_msg ( % __MODULE__ { state: :closed } = ice_agent , _ , _ ) do
901
+ Logger . debug ( "Received ex_turn message in closed state. Ignoring." )
902
+ ice_agent
903
+ end
904
+
871
905
def handle_ex_turn_msg ( ice_agent , client_ref , msg ) do
872
906
tr_id_tr = find_gathering_transaction ( ice_agent . gathering_transactions , client_ref )
873
907
@@ -919,6 +953,18 @@ defmodule ExICE.Priv.ICEAgent do
919
953
end
920
954
end
921
955
956
+ @ spec close ( t ( ) ) :: t ( )
957
+ def close ( % __MODULE__ { state: :closed } = ice_agent ) do
958
+ ice_agent
959
+ end
960
+
961
+ def close ( % __MODULE__ { } = ice_agent ) do
962
+ ice_agent . sockets
963
+ |> Enum . reduce ( ice_agent , fn socket , ice_agent -> close_socket ( ice_agent , socket ) end )
964
+ |> change_gathering_state ( :complete , notify: false )
965
+ |> change_connection_state ( :closed , notify: false )
966
+ end
967
+
922
968
## PRIV API
923
969
924
970
defp create_srflx_gathering_transactions ( stun_servers , sockets ) do
@@ -2497,7 +2543,6 @@ defmodule ExICE.Priv.ICEAgent do
2497
2543
end
2498
2544
2499
2545
defp generate_credentials ( ) do
2500
- # TODO am I using Base.encode64 correctly?
2501
2546
ufrag = :crypto . strong_rand_bytes ( 3 ) |> Base . encode64 ( )
2502
2547
pwd = :crypto . strong_rand_bytes ( 16 ) |> Base . encode64 ( )
2503
2548
{ ufrag , pwd }
@@ -2523,9 +2568,13 @@ defmodule ExICE.Priv.ICEAgent do
2523
2568
end
2524
2569
end
2525
2570
2526
- defp change_gathering_state ( ice_agent , new_gathering_state ) do
2527
- Logger . debug ( "Gathering state change: #{ ice_agent . gathering_state } -> #{ new_gathering_state } " )
2528
- notify ( ice_agent . on_gathering_state_change , { :gathering_state_change , new_gathering_state } )
2571
+ defp change_gathering_state ( ice_agent , new_gathering_state , opts \\ [ ] ) do
2572
+ Logger . debug ( "Gatering state change: #{ ice_agent . gathering_state } -> #{ new_gathering_state } " )
2573
+
2574
+ if opts [ :notify ] != false do
2575
+ notify ( ice_agent . on_gathering_state_change , { :gathering_state_change , new_gathering_state } )
2576
+ end
2577
+
2529
2578
% __MODULE__ { ice_agent | gathering_state: new_gathering_state }
2530
2579
end
2531
2580
@@ -2551,7 +2600,9 @@ defmodule ExICE.Priv.ICEAgent do
2551
2600
2552
2601
@ doc false
2553
2602
@ spec change_connection_state ( t ( ) , atom ( ) ) :: t ( )
2554
- def change_connection_state ( ice_agent , :failed ) do
2603
+ def change_connection_state ( ice_agent , new_state , opts \\ [ ] )
2604
+
2605
+ def change_connection_state ( ice_agent , :failed , opts ) do
2555
2606
ice_agent =
2556
2607
Enum . reduce ( ice_agent . sockets , ice_agent , fn socket , ice_agent ->
2557
2608
close_socket ( ice_agent , socket )
@@ -2599,10 +2650,10 @@ defmodule ExICE.Priv.ICEAgent do
2599
2650
nominating?: { false , nil }
2600
2651
}
2601
2652
|> disable_timer ( )
2602
- |> do_change_connection_state ( :failed )
2653
+ |> do_change_connection_state ( :failed , opts )
2603
2654
end
2604
2655
2605
- def change_connection_state ( ice_agent , :completed ) do
2656
+ def change_connection_state ( ice_agent , :completed , opts ) do
2606
2657
selected_pair = Map . fetch! ( ice_agent . checklist , ice_agent . selected_pair_id )
2607
2658
succeeded_pair = Map . fetch! ( ice_agent . checklist , selected_pair . succeeded_pair_id )
2608
2659
@@ -2632,16 +2683,20 @@ defmodule ExICE.Priv.ICEAgent do
2632
2683
end
2633
2684
end )
2634
2685
2635
- do_change_connection_state ( ice_agent , :completed )
2686
+ do_change_connection_state ( ice_agent , :completed , opts )
2636
2687
end
2637
2688
2638
- def change_connection_state ( ice_agent , new_conn_state ) do
2639
- do_change_connection_state ( ice_agent , new_conn_state )
2689
+ def change_connection_state ( ice_agent , new_conn_state , opts ) do
2690
+ do_change_connection_state ( ice_agent , new_conn_state , opts )
2640
2691
end
2641
2692
2642
- defp do_change_connection_state ( ice_agent , new_conn_state ) do
2693
+ defp do_change_connection_state ( ice_agent , new_conn_state , opts ) do
2643
2694
Logger . debug ( "Connection state change: #{ ice_agent . state } -> #{ new_conn_state } " )
2644
- notify ( ice_agent . on_connection_state_change , { :connection_state_change , new_conn_state } )
2695
+
2696
+ if opts [ :notify ] != false do
2697
+ notify ( ice_agent . on_connection_state_change , { :connection_state_change , new_conn_state } )
2698
+ end
2699
+
2645
2700
% __MODULE__ { ice_agent | state: new_conn_state }
2646
2701
end
2647
2702
0 commit comments