This commit is contained in:
anlicheng 2026-04-26 16:08:47 +08:00
parent c17750a5d9
commit 811029ddb5
3 changed files with 17 additions and 13 deletions

View File

@ -126,11 +126,11 @@ remove_container(Pid, ContainerName) when is_pid(Pid), is_binary(ContainerName)
container_call(Pid, docker_container_builder:remove_request(ContainerName)). container_call(Pid, docker_container_builder:remove_request(ContainerName)).
-spec await_reply(Pid :: pid(), Ref :: reference(), Timeout :: integer()) -> -spec await_reply(Pid :: pid(), Ref :: reference(), Timeout :: integer()) ->
{ok, Result :: binary()} | {error, Code :: integer(), Reason :: binary()}. {ok, Result :: term()} | {error, Code :: integer(), Reason :: binary()}.
await_reply(Pid, Ref, Timeout) when is_pid(Pid), is_reference(Ref), is_integer(Timeout) -> await_reply(Pid, Ref, Timeout) when is_pid(Pid), is_reference(Ref), is_integer(Timeout) ->
receive receive
{request_reply, Ref, {ok, ResultBin}} when is_binary(ResultBin) -> {request_reply, Ref, {ok, Result}} ->
{ok, ResultBin}; {ok, Result};
{request_reply, Ref, {error, Code, Reason}} when is_integer(Code), is_binary(Reason) -> {request_reply, Ref, {error, Code, Reason}} when is_integer(Code), is_binary(Reason) ->
{error, Code, Reason} {error, Code, Reason}
after Timeout -> after Timeout ->

View File

@ -164,7 +164,9 @@ request_success_response(Result) when is_binary(Result) ->
iot_util:json_data(Data); iot_util:json_data(Data);
error -> error ->
iot_util:json_data(Result) iot_util:json_data(Result)
end. end;
request_success_response(Result) ->
iot_util:json_data(Result).
request_error_response(Code, Reason) when is_integer(Code), is_binary(Reason) -> request_error_response(Code, Reason) when is_integer(Code), is_binary(Reason) ->
case decode_json_bytes(Reason) of case decode_json_bytes(Reason) of

View File

@ -215,21 +215,21 @@ handle_request_frame(PacketId,
case iot_host:attach_channel(HostPid, self()) of case iot_host:attach_channel(HostPid, self()) of
ok -> ok ->
erlang:monitor(process, HostPid), erlang:monitor(process, HostPid),
send_reply_frame(Transport, Socket, PacketId, {ok, <<"ok">>}), send_reply_frame(Transport, Socket, PacketId, {auth_response, {ok, <<"ok">>}}),
{noreply, State#state{uuid = UUID, host_pid = HostPid}}; {noreply, State#state{uuid = UUID, host_pid = HostPid}};
{denied, Reason} when is_binary(Reason) -> {denied, Reason} when is_binary(Reason) ->
erlang:monitor(process, HostPid), erlang:monitor(process, HostPid),
send_reply_frame(Transport, Socket, PacketId, {error, 1, Reason}), send_reply_frame(Transport, Socket, PacketId, {auth_response, {error, 1, 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]),
{noreply, State#state{uuid = UUID, host_pid = HostPid}}; {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, PacketId, {error, 2, Reason}), send_reply_frame(Transport, Socket, PacketId, {auth_response, {error, 2, 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]),
{stop, Reason, State} {stop, Reason, State}
end end
end; end;
{error, Reason} -> {error, Reason} ->
send_reply_frame(Transport, Socket, PacketId, {error, 2, Reason}), send_reply_frame(Transport, Socket, PacketId, {auth_response, {error, 2, Reason}}),
logger:warning("[ws_channel] uuid: ~p, token: ~p, auth failed, reason: ~p", [UUID, Token, Reason]), logger:warning("[ws_channel] uuid: ~p, token: ~p, auth failed, reason: ~p", [UUID, Token, Reason]),
{stop, Reason, State} {stop, Reason, State}
end; end;
@ -284,13 +284,15 @@ send_reply_frame(Transport, Socket, PacketId, Reply) ->
Packet = term_to_binary({response, PacketId, Reply}), Packet = term_to_binary({response, PacketId, Reply}),
Transport:send(Socket, Packet). Transport:send(Socket, Packet).
-spec decode_reply({ok, binary()} | {error, integer(), binary()}) -> -spec decode_reply({container_response, {ok, term()} | {error, integer(), binary()}} | tuple()) ->
{ok, binary()} | {error, integer(), binary()} | undefined. {ok, term()} | {error, integer(), binary()} | undefined.
decode_reply({ok, ResultBin}) -> decode_reply({container_response, {ok, Result}}) ->
{ok, ResultBin}; {ok, Result};
decode_reply({error, Code, Message}) -> decode_reply({container_response, {error, Code, Message}}) ->
{error, Code, Message}; {error, Code, Message};
decode_reply(undefined) -> decode_reply(undefined) ->
undefined;
decode_reply(_Reply) ->
undefined. undefined.
%% token是否是合法值 %% token是否是合法值