From 71babf4f469859882f0efde8da8dbb9b58be5842 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Fri, 9 May 2025 22:36:25 +0800 Subject: [PATCH] fix --- apps/iot/include/iot.hrl | 4 +- apps/iot/include/message_pb.hrl | 6 +- apps/iot/src/iot_host.erl | 35 +++++--- apps/iot/src/proto/message_pb.erl | 142 +++++++++++++++--------------- apps/iot/src/tcp/tcp_channel.erl | 24 ++--- 5 files changed, 110 insertions(+), 101 deletions(-) diff --git a/apps/iot/include/iot.hrl b/apps/iot/include/iot.hrl index f0e882d..377630d 100644 --- a/apps/iot/include/iot.hrl +++ b/apps/iot/include/iot.hrl @@ -34,8 +34,8 @@ -define(PACKET_COMMAND, 16#04). %% 服务器端推送消息 --define(PACKET_PUSH, 16#05). --define(PACKET_PUSH_REPLY, 16#06). +-define(PACKET_ASYNC_CALL, 16#05). +-define(PACKET_ASYNC_CALL_REPLY, 16#06). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%% 二级分类定义 diff --git a/apps/iot/include/message_pb.hrl b/apps/iot/include/message_pb.hrl index 0032f5d..450e83e 100644 --- a/apps/iot/include/message_pb.hrl +++ b/apps/iot/include/message_pb.hrl @@ -35,9 +35,9 @@ }). -endif. --ifndef('PUSH_REPLY_PB_H'). --define('PUSH_REPLY_PB_H', true). --record(push_reply, +-ifndef('ASYNC_CALL_REPLY_PB_H'). +-define('ASYNC_CALL_REPLY_PB_H', true). +-record(async_call_reply, {code = 0 :: non_neg_integer() | undefined, % = 1, optional, 32 bits result = <<>> :: unicode:chardata() | undefined, % = 2, optional message = <<>> :: unicode:chardata() | undefined % = 3, optional diff --git a/apps/iot/src/iot_host.erl b/apps/iot/src/iot_host.erl index 1f18c73..ad72039 100644 --- a/apps/iot/src/iot_host.erl +++ b/apps/iot/src/iot_host.erl @@ -24,7 +24,8 @@ -export([start_link/2, get_name/1, get_alias_name/1, get_pid/1, handle/2, activate/2]). -export([get_metric/1, get_status/1]). %% 通讯相关 --export([pub/3, async_request/3, attach_channel/2, command/3, push_config/2]). +-export([pub/3, attach_channel/2, command/3]). +-export([async_deploy/4, async_invoke/4, async_service_config/2, async_task_log/2]). %% 设备管理 -export([reload_device/2, delete_device/2, activate_device/3]). -export([heartbeat/1]). @@ -88,14 +89,24 @@ get_metric(Pid) when is_pid(Pid) -> attach_channel(Pid, ChannelPid) when is_pid(Pid), is_pid(ChannelPid) -> gen_statem:call(Pid, {attach_channel, ChannelPid}). --spec push_config(Pid :: pid(), ConfigJson :: binary()) -> {ok, Ref :: reference()} | {error, Reason :: any()}. -push_config(Pid, ConfigJson) when is_pid(Pid), is_binary(ConfigJson) -> - async_request(Pid, <<"$sys:push_config">>, ConfigJson). +-spec async_service_config(Pid :: pid(), ConfigJson :: binary()) -> {ok, Ref :: reference()} | {error, Reason :: any()}. +async_service_config(Pid, ConfigJson) when is_pid(Pid), is_binary(ConfigJson) -> + gen_statem:call(Pid, {async_call, self(), ?PUSH_SERVICE_CONFIG, ConfigJson}). -%% 响应的消息格式为: {async_response, Ref, #async_response{}} --spec async_request(Pid :: pid(), Method :: binary(), Params :: binary()) -> {ok, Ref :: reference()} | {error, Reason :: any()}. -async_request(Pid, Method, Params) when is_pid(Pid), is_binary(Method), is_binary(Params) -> - gen_statem:call(Pid, {async_request, self(), Method, Params}). +-spec async_deploy(Pid :: pid(), TaskId :: integer(), ServiceId :: binary(), TarUrl :: binary()) -> {ok, Ref :: reference()} | {error, Reason :: any()}. +async_deploy(Pid, TaskId, ServiceId, TarUrl) when is_pid(Pid), is_integer(TaskId), is_binary(ServiceId), is_binary(TarUrl) -> + PushBin = message_pb:encode_msg(#deploy{task_id = TaskId, service_id = ServiceId, tar_url = TarUrl}), + gen_statem:call(Pid, {async_call, self(), ?PUSH_DEPLOY, PushBin}). + +-spec async_invoke(Pid :: pid(), ServiceId :: integer(), Payload :: binary(), Timeout :: integer()) -> {ok, Ref :: reference()} | {error, Reason :: any()}. +async_invoke(Pid, ServiceId, Payload, Timeout) when is_pid(Pid), is_binary(ServiceId), is_binary(Payload), is_integer(Timeout) -> + InvokeBin = message_pb:encode_msg(#invoke{service_id = ServiceId, payload = Payload, timeout = Timeout}), + gen_statem:call(Pid, {async_call, self(), ?PUSH_INVOKE, InvokeBin}). + +-spec async_task_log(Pid :: pid(), TaskId :: integer()) -> {ok, Ref :: reference()} | {error, Reason :: any()}. +async_task_log(Pid, TaskId) when is_pid(Pid), is_integer(TaskId) -> + TaskLogBin = message_pb:encode_msg(#fetch_task_log{task_id = TaskId}), + gen_statem:call(Pid, {async_call, self(), ?PUSH_TASK_LOG, TaskLogBin}). -spec pub(Pid :: pid(), Topic :: binary(), Content :: binary()) -> ok | {error, Reason :: any()}. pub(Pid, Topic, Content) when is_pid(Pid), is_binary(Topic), is_binary(Content) -> @@ -196,16 +207,14 @@ handle_event({call, From}, get_status, _, State = #state{channel_pid = ChannelPi {keep_state, State, [{reply, From, {ok, Reply}}]}; %% 只要channel存在,就负责将消息推送到边缘端主机 -handle_event({call, From}, {async_request, ReceiverPid, Method, Params}, _, State = #state{uuid = UUID, channel_pid = ChannelPid, has_session = HasSession}) -> +handle_event({call, From}, {async_call, ReceiverPid, PushType, PushBin}, _, State = #state{uuid = UUID, channel_pid = ChannelPid, has_session = HasSession}) -> case HasSession andalso is_pid(ChannelPid) of true -> %% 通过websocket发送请求 - lager:debug("[iot_host] host: ~p, will async_request method: ~p, params: ~p", [UUID, Method, Params]), - Ref = tcp_channel:push(ChannelPid, ReceiverPid, Method, Params), - + Ref = tcp_channel:async_call(ChannelPid, ReceiverPid, PushType, PushBin), {keep_state, State, [{reply, From, {ok, Ref}}]}; false -> - lager:debug("[iot_host] uuid: ~p, publish_message invalid state: ~p", [UUID, state_map(State)]), + lager:debug("[iot_host] uuid: ~p, publish_type: ~p, invalid state: ~p", [UUID, PushType, state_map(State)]), {keep_state, State, [{reply, From, {error, <<"主机离线,发送请求失败"/utf8>>}}]} end; diff --git a/apps/iot/src/proto/message_pb.erl b/apps/iot/src/proto/message_pb.erl index 056408d..44785e8 100644 --- a/apps/iot/src/proto/message_pb.erl +++ b/apps/iot/src/proto/message_pb.erl @@ -61,7 +61,7 @@ -type pub() :: #pub{}. --type push_reply() :: #push_reply{}. +-type async_call_reply() :: #async_call_reply{}. -type deploy() :: #deploy{}. @@ -79,9 +79,9 @@ -type event() :: #event{}. --export_type(['auth_request'/0, 'auth_reply'/0, 'pub'/0, 'push_reply'/0, 'deploy'/0, 'fetch_task_log'/0, 'invoke'/0, 'service_config'/0, 'data'/0, 'ping'/0, 'service_inform'/0, 'event'/0]). --type '$msg_name'() :: auth_request | auth_reply | pub | push_reply | deploy | fetch_task_log | invoke | service_config | data | ping | service_inform | event. --type '$msg'() :: auth_request() | auth_reply() | pub() | push_reply() | deploy() | fetch_task_log() | invoke() | service_config() | data() | ping() | service_inform() | event(). +-export_type(['auth_request'/0, 'auth_reply'/0, 'pub'/0, 'async_call_reply'/0, 'deploy'/0, 'fetch_task_log'/0, 'invoke'/0, 'service_config'/0, 'data'/0, 'ping'/0, 'service_inform'/0, 'event'/0]). +-type '$msg_name'() :: auth_request | auth_reply | pub | async_call_reply | deploy | fetch_task_log | invoke | service_config | data | ping | service_inform | event. +-type '$msg'() :: auth_request() | auth_reply() | pub() | async_call_reply() | deploy() | fetch_task_log() | invoke() | service_config() | data() | ping() | service_inform() | event(). -export_type(['$msg_name'/0, '$msg'/0]). -if(?OTP_RELEASE >= 24). @@ -111,7 +111,7 @@ encode_msg(Msg, MsgName, Opts) -> auth_request -> encode_msg_auth_request(id(Msg, TrUserData), TrUserData); auth_reply -> encode_msg_auth_reply(id(Msg, TrUserData), TrUserData); pub -> encode_msg_pub(id(Msg, TrUserData), TrUserData); - push_reply -> encode_msg_push_reply(id(Msg, TrUserData), TrUserData); + async_call_reply -> encode_msg_async_call_reply(id(Msg, TrUserData), TrUserData); deploy -> encode_msg_deploy(id(Msg, TrUserData), TrUserData); fetch_task_log -> encode_msg_fetch_task_log(id(Msg, TrUserData), TrUserData); invoke -> encode_msg_invoke(id(Msg, TrUserData), TrUserData); @@ -226,10 +226,10 @@ encode_msg_pub(#pub{topic = F1, content = F2}, Bin, TrUserData) -> end end. -encode_msg_push_reply(Msg, TrUserData) -> encode_msg_push_reply(Msg, <<>>, TrUserData). +encode_msg_async_call_reply(Msg, TrUserData) -> encode_msg_async_call_reply(Msg, <<>>, TrUserData). -encode_msg_push_reply(#push_reply{code = F1, result = F2, message = F3}, Bin, TrUserData) -> +encode_msg_async_call_reply(#async_call_reply{code = F1, result = F2, message = F3}, Bin, TrUserData) -> B1 = if F1 == undefined -> Bin; true -> begin @@ -778,7 +778,7 @@ decode_msg_1_catch(Bin, MsgName, TrUserData) -> decode_msg_2_doit(auth_request, Bin, TrUserData) -> id(decode_msg_auth_request(Bin, TrUserData), TrUserData); decode_msg_2_doit(auth_reply, Bin, TrUserData) -> id(decode_msg_auth_reply(Bin, TrUserData), TrUserData); decode_msg_2_doit(pub, Bin, TrUserData) -> id(decode_msg_pub(Bin, TrUserData), TrUserData); -decode_msg_2_doit(push_reply, Bin, TrUserData) -> id(decode_msg_push_reply(Bin, TrUserData), TrUserData); +decode_msg_2_doit(async_call_reply, Bin, TrUserData) -> id(decode_msg_async_call_reply(Bin, TrUserData), TrUserData); decode_msg_2_doit(deploy, Bin, TrUserData) -> id(decode_msg_deploy(Bin, TrUserData), TrUserData); decode_msg_2_doit(fetch_task_log, Bin, TrUserData) -> id(decode_msg_fetch_task_log(Bin, TrUserData), TrUserData); decode_msg_2_doit(invoke, Bin, TrUserData) -> id(decode_msg_invoke(Bin, TrUserData), TrUserData); @@ -964,63 +964,63 @@ skip_32_pub(<<_:32, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, TrUserData) -> dfp_rea skip_64_pub(<<_:64, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, TrUserData) -> dfp_read_field_def_pub(Rest, Z1, Z2, F, F@_1, F@_2, TrUserData). -decode_msg_push_reply(Bin, TrUserData) -> dfp_read_field_def_push_reply(Bin, 0, 0, 0, id(0, TrUserData), id(<<>>, TrUserData), id(<<>>, TrUserData), TrUserData). +decode_msg_async_call_reply(Bin, TrUserData) -> dfp_read_field_def_async_call_reply(Bin, 0, 0, 0, id(0, TrUserData), id(<<>>, TrUserData), id(<<>>, TrUserData), TrUserData). -dfp_read_field_def_push_reply(<<8, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData) -> d_field_push_reply_code(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData); -dfp_read_field_def_push_reply(<<18, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData) -> d_field_push_reply_result(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData); -dfp_read_field_def_push_reply(<<26, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData) -> d_field_push_reply_message(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData); -dfp_read_field_def_push_reply(<<>>, 0, 0, _, F@_1, F@_2, F@_3, _) -> #push_reply{code = F@_1, result = F@_2, message = F@_3}; -dfp_read_field_def_push_reply(Other, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData) -> dg_read_field_def_push_reply(Other, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData). +dfp_read_field_def_async_call_reply(<<8, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData) -> d_field_async_call_reply_code(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData); +dfp_read_field_def_async_call_reply(<<18, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData) -> d_field_async_call_reply_result(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData); +dfp_read_field_def_async_call_reply(<<26, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData) -> d_field_async_call_reply_message(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData); +dfp_read_field_def_async_call_reply(<<>>, 0, 0, _, F@_1, F@_2, F@_3, _) -> #async_call_reply{code = F@_1, result = F@_2, message = F@_3}; +dfp_read_field_def_async_call_reply(Other, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData) -> dg_read_field_def_async_call_reply(Other, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData). -dg_read_field_def_push_reply(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, F@_3, TrUserData) when N < 32 - 7 -> dg_read_field_def_push_reply(Rest, N + 7, X bsl N + Acc, F, F@_1, F@_2, F@_3, TrUserData); -dg_read_field_def_push_reply(<<0:1, X:7, Rest/binary>>, N, Acc, _, F@_1, F@_2, F@_3, TrUserData) -> +dg_read_field_def_async_call_reply(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, F@_3, TrUserData) when N < 32 - 7 -> dg_read_field_def_async_call_reply(Rest, N + 7, X bsl N + Acc, F, F@_1, F@_2, F@_3, TrUserData); +dg_read_field_def_async_call_reply(<<0:1, X:7, Rest/binary>>, N, Acc, _, F@_1, F@_2, F@_3, TrUserData) -> Key = X bsl N + Acc, case Key of - 8 -> d_field_push_reply_code(Rest, 0, 0, 0, F@_1, F@_2, F@_3, TrUserData); - 18 -> d_field_push_reply_result(Rest, 0, 0, 0, F@_1, F@_2, F@_3, TrUserData); - 26 -> d_field_push_reply_message(Rest, 0, 0, 0, F@_1, F@_2, F@_3, TrUserData); + 8 -> d_field_async_call_reply_code(Rest, 0, 0, 0, F@_1, F@_2, F@_3, TrUserData); + 18 -> d_field_async_call_reply_result(Rest, 0, 0, 0, F@_1, F@_2, F@_3, TrUserData); + 26 -> d_field_async_call_reply_message(Rest, 0, 0, 0, F@_1, F@_2, F@_3, TrUserData); _ -> case Key band 7 of - 0 -> skip_varint_push_reply(Rest, 0, 0, Key bsr 3, F@_1, F@_2, F@_3, TrUserData); - 1 -> skip_64_push_reply(Rest, 0, 0, Key bsr 3, F@_1, F@_2, F@_3, TrUserData); - 2 -> skip_length_delimited_push_reply(Rest, 0, 0, Key bsr 3, F@_1, F@_2, F@_3, TrUserData); - 3 -> skip_group_push_reply(Rest, 0, 0, Key bsr 3, F@_1, F@_2, F@_3, TrUserData); - 5 -> skip_32_push_reply(Rest, 0, 0, Key bsr 3, F@_1, F@_2, F@_3, TrUserData) + 0 -> skip_varint_async_call_reply(Rest, 0, 0, Key bsr 3, F@_1, F@_2, F@_3, TrUserData); + 1 -> skip_64_async_call_reply(Rest, 0, 0, Key bsr 3, F@_1, F@_2, F@_3, TrUserData); + 2 -> skip_length_delimited_async_call_reply(Rest, 0, 0, Key bsr 3, F@_1, F@_2, F@_3, TrUserData); + 3 -> skip_group_async_call_reply(Rest, 0, 0, Key bsr 3, F@_1, F@_2, F@_3, TrUserData); + 5 -> skip_32_async_call_reply(Rest, 0, 0, Key bsr 3, F@_1, F@_2, F@_3, TrUserData) end end; -dg_read_field_def_push_reply(<<>>, 0, 0, _, F@_1, F@_2, F@_3, _) -> #push_reply{code = F@_1, result = F@_2, message = F@_3}. +dg_read_field_def_async_call_reply(<<>>, 0, 0, _, F@_1, F@_2, F@_3, _) -> #async_call_reply{code = F@_1, result = F@_2, message = F@_3}. -d_field_push_reply_code(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, F@_3, TrUserData) when N < 57 -> d_field_push_reply_code(Rest, N + 7, X bsl N + Acc, F, F@_1, F@_2, F@_3, TrUserData); -d_field_push_reply_code(<<0:1, X:7, Rest/binary>>, N, Acc, F, _, F@_2, F@_3, TrUserData) -> +d_field_async_call_reply_code(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, F@_3, TrUserData) when N < 57 -> d_field_async_call_reply_code(Rest, N + 7, X bsl N + Acc, F, F@_1, F@_2, F@_3, TrUserData); +d_field_async_call_reply_code(<<0:1, X:7, Rest/binary>>, N, Acc, F, _, F@_2, F@_3, TrUserData) -> {NewFValue, RestF} = {id((X bsl N + Acc) band 4294967295, TrUserData), Rest}, - dfp_read_field_def_push_reply(RestF, 0, 0, F, NewFValue, F@_2, F@_3, TrUserData). + dfp_read_field_def_async_call_reply(RestF, 0, 0, F, NewFValue, F@_2, F@_3, TrUserData). -d_field_push_reply_result(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, F@_3, TrUserData) when N < 57 -> d_field_push_reply_result(Rest, N + 7, X bsl N + Acc, F, F@_1, F@_2, F@_3, TrUserData); -d_field_push_reply_result(<<0:1, X:7, Rest/binary>>, N, Acc, F, F@_1, _, F@_3, TrUserData) -> +d_field_async_call_reply_result(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, F@_3, TrUserData) when N < 57 -> d_field_async_call_reply_result(Rest, N + 7, X bsl N + Acc, F, F@_1, F@_2, F@_3, TrUserData); +d_field_async_call_reply_result(<<0:1, X:7, Rest/binary>>, N, Acc, F, F@_1, _, F@_3, TrUserData) -> {NewFValue, RestF} = begin Len = X bsl N + Acc, <> = Rest, Bytes2 = binary:copy(Bytes), {id(Bytes2, TrUserData), Rest2} end, - dfp_read_field_def_push_reply(RestF, 0, 0, F, F@_1, NewFValue, F@_3, TrUserData). + dfp_read_field_def_async_call_reply(RestF, 0, 0, F, F@_1, NewFValue, F@_3, TrUserData). -d_field_push_reply_message(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, F@_3, TrUserData) when N < 57 -> d_field_push_reply_message(Rest, N + 7, X bsl N + Acc, F, F@_1, F@_2, F@_3, TrUserData); -d_field_push_reply_message(<<0:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, _, TrUserData) -> +d_field_async_call_reply_message(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, F@_3, TrUserData) when N < 57 -> d_field_async_call_reply_message(Rest, N + 7, X bsl N + Acc, F, F@_1, F@_2, F@_3, TrUserData); +d_field_async_call_reply_message(<<0:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, _, TrUserData) -> {NewFValue, RestF} = begin Len = X bsl N + Acc, <> = Rest, Bytes2 = binary:copy(Bytes), {id(Bytes2, TrUserData), Rest2} end, - dfp_read_field_def_push_reply(RestF, 0, 0, F, F@_1, F@_2, NewFValue, TrUserData). + dfp_read_field_def_async_call_reply(RestF, 0, 0, F, F@_1, F@_2, NewFValue, TrUserData). -skip_varint_push_reply(<<1:1, _:7, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData) -> skip_varint_push_reply(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData); -skip_varint_push_reply(<<0:1, _:7, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData) -> dfp_read_field_def_push_reply(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData). +skip_varint_async_call_reply(<<1:1, _:7, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData) -> skip_varint_async_call_reply(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData); +skip_varint_async_call_reply(<<0:1, _:7, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData) -> dfp_read_field_def_async_call_reply(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData). -skip_length_delimited_push_reply(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, F@_3, TrUserData) when N < 57 -> skip_length_delimited_push_reply(Rest, N + 7, X bsl N + Acc, F, F@_1, F@_2, F@_3, TrUserData); -skip_length_delimited_push_reply(<<0:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, F@_3, TrUserData) -> +skip_length_delimited_async_call_reply(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, F@_3, TrUserData) when N < 57 -> skip_length_delimited_async_call_reply(Rest, N + 7, X bsl N + Acc, F, F@_1, F@_2, F@_3, TrUserData); +skip_length_delimited_async_call_reply(<<0:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, F@_3, TrUserData) -> Length = X bsl N + Acc, <<_:Length/binary, Rest2/binary>> = Rest, - dfp_read_field_def_push_reply(Rest2, 0, 0, F, F@_1, F@_2, F@_3, TrUserData). + dfp_read_field_def_async_call_reply(Rest2, 0, 0, F, F@_1, F@_2, F@_3, TrUserData). -skip_group_push_reply(Bin, _, Z2, FNum, F@_1, F@_2, F@_3, TrUserData) -> +skip_group_async_call_reply(Bin, _, Z2, FNum, F@_1, F@_2, F@_3, TrUserData) -> {_, Rest} = read_group(Bin, FNum), - dfp_read_field_def_push_reply(Rest, 0, Z2, FNum, F@_1, F@_2, F@_3, TrUserData). + dfp_read_field_def_async_call_reply(Rest, 0, Z2, FNum, F@_1, F@_2, F@_3, TrUserData). -skip_32_push_reply(<<_:32, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData) -> dfp_read_field_def_push_reply(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData). +skip_32_async_call_reply(<<_:32, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData) -> dfp_read_field_def_async_call_reply(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData). -skip_64_push_reply(<<_:64, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData) -> dfp_read_field_def_push_reply(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData). +skip_64_async_call_reply(<<_:64, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData) -> dfp_read_field_def_async_call_reply(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData). decode_msg_deploy(Bin, TrUserData) -> dfp_read_field_def_deploy(Bin, 0, 0, 0, id(0, TrUserData), id(<<>>, TrUserData), id(<<>>, TrUserData), TrUserData). @@ -1710,7 +1710,7 @@ merge_msgs(Prev, New, MsgName, Opts) -> auth_request -> merge_msg_auth_request(Prev, New, TrUserData); auth_reply -> merge_msg_auth_reply(Prev, New, TrUserData); pub -> merge_msg_pub(Prev, New, TrUserData); - push_reply -> merge_msg_push_reply(Prev, New, TrUserData); + async_call_reply -> merge_msg_async_call_reply(Prev, New, TrUserData); deploy -> merge_msg_deploy(Prev, New, TrUserData); fetch_task_log -> merge_msg_fetch_task_log(Prev, New, TrUserData); invoke -> merge_msg_invoke(Prev, New, TrUserData); @@ -1766,20 +1766,20 @@ merge_msg_pub(#pub{topic = PFtopic, content = PFcontent}, #pub{topic = NFtopic, true -> NFcontent end}. --compile({nowarn_unused_function,merge_msg_push_reply/3}). -merge_msg_push_reply(#push_reply{code = PFcode, result = PFresult, message = PFmessage}, #push_reply{code = NFcode, result = NFresult, message = NFmessage}, _) -> - #push_reply{code = - if NFcode =:= undefined -> PFcode; - true -> NFcode - end, - result = - if NFresult =:= undefined -> PFresult; - true -> NFresult - end, - message = - if NFmessage =:= undefined -> PFmessage; - true -> NFmessage - end}. +-compile({nowarn_unused_function,merge_msg_async_call_reply/3}). +merge_msg_async_call_reply(#async_call_reply{code = PFcode, result = PFresult, message = PFmessage}, #async_call_reply{code = NFcode, result = NFresult, message = NFmessage}, _) -> + #async_call_reply{code = + if NFcode =:= undefined -> PFcode; + true -> NFcode + end, + result = + if NFresult =:= undefined -> PFresult; + true -> NFresult + end, + message = + if NFmessage =:= undefined -> PFmessage; + true -> NFmessage + end}. -compile({nowarn_unused_function,merge_msg_deploy/3}). merge_msg_deploy(#deploy{task_id = PFtask_id, service_id = PFservice_id, tar_url = PFtar_url}, #deploy{task_id = NFtask_id, service_id = NFservice_id, tar_url = NFtar_url}, _) -> @@ -1958,7 +1958,7 @@ verify_msg(Msg, MsgName, Opts) -> auth_request -> v_msg_auth_request(Msg, [MsgName], TrUserData); auth_reply -> v_msg_auth_reply(Msg, [MsgName], TrUserData); pub -> v_msg_pub(Msg, [MsgName], TrUserData); - push_reply -> v_msg_push_reply(Msg, [MsgName], TrUserData); + async_call_reply -> v_msg_async_call_reply(Msg, [MsgName], TrUserData); deploy -> v_msg_deploy(Msg, [MsgName], TrUserData); fetch_task_log -> v_msg_fetch_task_log(Msg, [MsgName], TrUserData); invoke -> v_msg_invoke(Msg, [MsgName], TrUserData); @@ -2016,9 +2016,9 @@ v_msg_pub(#pub{topic = F1, content = F2}, Path, TrUserData) -> ok; v_msg_pub(X, Path, _TrUserData) -> mk_type_error({expected_msg, pub}, X, Path). --compile({nowarn_unused_function,v_msg_push_reply/3}). --dialyzer({nowarn_function,v_msg_push_reply/3}). -v_msg_push_reply(#push_reply{code = F1, result = F2, message = F3}, Path, TrUserData) -> +-compile({nowarn_unused_function,v_msg_async_call_reply/3}). +-dialyzer({nowarn_function,v_msg_async_call_reply/3}). +v_msg_async_call_reply(#async_call_reply{code = F1, result = F2, message = F3}, Path, TrUserData) -> if F1 == undefined -> ok; true -> v_type_uint32(F1, [code | Path], TrUserData) end, @@ -2029,7 +2029,7 @@ v_msg_push_reply(#push_reply{code = F1, result = F2, message = F3}, Path, TrUser true -> v_type_string(F3, [message | Path], TrUserData) end, ok; -v_msg_push_reply(X, Path, _TrUserData) -> mk_type_error({expected_msg, push_reply}, X, Path). +v_msg_async_call_reply(X, Path, _TrUserData) -> mk_type_error({expected_msg, async_call_reply}, X, Path). -compile({nowarn_unused_function,v_msg_deploy/3}). -dialyzer({nowarn_function,v_msg_deploy/3}). @@ -2262,7 +2262,7 @@ get_msg_defs() -> #field{name = timestamp, fnum = 6, rnum = 6, type = uint32, occurrence = optional, opts = []}]}, {{msg, auth_reply}, [#field{name = code, fnum = 1, rnum = 2, type = uint32, occurrence = optional, opts = []}, #field{name = message, fnum = 2, rnum = 3, type = string, occurrence = optional, opts = []}]}, {{msg, pub}, [#field{name = topic, fnum = 1, rnum = 2, type = string, occurrence = optional, opts = []}, #field{name = content, fnum = 2, rnum = 3, type = string, occurrence = optional, opts = []}]}, - {{msg, push_reply}, + {{msg, async_call_reply}, [#field{name = code, fnum = 1, rnum = 2, type = uint32, occurrence = optional, opts = []}, #field{name = result, fnum = 2, rnum = 3, type = string, occurrence = optional, opts = []}, #field{name = message, fnum = 3, rnum = 4, type = string, occurrence = optional, opts = []}]}, @@ -2308,13 +2308,13 @@ get_msg_defs() -> #field{name = params, fnum = 3, rnum = 4, type = string, occurrence = optional, opts = []}]}]. -get_msg_names() -> [auth_request, auth_reply, pub, push_reply, deploy, fetch_task_log, invoke, service_config, data, ping, service_inform, event]. +get_msg_names() -> [auth_request, auth_reply, pub, async_call_reply, deploy, fetch_task_log, invoke, service_config, data, ping, service_inform, event]. get_group_names() -> []. -get_msg_or_group_names() -> [auth_request, auth_reply, pub, push_reply, deploy, fetch_task_log, invoke, service_config, data, ping, service_inform, event]. +get_msg_or_group_names() -> [auth_request, auth_reply, pub, async_call_reply, deploy, fetch_task_log, invoke, service_config, data, ping, service_inform, event]. get_enum_names() -> []. @@ -2339,7 +2339,7 @@ find_msg_def(auth_request) -> #field{name = timestamp, fnum = 6, rnum = 6, type = uint32, occurrence = optional, opts = []}]; find_msg_def(auth_reply) -> [#field{name = code, fnum = 1, rnum = 2, type = uint32, occurrence = optional, opts = []}, #field{name = message, fnum = 2, rnum = 3, type = string, occurrence = optional, opts = []}]; find_msg_def(pub) -> [#field{name = topic, fnum = 1, rnum = 2, type = string, occurrence = optional, opts = []}, #field{name = content, fnum = 2, rnum = 3, type = string, occurrence = optional, opts = []}]; -find_msg_def(push_reply) -> +find_msg_def(async_call_reply) -> [#field{name = code, fnum = 1, rnum = 2, type = uint32, occurrence = optional, opts = []}, #field{name = result, fnum = 2, rnum = 3, type = string, occurrence = optional, opts = []}, #field{name = message, fnum = 3, rnum = 4, type = string, occurrence = optional, opts = []}]; @@ -2444,7 +2444,7 @@ service_and_rpc_name_to_fqbins(S, R) -> error({gpb_error, {badservice_or_rpc, {S fqbin_to_msg_name(<<"AuthRequest">>) -> auth_request; fqbin_to_msg_name(<<"AuthReply">>) -> auth_reply; fqbin_to_msg_name(<<"Pub">>) -> pub; -fqbin_to_msg_name(<<"PushReply">>) -> push_reply; +fqbin_to_msg_name(<<"AsyncCallReply">>) -> async_call_reply; fqbin_to_msg_name(<<"Deploy">>) -> deploy; fqbin_to_msg_name(<<"FetchTaskLog">>) -> fetch_task_log; fqbin_to_msg_name(<<"Invoke">>) -> invoke; @@ -2459,7 +2459,7 @@ fqbin_to_msg_name(E) -> error({gpb_error, {badmsg, E}}). msg_name_to_fqbin(auth_request) -> <<"AuthRequest">>; msg_name_to_fqbin(auth_reply) -> <<"AuthReply">>; msg_name_to_fqbin(pub) -> <<"Pub">>; -msg_name_to_fqbin(push_reply) -> <<"PushReply">>; +msg_name_to_fqbin(async_call_reply) -> <<"AsyncCallReply">>; msg_name_to_fqbin(deploy) -> <<"Deploy">>; msg_name_to_fqbin(fetch_task_log) -> <<"FetchTaskLog">>; msg_name_to_fqbin(invoke) -> <<"Invoke">>; @@ -2506,7 +2506,7 @@ get_all_source_basenames() -> ["message_pb.proto"]. get_all_proto_names() -> ["message_pb"]. -get_msg_containment("message_pb") -> [auth_reply, auth_request, data, deploy, event, fetch_task_log, invoke, ping, pub, push_reply, service_config, service_inform]; +get_msg_containment("message_pb") -> [async_call_reply, auth_reply, auth_request, data, deploy, event, fetch_task_log, invoke, ping, pub, service_config, service_inform]; get_msg_containment(P) -> error({gpb_error, {badproto, P}}). @@ -2534,9 +2534,9 @@ get_proto_by_msg_name_as_fqbin(<<"Invoke">>) -> "message_pb"; get_proto_by_msg_name_as_fqbin(<<"ServiceConfig">>) -> "message_pb"; get_proto_by_msg_name_as_fqbin(<<"Ping">>) -> "message_pb"; get_proto_by_msg_name_as_fqbin(<<"FetchTaskLog">>) -> "message_pb"; -get_proto_by_msg_name_as_fqbin(<<"PushReply">>) -> "message_pb"; get_proto_by_msg_name_as_fqbin(<<"Deploy">>) -> "message_pb"; get_proto_by_msg_name_as_fqbin(<<"AuthReply">>) -> "message_pb"; +get_proto_by_msg_name_as_fqbin(<<"AsyncCallReply">>) -> "message_pb"; get_proto_by_msg_name_as_fqbin(<<"ServiceInform">>) -> "message_pb"; get_proto_by_msg_name_as_fqbin(E) -> error({gpb_error, {badmsg, E}}). diff --git a/apps/iot/src/tcp/tcp_channel.erl b/apps/iot/src/tcp/tcp_channel.erl index 099bdcc..e19517e 100644 --- a/apps/iot/src/tcp/tcp_channel.erl +++ b/apps/iot/src/tcp/tcp_channel.erl @@ -13,7 +13,7 @@ %% API -export([stop/2, send/2]). --export([pub/3, push/4, command/3]). +-export([pub/3, async_call/4, command/3]). -export([start_link/2]). %% gen_server callbacks @@ -44,10 +44,10 @@ command(Pid, CommandType, Command) when is_pid(Pid), is_integer(CommandType), is gen_server:cast(Pid, {command, CommandType, Command}). %% 向通道中写入消息 --spec push(Pid :: pid(), ReceiverPid :: pid(), Method :: binary(), Params :: binary()) -> Ref :: reference(). -push(Pid, ReceiverPid, PushType, PushBin) when is_pid(Pid), is_integer(PushType), is_binary(PushBin) -> +-spec async_call(Pid :: pid(), ReceiverPid :: pid(), CallType :: integer(), CallBin :: binary()) -> Ref :: reference(). +async_call(Pid, ReceiverPid, CallType, CallBin) when is_pid(Pid), is_pid(ReceiverPid), is_integer(CallType), is_binary(CallBin) -> Ref = make_ref(), - gen_server:cast(Pid, {push, ReceiverPid, Ref, PushType, PushBin}), + gen_server:cast(Pid, {async_call, ReceiverPid, Ref, CallType, CallBin}), Ref. %% 关闭方法 @@ -90,8 +90,8 @@ handle_cast({command, CommandType, Command}, State = #state{transport = Transpor {noreply, State}; %% 推送消息 -handle_cast({push, ReceiverPid, Ref, PushType, PushBin}, State = #state{transport = Transport, socket = Socket, packet_id = PacketId, inflight = Inflight}) -> - Transport:send(Socket, <>), +handle_cast({async_call, ReceiverPid, Ref, CallType, CallBin}, State = #state{transport = Transport, socket = Socket, packet_id = PacketId, inflight = Inflight}) -> + Transport:send(Socket, <>), {noreply, State#state{packet_id = PacketId + 1, inflight = maps:put(PacketId, {ReceiverPid, Ref}, Inflight)}}. %% auth验证 @@ -157,19 +157,19 @@ handle_info({tcp, Socket, <> {noreply, State}; %% 主机端的消息响应 -handle_info({tcp, Socket, <>}, State = #state{socket = Socket, uuid = UUID, inflight = Inflight}) when PacketId > 0 -> - PushReply = message_pb:decode_msg(ResponseBin, push_reply), - lager:debug("[ws_channel] uuid: ~p, get push_reply: ~p, packet_id: ~p", [UUID, PushReply, PacketId]), +handle_info({tcp, Socket, <>}, State = #state{socket = Socket, uuid = UUID, inflight = Inflight}) when PacketId > 0 -> + AsyncCallReply = message_pb:decode_msg(ResponseBin, async_call_reply), + lager:debug("[ws_channel] uuid: ~p, get async_call_reply: ~p, packet_id: ~p", [UUID, AsyncCallReply, PacketId]), case maps:take(PacketId, Inflight) of error -> - lager:warning("[ws_channel] get unknown async_response message: ~p, packet_id: ~p", [PushReply, PacketId]), + lager:warning("[ws_channel] get unknown async_call_reply message: ~p, packet_id: ~p", [AsyncCallReply, PacketId]), {noreply, State}; {{ReceiverPid, Ref}, NInflight} -> case is_pid(ReceiverPid) andalso is_process_alive(ReceiverPid) of true -> - ReceiverPid ! {push_reply, Ref, PushReply}; + ReceiverPid ! {push_reply, Ref, AsyncCallReply}; false -> - lager:warning("[ws_channel] get async_response message: ~p, packet_id: ~p, but receiver_pid is deaded", [PushReply, PacketId]) + lager:warning("[ws_channel] get async_call_reply message: ~p, packet_id: ~p, but receiver_pid is deaded", [AsyncCallReply, PacketId]) end, {noreply, State#state{inflight = NInflight}} end;