fix ssl_channel

This commit is contained in:
anlicheng 2026-05-09 12:53:47 +08:00
parent 4c9b309b19
commit 8ee8d54c26

View File

@ -13,7 +13,7 @@
-define(INFLIGHT_TIMEOUT, 60000). -define(INFLIGHT_TIMEOUT, 60000).
%% API %% API
-export([pub/4, container_call/4, cancel_command_call/2, activate/4]). -export([pub/4, container_call/4, cancel_command_call/2]).
-export([start_link/3, stop/2]). -export([start_link/3, stop/2]).
%% gen_server callbacks %% gen_server callbacks
@ -41,12 +41,6 @@
pub(Pid, Topic, Qos, Content) when is_pid(Pid), is_binary(Topic), is_integer(Qos), is_binary(Content) -> pub(Pid, Topic, Qos, Content) when is_pid(Pid), is_binary(Topic), is_integer(Qos), is_binary(Content) ->
gen_server:cast(Pid, {pub, Topic, Qos, Content}). gen_server:cast(Pid, {pub, Topic, Qos, Content}).
-spec activate(Pid :: pid(), ReceiverPid :: pid(), Ref :: reference(), Auth :: boolean()) -> ok.
activate(Pid, ReceiverPid, Ref, Auth) when is_pid(Pid), is_pid(ReceiverPid), is_reference(Ref), is_boolean(Auth) ->
Command = case Auth of true -> activate; false -> deactivate end,
gen_server:cast(Pid, {command_call, ReceiverPid, Ref, {auth, Command}}),
ok.
-spec container_call(Pid :: pid(), ReceiverPid :: pid(), Ref :: reference(), Request :: map()) -> ok. -spec container_call(Pid :: pid(), ReceiverPid :: pid(), Ref :: reference(), Request :: map()) -> ok.
container_call(Pid, ReceiverPid, Ref, Request) when is_pid(Pid), is_pid(ReceiverPid), is_reference(Ref), is_map(Request) -> container_call(Pid, ReceiverPid, Ref, Request) when is_pid(Pid), is_pid(ReceiverPid), is_reference(Ref), is_map(Request) ->
gen_server:cast(Pid, {command_call, ReceiverPid, Ref, {container, Request}}), gen_server:cast(Pid, {command_call, ReceiverPid, Ref, {container, Request}}),
@ -75,7 +69,6 @@ init(Ref, Transport, _Opts = []) ->
{ok, Socket} = ranch:handshake(Ref), {ok, Socket} = ranch:handshake(Ref),
logger:debug("[ssl_channel] get a new connection: ~p", [Socket]), logger:debug("[ssl_channel] get a new connection: ~p", [Socket]),
Transport:setopts(Socket, [binary, {active, true}, {packet, 4}]), Transport:setopts(Socket, [binary, {active, true}, {packet, 4}]),
% erlang:start_timer(?PING_TICKER, self(), ping_ticker),
gen_server:enter_loop(?MODULE, [], #state{transport = Transport, socket = Socket}). gen_server:enter_loop(?MODULE, [], #state{transport = Transport, socket = Socket}).
handle_call({cancel_command_call, Ref}, _From, State = #state{inflight = Inflight}) -> handle_call({cancel_command_call, Ref}, _From, State = #state{inflight = Inflight}) ->
@ -187,11 +180,6 @@ handle_request_frame(Ref, {auth_request, #{uuid := UUID, token := Token, timesta
erlang:monitor(process, HostPid), erlang:monitor(process, HostPid),
send_reply_frame(Transport, Socket, Ref, {auth_response, ok}), send_reply_frame(Transport, Socket, Ref, {auth_response, ok}),
{noreply, State#state{uuid = UUID, host_pid = HostPid}}; {noreply, State#state{uuid = UUID, host_pid = HostPid}};
{denied, Reason} when is_binary(Reason) ->
erlang:monitor(process, HostPid),
send_reply_frame(Transport, Socket, Ref, {auth_response, {error, {denied, Reason}}}),
logger:debug("[ws_channel] uuid: ~p, attach channel get error: ~p, stop channel", [UUID, Reason]),
{noreply, State#state{uuid = UUID, host_pid = HostPid}};
{error, Reason} when is_binary(Reason) -> {error, Reason} when is_binary(Reason) ->
send_reply_frame(Transport, Socket, Ref, {auth_response, {error, {failed, Reason}}}), send_reply_frame(Transport, Socket, Ref, {auth_response, {error, {failed, Reason}}}),
logger:debug("[ws_channel] uuid: ~p, attach channel get error: ~p, stop channel", [UUID, Reason]), logger:debug("[ws_channel] uuid: ~p, attach channel get error: ~p, stop channel", [UUID, Reason]),
@ -257,10 +245,6 @@ decode_command_response({container, {ok, Result}}) ->
{ok, Result}; {ok, Result};
decode_command_response({container, {error, Reason}}) -> decode_command_response({container, {error, Reason}}) ->
{error, Reason}; {error, Reason};
decode_command_response({auth, ok}) ->
ok;
decode_command_response({auth, {error, Reason}}) ->
{error, Reason};
decode_command_response(_Reply) -> decode_command_response(_Reply) ->
{error, invalid_response}. {error, invalid_response}.