This commit is contained in:
anlicheng 2026-04-27 15:37:58 +08:00
parent 94d20f88f4
commit 84e1e29c7f

View File

@ -109,10 +109,11 @@ handle_cast({command, Command}, State = #state{transport = Transport, socket = S
%%
handle_cast({request_call, ReceiverPid, Ref, Body}, State = #state{transport = Transport, socket = Socket, inflight = Inflight}) ->
Packet = term_to_binary({request, Ref, Body}),
TimerRef = erlang:start_timer(?INFLIGHT_TIMEOUT, self(), {request_timeout, Ref}),
Transport:send(Socket, Packet),
TimerRef = erlang:start_timer(?INFLIGHT_TIMEOUT, self(), {request_timeout, Ref}),
RequestInfo = #inflight_request{receiver_pid = ReceiverPid, timer_ref = TimerRef},
{noreply, State#state{inflight = maps:put(Ref, RequestInfo, Inflight)}}.
handle_info({timeout, TimerRef, {request_timeout, Ref}}, State = #state{inflight = Inflight}) ->
@ -133,21 +134,20 @@ handle_info({'DOWN', _, process, HostPid, Reason}, State = #state{uuid = UUID, h
logger:debug("[ws_channel] uuid: ~p, channel will close because host exited with reason: ~p", [UUID, Reason]),
{stop, Reason, State};
handle_info({ssl, Socket, PacketBin}, State = #state{transport = Transport, socket = Socket, host_pid = HostPid, inflight = Inflight})
when is_binary(PacketBin) ->
case catch binary_to_term(PacketBin, [safe]) of
handle_info({ssl, Socket, PacketBin}, State = #state{transport = Transport, socket = Socket, host_pid = HostPid, inflight = Inflight}) when is_binary(PacketBin) ->
try binary_to_term(PacketBin, [safe]) of
{request, Ref, Body} ->
handle_request_frame(Ref, Body, Transport, Socket, State);
{message, Body} ->
handle_message_frame(Body, HostPid, State);
{response, Ref, Response} ->
handle_response_frame(Ref, Response, Inflight, State);
{'EXIT', Reason} ->
logger:warning("[ssl_channel] invalid packet: ~p", [Reason]),
{stop, bad_packet, State};
Other ->
logger:warning("[ssl_channel] unsupported packet: ~p", [Other]),
{stop, bad_packet, State}
catch error:Error ->
logger:warning("[ssl_channel] binary_to_term get error: ~p", [Error]),
{stop, bad_packet, State}
end;
handle_info({ssl_closed, Socket}, State = #state{socket = Socket}) ->
@ -179,8 +179,7 @@ code_change(_OldVsn, State, _Extra) ->
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-spec handle_request_frame(reference(), tuple(), module(), any(), #state{}) -> {noreply, #state{}} | {stop, term(), #state{}}.
handle_request_frame(Ref,
{auth_request, #{uuid := UUID, token := Token, timestamp := Timestamp}},
handle_request_frame(Ref, {auth_request, #{uuid := UUID, token := Token, timestamp := Timestamp}},
Transport, Socket, State) ->
logger:debug("[ws_channel] auth uuid: ~p", [UUID]),