@@ -17,7 +17,7 @@ defmodule ExLibSRT.Server do
1717 * `t:srt_server_conn_closed/0` - a client connection has been closed
1818 * `t:srt_server_error/0` - server has encountered an error
1919 * `t:srt_data/0` - server has received new data on a client connection
20- * `t:srt_server_connect_request/0` - server has triggered a new connection request
20+ * `t:srt_server_connect_request/0` - server has triggered a new connection request
2121 (see `accept_awaiting_connect_request/1` and `reject_awaiting_connect_request/1` for answering the request)
2222
2323 ### Accepting connections
@@ -33,11 +33,13 @@ defmodule ExLibSRT.Server do
3333 > #### Response timeout {: .warning}
3434 >
3535 > It is very important to answer the connection request as fast as possible.
36- > Due to how `libsrt` works, while the server waits for the response it blocks the receiving thread
36+ > Due to how `libsrt` works, while the server waits for the response it blocks the receiving thread
3737 > and potentially interrupts other ongoing connections.
3838 """
3939
40- @ type t :: reference ( )
40+ use Agent
41+
42+ @ type t :: pid ( )
4143
4244 @ type connection_id :: non_neg_integer ( )
4345
@@ -49,14 +51,37 @@ defmodule ExLibSRT.Server do
4951 { :srt_server_connect_request , address :: String . t ( ) , stream_id :: String . t ( ) }
5052
5153 @ doc """
52- Starts a new SRT server binding to given address and port.
54+ Starts a new SRT server binding to given address and port and links to current process.
55+
56+ One may usually want to bind to `0.0.0.0` address.
57+ """
58+ @ spec start_link ( address :: String . t ( ) , port :: non_neg_integer ( ) ) ::
59+ { :ok , t ( ) } | { :error , reason :: String . t ( ) , error_code :: integer ( ) }
60+ def start_link ( address , port ) do
61+ case ExLibSRT.Native . start_server ( address , port ) do
62+ { :ok , server_ref } ->
63+ Agent . start_link ( fn -> server_ref end )
64+
65+ { :error , reason , error_code } ->
66+ { :error , reason , error_code }
67+ end
68+ end
69+
70+ @ doc """
71+ Starts a new SRT server outside the supervision tree, binding to given address and port.
5372
5473 One may usually want to bind to `0.0.0.0` address.
5574 """
5675 @ spec start ( address :: String . t ( ) , port :: non_neg_integer ( ) ) ::
5776 { :ok , t ( ) } | { :error , reason :: String . t ( ) , error_code :: integer ( ) }
5877 def start ( address , port ) do
59- ExLibSRT.Native . start_server ( address , port )
78+ case ExLibSRT.Native . start_server ( address , port ) do
79+ { :ok , server_ref } ->
80+ Agent . start ( fn -> server_ref end , name: { :global , server_ref } )
81+
82+ { :error , reason , error_code } ->
83+ { :error , reason , error_code }
84+ end
6085 end
6186
6287 @ doc """
@@ -65,25 +90,31 @@ defmodule ExLibSRT.Server do
6590 Stopping a server should gracefuly close all the client connections.
6691 """
6792 @ spec stop ( t ( ) ) :: :ok
68- def stop ( server ) do
69- ExLibSRT.Native . stop_server ( server )
93+ def stop ( agent ) do
94+ server_ref = Agent . get ( agent , & & 1 )
95+ ExLibSRT.Native . stop_server ( server_ref )
96+ Agent . stop ( agent )
7097 end
7198
7299 @ doc """
73100 Acccepts the currently awaiting connection request.
74101 """
75102 @ spec accept_awaiting_connect_request ( t ( ) ) :: :ok | { :error , reason :: String . t ( ) }
76- def accept_awaiting_connect_request ( server ) ,
77- do: ExLibSRT.Native . accept_awaiting_connect_request ( self ( ) , server )
103+ def accept_awaiting_connect_request ( agent ) do
104+ server_ref = Agent . get ( agent , & & 1 )
105+ ExLibSRT.Native . accept_awaiting_connect_request ( self ( ) , server_ref )
106+ end
78107
79108 @ doc """
80109 Acccepts the currently awaiting connection request and starts a separate connection process
81110 """
82111 @ spec accept_awaiting_connect_request_with_handler ( ExLibSRT.Connection.Handler . t ( ) , t ( ) ) ::
83112 { :ok , ExLibSRT.Connection . t ( ) } | { :error , reason :: any ( ) }
84- def accept_awaiting_connect_request_with_handler ( handler , server ) do
113+ def accept_awaiting_connect_request_with_handler ( handler , agent ) do
114+ server_ref = Agent . get ( agent , & & 1 )
115+
85116 with { :ok , handler } <- ExLibSRT.Connection . start ( handler ) do
86- case ExLibSRT.Native . accept_awaiting_connect_request ( handler , server ) do
117+ case ExLibSRT.Native . accept_awaiting_connect_request ( handler , server_ref ) do
87118 :ok ->
88119 { :ok , handler }
89120
@@ -99,21 +130,27 @@ defmodule ExLibSRT.Server do
99130 Rejects the currently awaiting connection request.
100131 """
101132 @ spec reject_awaiting_connect_request ( t ( ) ) :: :ok | { :error , reason :: String . t ( ) }
102- def reject_awaiting_connect_request ( server ) ,
103- do: ExLibSRT.Native . reject_awaiting_connect_request ( server )
133+ def reject_awaiting_connect_request ( agent ) do
134+ server_ref = Agent . get ( agent , & & 1 )
135+ ExLibSRT.Native . reject_awaiting_connect_request ( server_ref )
136+ end
104137
105138 @ doc """
106139 Closes the connection to the given client.
107140 """
108141 @ spec close_server_connection ( connection_id ( ) , t ( ) ) :: :ok | { :error , reason :: String . t ( ) }
109- def close_server_connection ( connection_id , server ) ,
110- do: ExLibSRT.Native . close_server_connection ( connection_id , server )
142+ def close_server_connection ( connection_id , agent ) do
143+ server_ref = Agent . get ( agent , & & 1 )
144+ ExLibSRT.Native . close_server_connection ( connection_id , server_ref )
145+ end
111146
112147 @ doc """
113148 Reads socket statistics.
114149 """
115150 @ spec read_socket_stats ( connection_id ( ) , t ( ) ) ::
116151 { :ok , ExLibSRT.SocketStats . t ( ) } | { :error , reason :: String . t ( ) }
117- def read_socket_stats ( connection_id , server ) ,
118- do: ExLibSRT.Native . read_server_socket_stats ( connection_id , server )
152+ def read_socket_stats ( connection_id , agent ) do
153+ server_ref = Agent . get ( agent , & & 1 )
154+ ExLibSRT.Native . read_server_socket_stats ( connection_id , server_ref )
155+ end
119156end
0 commit comments