|
|
|
|
@ -4,7 +4,7 @@
|
|
|
|
|
%%% @doc
|
|
|
|
|
%%%
|
|
|
|
|
%%% @end
|
|
|
|
|
%%% Created : 20. 4月 2025 16:25
|
|
|
|
|
%%% Created : 20. 4月 2025 18:47
|
|
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
|
-module(efka_agent).
|
|
|
|
|
-author("anlicheng").
|
|
|
|
|
@ -19,15 +19,10 @@
|
|
|
|
|
|
|
|
|
|
-define(SERVER, ?MODULE).
|
|
|
|
|
|
|
|
|
|
-define(DISCONNECTED, 0).
|
|
|
|
|
-define(CONNECTING, 1).
|
|
|
|
|
-define(CONNECTED, 2).
|
|
|
|
|
|
|
|
|
|
-record(state, {
|
|
|
|
|
conn_pid :: pid() | undefined,
|
|
|
|
|
host :: string(),
|
|
|
|
|
port :: integer(),
|
|
|
|
|
status = ?DISCONNECTED
|
|
|
|
|
socket
|
|
|
|
|
}).
|
|
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
|
@ -50,11 +45,10 @@ start_link() ->
|
|
|
|
|
{ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
|
|
|
|
|
{stop, Reason :: term()} | ignore).
|
|
|
|
|
init([]) ->
|
|
|
|
|
{ok, Props} = application:get_env(efka, wss_server),
|
|
|
|
|
{ok, Props} = application:get_env(efka, tls_server),
|
|
|
|
|
Host = proplists:get_value(host, Props),
|
|
|
|
|
Port = proplists:get_value(port, Props),
|
|
|
|
|
|
|
|
|
|
%% 异步建立到服务器的链接
|
|
|
|
|
erlang:start_timer(0, self(), create_connection),
|
|
|
|
|
|
|
|
|
|
{ok, #state{host = Host, port = Port}}.
|
|
|
|
|
@ -62,7 +56,7 @@ init([]) ->
|
|
|
|
|
%% @private
|
|
|
|
|
%% @doc Handling call messages
|
|
|
|
|
-spec(handle_call(Request :: term(), From :: {pid(), Tag :: term()},
|
|
|
|
|
State :: #state{}) ->
|
|
|
|
|
State :: #state{}) ->
|
|
|
|
|
{reply, Reply :: term(), NewState :: #state{}} |
|
|
|
|
|
{reply, Reply :: term(), NewState :: #state{}, timeout() | hibernate} |
|
|
|
|
|
{noreply, NewState :: #state{}} |
|
|
|
|
|
@ -89,38 +83,27 @@ handle_cast(_Request, State = #state{}) ->
|
|
|
|
|
{stop, Reason :: term(), NewState :: #state{}}).
|
|
|
|
|
handle_info({timeout, _, create_connection}, State = #state{host = Host, port = Port}) ->
|
|
|
|
|
case connect(Host, Port) of
|
|
|
|
|
{ok, ConnPid} ->
|
|
|
|
|
{noreply, State#state{conn_pid = ConnPid, status = ?CONNECTING}};
|
|
|
|
|
{ok, Socket} ->
|
|
|
|
|
{noreply, State#state{socket = Socket}};
|
|
|
|
|
{error, Reason} ->
|
|
|
|
|
lager:debug("[efka_agent] connect host: ~p, port: ~p, get error: ~p", [Host, Port, Reason]),
|
|
|
|
|
lager:debug("[efka_agent] create_connection get error: ~p", [Reason]),
|
|
|
|
|
retry_connect(),
|
|
|
|
|
{noreply, State#state{conn_pid = undefined, status = ?DISCONNECTED}}
|
|
|
|
|
{noreply, State#state{socket = undefined}}
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
%% upgrade逻辑处理
|
|
|
|
|
handle_info({gun_upgrade, ConnPid, _StreamRef, [<<"websocket">>], Headers}, State = #state{conn_pid = ConnPid}) ->
|
|
|
|
|
lager:debug("[efka_agent] upgrade success, headers: ~p", [Headers]),
|
|
|
|
|
{noreply, State#state{status = ?CONNECTED}};
|
|
|
|
|
handle_info({gun_response, ConnPid, _, _, Status, Headers}, State = #state{conn_pid = ConnPid}) ->
|
|
|
|
|
lager:debug("[efka_agent] upgrade failed, status: ~p, headers: ~p", [Status, Headers]),
|
|
|
|
|
handle_info({ssl, Socket, Data}, State = #state{socket = Socket}) ->
|
|
|
|
|
lager:debug("[efka_agent] socket get message: ~p", [Data]),
|
|
|
|
|
retry_connect(),
|
|
|
|
|
gun:close(ConnPid),
|
|
|
|
|
{noreply, State#state{conn_pid = undefined, status = ?DISCONNECTED}};
|
|
|
|
|
{noreply, State#state{socket = undefined}};
|
|
|
|
|
|
|
|
|
|
%% 处理接受到的消息
|
|
|
|
|
handle_info({gun_ws, ConnPid, {binary, Bin}}, State = #state{conn_pid = ConnPid}) ->
|
|
|
|
|
lager:debug("[efka_agent] get binary message: ~p", [Bin]),
|
|
|
|
|
{noreply, State};
|
|
|
|
|
|
|
|
|
|
handle_info({gun_ws, ConnPid, {text, Msg}}, State = #state{conn_pid = ConnPid}) ->
|
|
|
|
|
lager:debug("[efka_agent] get text message: ~p", [Msg]),
|
|
|
|
|
{noreply, State};
|
|
|
|
|
|
|
|
|
|
%% websocket链接关闭
|
|
|
|
|
handle_info({gun_ws, ConnPid, close}, State = #state{conn_pid = ConnPid}) ->
|
|
|
|
|
lager:debug("[efka_agent] ws close"),
|
|
|
|
|
handle_info({ssl_error, Socket, Reason}, State = #state{socket = Socket}) ->
|
|
|
|
|
lager:warning("[efka_agent] socket close with reason: ~p", [Reason]),
|
|
|
|
|
retry_connect(),
|
|
|
|
|
{noreply, State#state{conn_pid = undefined}};
|
|
|
|
|
{noreply, State#state{socket = undefined}};
|
|
|
|
|
handle_info({ssl_closed, Socket}, State = #state{socket = Socket}) ->
|
|
|
|
|
lager:warning("[efka_agent] socket closed"),
|
|
|
|
|
retry_connect(),
|
|
|
|
|
{noreply, State#state{socket = undefined}};
|
|
|
|
|
|
|
|
|
|
handle_info(_Info, State = #state{}) ->
|
|
|
|
|
{noreply, State}.
|
|
|
|
|
@ -131,14 +114,14 @@ handle_info(_Info, State = #state{}) ->
|
|
|
|
|
%% necessary cleaning up. When it returns, the gen_server terminates
|
|
|
|
|
%% with Reason. The return value is ignored.
|
|
|
|
|
-spec(terminate(Reason :: (normal | shutdown | {shutdown, term()} | term()),
|
|
|
|
|
State :: #state{}) -> term()).
|
|
|
|
|
State :: #state{}) -> term()).
|
|
|
|
|
terminate(_Reason, _State = #state{}) ->
|
|
|
|
|
ok.
|
|
|
|
|
|
|
|
|
|
%% @private
|
|
|
|
|
%% @doc Convert process state when code is changed
|
|
|
|
|
-spec(code_change(OldVsn :: term() | {down, term()}, State :: #state{},
|
|
|
|
|
Extra :: term()) ->
|
|
|
|
|
Extra :: term()) ->
|
|
|
|
|
{ok, NewState :: #state{}} | {error, Reason :: term()}).
|
|
|
|
|
code_change(_OldVsn, State = #state{}, _Extra) ->
|
|
|
|
|
{ok, State}.
|
|
|
|
|
@ -150,28 +133,9 @@ code_change(_OldVsn, State = #state{}, _Extra) ->
|
|
|
|
|
retry_connect() ->
|
|
|
|
|
erlang:start_timer(5000, self(), create_connection).
|
|
|
|
|
|
|
|
|
|
-spec connect(Host :: string(), Port :: integer()) -> {ok, ConnPid :: pid()} | {error, Reason :: any()}.
|
|
|
|
|
connect(Host, Port) when is_list(Host), is_integer(Port) ->
|
|
|
|
|
Opts = #{
|
|
|
|
|
protocols => [http],
|
|
|
|
|
transport => tcp
|
|
|
|
|
},
|
|
|
|
|
case gun:open(Host, Port, Opts) of
|
|
|
|
|
{ok, ConnPid} ->
|
|
|
|
|
%% 等待连接建立
|
|
|
|
|
receive
|
|
|
|
|
{gun_up, ConnPid, http} ->
|
|
|
|
|
%% 发起 WebSocket 握手请求
|
|
|
|
|
gun:ws_upgrade(ConnPid, "/", [
|
|
|
|
|
{<<"connection">>, <<"upgrade">>},
|
|
|
|
|
{<<"upgrade">>, <<"websocket">>},
|
|
|
|
|
{<<"sec-websocket-version">>, <<"13">>},
|
|
|
|
|
{<<"sec-websocket-key">>, base64:encode(crypto:strong_rand_bytes(16))}
|
|
|
|
|
]),
|
|
|
|
|
{ok, ConnPid}
|
|
|
|
|
after 5000 ->
|
|
|
|
|
{error, connect_timeout}
|
|
|
|
|
end;
|
|
|
|
|
{error, Reason} ->
|
|
|
|
|
{error, Reason}
|
|
|
|
|
end.
|
|
|
|
|
SslOptions = [
|
|
|
|
|
{verify, verify_none},
|
|
|
|
|
{active, true}
|
|
|
|
|
],
|
|
|
|
|
ssl:connect(Host, Port, SslOptions, 5000).
|