fix
This commit is contained in:
parent
cb1e195a38
commit
fe7a197af8
@ -1,34 +0,0 @@
|
|||||||
%%%-------------------------------------------------------------------
|
|
||||||
%%% @author anlicheng
|
|
||||||
%%% @copyright (C) 2025, <COMPANY>
|
|
||||||
%%% @doc
|
|
||||||
%%%
|
|
||||||
%%% @end
|
|
||||||
%%% Created : 18. 8月 2025 18:40
|
|
||||||
%%%-------------------------------------------------------------------
|
|
||||||
-module(http_client).
|
|
||||||
-author("anlicheng").
|
|
||||||
|
|
||||||
%% API
|
|
||||||
-export([post/3]).
|
|
||||||
|
|
||||||
%% Headers = [
|
|
||||||
%% {<<"content-type">>, <<"application/json">>}
|
|
||||||
%% ]
|
|
||||||
-spec post(Url :: string(), Headers :: list(), Body :: binary()) -> {ok, RespBody :: binary()} | {error, Reason :: any()}.
|
|
||||||
post(Url, Headers, Body) when is_list(Url), is_list(Headers), is_binary(Body) ->
|
|
||||||
case hackney:request(post, Url, Headers, Body, [{pool, false}]) of
|
|
||||||
{ok, 200, _, ClientRef} ->
|
|
||||||
{ok, RespBody} = hackney:body(ClientRef),
|
|
||||||
logger:debug("[http_client] send body: ~p, get error is: ~p", [Body, RespBody]),
|
|
||||||
hackney:close(ClientRef),
|
|
||||||
{ok, RespBody};
|
|
||||||
{ok, HttpCode, _, ClientRef} ->
|
|
||||||
{ok, RespBody} = hackney:body(ClientRef),
|
|
||||||
hackney:close(ClientRef),
|
|
||||||
logger:warning("[http_client] send body: ~p, get error is: ~p", [Body, {HttpCode, RespBody}]),
|
|
||||||
{error, {HttpCode, RespBody}};
|
|
||||||
{error, Reason} ->
|
|
||||||
logger:warning("[http_client] send body: ~p, get error is: ~p", [Body, Reason]),
|
|
||||||
{error, Reason}
|
|
||||||
end.
|
|
||||||
@ -1,40 +0,0 @@
|
|||||||
%%%-------------------------------------------------------------------
|
|
||||||
%%% @author licheng5
|
|
||||||
%%% @copyright (C) 2023, <COMPANY>
|
|
||||||
%%% @doc
|
|
||||||
%%%
|
|
||||||
%%% @end
|
|
||||||
%%% Created : 03. 3月 2023 11:48
|
|
||||||
%%%-------------------------------------------------------------------
|
|
||||||
-module(iot_http_client).
|
|
||||||
-author("licheng5").
|
|
||||||
|
|
||||||
%% API
|
|
||||||
-export([post/2]).
|
|
||||||
|
|
||||||
post(Url, Body) when is_list(Url), is_binary(Body) ->
|
|
||||||
case hackney:request(post, Url, [], Body) of
|
|
||||||
{ok, 200, _, ClientRef} ->
|
|
||||||
case hackney:body(ClientRef) of
|
|
||||||
{ok, RespBody} ->
|
|
||||||
logger:debug("[iot_http_client] url: ~p, response is: ~p", [Url, RespBody]),
|
|
||||||
ok;
|
|
||||||
{error, Reason} ->
|
|
||||||
logger:warning("[iot_http_client] url: ~p, get error: ~p", [Url, Reason]),
|
|
||||||
{error, Reason}
|
|
||||||
end;
|
|
||||||
|
|
||||||
{ok, HttpCode, _, ClientRef} ->
|
|
||||||
case hackney:body(ClientRef) of
|
|
||||||
{ok, RespBody} ->
|
|
||||||
logger:debug("[iot_http_client] url: ~p, http_code: ~p, response is: ~p", [Url, HttpCode, RespBody]),
|
|
||||||
ok;
|
|
||||||
{error, Reason} ->
|
|
||||||
logger:warning("[iot_http_client] url: ~p, http_code: ~p, get error: ~p", [Url, HttpCode, Reason]),
|
|
||||||
{error, Reason}
|
|
||||||
end;
|
|
||||||
|
|
||||||
{error, Reason} ->
|
|
||||||
logger:warning("[iot_http_client] url: ~p, get error: ~p", [Url, Reason]),
|
|
||||||
{error, Reason}
|
|
||||||
end.
|
|
||||||
@ -121,21 +121,18 @@ handle_cast({command, CommandType, Command}, State = #state{transport = Transpor
|
|||||||
|
|
||||||
%% 推送需要响应的请求
|
%% 推送需要响应的请求
|
||||||
handle_cast({request_call, ReceiverPid, Ref, Body}, State = #state{transport = Transport, socket = Socket, packet_id = PacketId, inflight = Inflight}) ->
|
handle_cast({request_call, ReceiverPid, Ref, Body}, State = #state{transport = Transport, socket = Socket, packet_id = PacketId, inflight = Inflight}) ->
|
||||||
case next_packet_id(PacketId, Inflight) of
|
Encoded = message_pb:encode_msg(#'RequestFrame'{
|
||||||
{ok, NPacketId, NextPacketId} ->
|
packet_id = PacketId,
|
||||||
Encoded = message_pb:encode_msg(#'RequestFrame'{
|
body = Body
|
||||||
packet_id = NPacketId,
|
}),
|
||||||
body = Body
|
TimerRef = erlang:start_timer(?INFLIGHT_TIMEOUT, self(), {request_timeout, PacketId}),
|
||||||
}),
|
Transport:send(Socket, <<?FRAME_REQUEST, Encoded/binary>>),
|
||||||
TimerRef = erlang:start_timer(?INFLIGHT_TIMEOUT, self(), {request_timeout, NPacketId}),
|
|
||||||
Transport:send(Socket, <<?FRAME_REQUEST, Encoded/binary>>),
|
|
||||||
|
|
||||||
RequestInfo = #inflight_request{receiver_pid = ReceiverPid, ref = Ref, timer_ref = TimerRef},
|
RequestInfo = #inflight_request{receiver_pid = ReceiverPid, ref = Ref, timer_ref = TimerRef},
|
||||||
{noreply, State#state{packet_id = NextPacketId, inflight = maps:put(NPacketId, RequestInfo, Inflight)}};
|
{noreply, State#state{
|
||||||
{error, inflight_full} ->
|
packet_id = inc_packet_id(PacketId),
|
||||||
logger:warning("[ws_channel] uuid: ~p, inflight requests exhausted", [State#state.uuid]),
|
inflight = maps:put(PacketId, RequestInfo, Inflight)
|
||||||
{noreply, State}
|
}}.
|
||||||
end.
|
|
||||||
|
|
||||||
%% auth验证
|
%% auth验证
|
||||||
handle_info({tcp, Socket, <<?FRAME_REQUEST, FrameBin/binary>>}, State = #state{transport = Transport, socket = Socket}) ->
|
handle_info({tcp, Socket, <<?FRAME_REQUEST, FrameBin/binary>>}, State = #state{transport = Transport, socket = Socket}) ->
|
||||||
@ -205,42 +202,17 @@ take_inflight_by_ref(Ref, Inflight) ->
|
|||||||
end
|
end
|
||||||
end, error, Inflight).
|
end, error, Inflight).
|
||||||
|
|
||||||
-spec next_packet_id(integer(), map()) ->
|
|
||||||
{ok, integer(), integer()} | {error, inflight_full}.
|
|
||||||
next_packet_id(_PacketId, Inflight) when map_size(Inflight) >= ?MAX_PACKET_ID ->
|
|
||||||
{error, inflight_full};
|
|
||||||
next_packet_id(PacketId, Inflight) ->
|
|
||||||
next_packet_id(PacketId, Inflight, PacketId, 0).
|
|
||||||
|
|
||||||
-spec next_packet_id(integer(), map(), integer(), non_neg_integer()) ->
|
|
||||||
{ok, integer(), integer()} | {error, inflight_full}.
|
|
||||||
next_packet_id(_PacketId, _Inflight, _StartPacketId, TryCount) when TryCount > ?MAX_PACKET_ID ->
|
|
||||||
{error, inflight_full};
|
|
||||||
next_packet_id(PacketId, Inflight, StartPacketId, TryCount) ->
|
|
||||||
case maps:is_key(PacketId, Inflight) of
|
|
||||||
false ->
|
|
||||||
{ok, PacketId, inc_packet_id(PacketId)};
|
|
||||||
true ->
|
|
||||||
NPacketId = inc_packet_id(PacketId),
|
|
||||||
case NPacketId =:= StartPacketId of
|
|
||||||
true ->
|
|
||||||
{error, inflight_full};
|
|
||||||
false ->
|
|
||||||
next_packet_id(NPacketId, Inflight, StartPacketId, TryCount + 1)
|
|
||||||
end
|
|
||||||
end.
|
|
||||||
|
|
||||||
-spec inc_packet_id(integer()) -> integer().
|
-spec inc_packet_id(integer()) -> integer().
|
||||||
inc_packet_id(?MAX_PACKET_ID) ->
|
inc_packet_id(?MAX_PACKET_ID) ->
|
||||||
1;
|
1;
|
||||||
inc_packet_id(PacketId) when PacketId > 0, PacketId < ?MAX_PACKET_ID ->
|
inc_packet_id(PacketId) when PacketId > 0, PacketId < ?MAX_PACKET_ID ->
|
||||||
PacketId + 1.
|
PacketId + 1.
|
||||||
|
|
||||||
-spec handle_request_frame(message_pb:'RequestFrame'(), module(), any(), #state{}) ->
|
-spec handle_request_frame(message_pb:'RequestFrame'(), module(), any(), #state{}) -> {noreply, #state{}} | {stop, #state{}}.
|
||||||
{noreply, #state{}} | {stop, #state{}}.
|
|
||||||
handle_request_frame(#'RequestFrame'{packet_id = PacketId,
|
handle_request_frame(#'RequestFrame'{packet_id = PacketId,
|
||||||
body = {auth_request, #'AuthRequest'{uuid = UUID, username = Username, token = Token, salt = Salt, timestamp = Timestamp}}},
|
body = {auth_request, #'AuthRequest'{uuid = UUID, username = Username, token = Token, salt = Salt, timestamp = Timestamp}}},
|
||||||
Transport, Socket, State) ->
|
Transport, Socket, State) ->
|
||||||
|
|
||||||
logger:debug("[ws_channel] auth uuid: ~p", [UUID]),
|
logger:debug("[ws_channel] auth uuid: ~p", [UUID]),
|
||||||
case iot_auth:check(Username, Token, UUID, Salt, Timestamp) of
|
case iot_auth:check(Username, Token, UUID, Salt, Timestamp) of
|
||||||
true ->
|
true ->
|
||||||
@ -310,14 +282,14 @@ handle_response_frame(#'ResponseFrame'{packet_id = PacketId, body = {rpc_reply,
|
|||||||
case maps:take(PacketId, Inflight) of
|
case maps:take(PacketId, Inflight) of
|
||||||
error ->
|
error ->
|
||||||
{noreply, State};
|
{noreply, State};
|
||||||
{#inflight_request{receiver_pid = ReceiverPid, ref = Ref, timer_ref = TimerRef}, NInflight} ->
|
{#inflight_request{receiver_pid = ReceiverPid, ref = Ref, timer_ref = TimerRef}, NInflight} ->
|
||||||
erlang:cancel_timer(TimerRef),
|
erlang:cancel_timer(TimerRef),
|
||||||
case is_pid(ReceiverPid) andalso is_process_alive(ReceiverPid) of
|
case is_pid(ReceiverPid) andalso is_process_alive(ReceiverPid) of
|
||||||
true ->
|
true ->
|
||||||
ReceiverPid ! {rpc_reply, Ref, RpcReply};
|
ReceiverPid ! {rpc_reply, Ref, RpcReply};
|
||||||
false ->
|
false ->
|
||||||
logger:warning("[ws_channel] get async_call_reply message: ~p, packet_id: ~p, but receiver_pid is deaded", [RpcReply, PacketId])
|
logger:warning("[ws_channel] get async_call_reply message: ~p, packet_id: ~p, but receiver_pid is deaded", [RpcReply, PacketId])
|
||||||
end,
|
end,
|
||||||
{noreply, State#state{inflight = NInflight}}
|
{noreply, State#state{inflight = NInflight}}
|
||||||
end;
|
end;
|
||||||
handle_response_frame(#'ResponseFrame'{packet_id = PacketId, body = {auth_reply, AuthReply}}, _Inflight, State) ->
|
handle_response_frame(#'ResponseFrame'{packet_id = PacketId, body = {auth_reply, AuthReply}}, _Inflight, State) ->
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user