diff --git a/src/host/iot_host.erl b/src/host/iot_host.erl index 8f5f025..8db63dc 100644 --- a/src/host/iot_host.erl +++ b/src/host/iot_host.erl @@ -126,11 +126,11 @@ remove_container(Pid, ContainerName) when is_pid(Pid), is_binary(ContainerName) container_call(Pid, docker_container_builder:remove_request(ContainerName)). -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) -> receive - {request_reply, Ref, {ok, ResultBin}} when is_binary(ResultBin) -> - {ok, ResultBin}; + {request_reply, Ref, {ok, Result}} -> + {ok, Result}; {request_reply, Ref, {error, Code, Reason}} when is_integer(Code), is_binary(Reason) -> {error, Code, Reason} after Timeout -> diff --git a/src/transport/http/container_handler.erl b/src/transport/http/container_handler.erl index b051c17..c982f9e 100644 --- a/src/transport/http/container_handler.erl +++ b/src/transport/http/container_handler.erl @@ -164,7 +164,9 @@ request_success_response(Result) when is_binary(Result) -> iot_util:json_data(Data); error -> 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) -> case decode_json_bytes(Reason) of diff --git a/src/transport/tcp/ssl_channel.erl b/src/transport/tcp/ssl_channel.erl index 23ba4db..b616908 100644 --- a/src/transport/tcp/ssl_channel.erl +++ b/src/transport/tcp/ssl_channel.erl @@ -215,21 +215,21 @@ handle_request_frame(PacketId, case iot_host:attach_channel(HostPid, self()) of ok -> 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}}; {denied, Reason} when is_binary(Reason) -> 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]), {noreply, State#state{uuid = UUID, host_pid = HostPid}}; {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]), {stop, Reason, State} end end; {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]), {stop, Reason, State} end; @@ -284,13 +284,15 @@ send_reply_frame(Transport, Socket, PacketId, Reply) -> Packet = term_to_binary({response, PacketId, Reply}), Transport:send(Socket, Packet). --spec decode_reply({ok, binary()} | {error, integer(), binary()}) -> - {ok, binary()} | {error, integer(), binary()} | undefined. -decode_reply({ok, ResultBin}) -> - {ok, ResultBin}; -decode_reply({error, Code, Message}) -> +-spec decode_reply({container_response, {ok, term()} | {error, integer(), binary()}} | tuple()) -> + {ok, term()} | {error, integer(), binary()} | undefined. +decode_reply({container_response, {ok, Result}}) -> + {ok, Result}; +decode_reply({container_response, {error, Code, Message}}) -> {error, Code, Message}; decode_reply(undefined) -> + undefined; +decode_reply(_Reply) -> undefined. %% 检测token是否是合法值