fix ws command

This commit is contained in:
anlicheng 2023-12-22 15:19:21 +08:00
parent 2da8a40ac2
commit 5f28301ed1

View File

@ -14,6 +14,7 @@
-export([init/2]).
-export([websocket_init/1, websocket_handle/2, websocket_info/2, terminate/3]).
-export([publish/3, stop/2, send/2]).
-export([publish_command/3, send_command/2]).
-record(state, {
uuid :: undefined | binary(),
@ -23,7 +24,9 @@
packet_id = 1 :: integer(),
%%
inflight = #{}
inflight = #{},
%%
cmd_inflight = #{}
}).
%%
@ -38,6 +41,20 @@ publish(Pid, ReceiverPid, Msg) when is_pid(Pid), is_binary(Msg) ->
send(Pid, Msg) when is_pid(Pid), is_binary(Msg) ->
Pid ! {send, Msg}.
%%
%%
-spec publish_command(Pid :: pid(), ReceiverPid :: pid(), Msg :: binary()) -> Ref :: reference().
publish_command(Pid, ReceiverPid, Cmd) when is_pid(Pid), is_binary(Cmd) ->
Ref = make_ref(),
Pid ! {publish_command, ReceiverPid, Ref, Cmd},
Ref.
%%
-spec send_command(Pid :: pid(), Cmd :: binary()) -> no_return().
send_command(Pid, Cmd) when is_pid(Pid), is_binary(Cmd) ->
Pid ! {send_command, Cmd}.
%%
-spec stop(Pid :: pid(), Reason :: any()) -> no_return().
stop(undefined, _Reason) ->
@ -140,6 +157,25 @@ websocket_handle({binary, <<?PACKET_PUBLISH_RESPONSE, PacketId:32, Body/binary>>
{ok, State#state{inflight = NInflight}}
end;
%%
websocket_handle({binary, <<?PACKET_COMMAND_RESPONSE, PacketId:32, Body/binary>>}, State = #state{uuid = UUID, cmd_inflight = CmdInflight}) when PacketId > 0 ->
lager:debug("[ws_channel] uuid: ~p, get command response message: ~p, packet_id: ~p", [UUID, Body, PacketId]),
case maps:take(PacketId, CmdInflight) of
error ->
lager:warning("[ws_channel] get unknown command response message: ~p, packet_id: ~p", [Body, PacketId]),
{ok, State};
{{ReceiverPid, Ref}, NCmdInflight} ->
case is_pid(ReceiverPid) andalso is_process_alive(ReceiverPid) of
true when Body == <<>> ->
ReceiverPid ! {cmd_response, Ref};
true ->
ReceiverPid ! {cmd_response, Ref, Body};
false ->
lager:warning("[ws_channel] get command response message: ~p, packet_id: ~p, but receiver_pid is deaded", [Body, PacketId])
end,
{ok, State#state{cmd_inflight = NCmdInflight}}
end;
websocket_handle(Info, State) ->
lager:error("[ws_channel] get a unknown message: ~p, channel will closed", [Info]),
{stop, State}.
@ -158,6 +194,15 @@ websocket_info({publish, ReceiverPid, Ref, Msg}, State = #state{packet_id = Pack
websocket_info({send, Msg}, State) when is_binary(Msg) ->
{reply, {binary, <<?PACKET_PUBLISH, 0:32, Msg/binary>>}, State};
%%
websocket_info({publish_command, ReceiverPid, Ref, Cmd}, State = #state{packet_id = PacketId, cmd_inflight = CmdInflight}) when is_binary(Cmd) ->
NCmdInflight = maps:put(PacketId, {ReceiverPid, Ref}, CmdInflight),
{reply, {binary, <<?PACKET_COMMAND, PacketId:32, Cmd/binary>>}, State#state{packet_id = PacketId + 1, cmd_inflight = NCmdInflight}};
%% ,
websocket_info({send_command, Cmd}, State) when is_binary(Cmd) ->
{reply, {binary, <<?PACKET_COMMAND, 0:32, Cmd/binary>>}, State};
%%
websocket_info({'DOWN', _, process, HostPid, Reason}, State = #state{uuid = UUID, host_pid = HostPid}) ->
lager:debug("[ws_channel] uuid: ~p, channel will close because host exited with reason: ~p", [UUID, Reason]),