fix heartbeat
This commit is contained in:
parent
c0400e6347
commit
e46dc8d2bb
@ -37,6 +37,15 @@ init([]) ->
|
|||||||
modules => ['iot_container_task_sup']
|
modules => ['iot_container_task_sup']
|
||||||
},
|
},
|
||||||
|
|
||||||
|
#{
|
||||||
|
id => 'udp_server',
|
||||||
|
start => {'udp_server', start_link, []},
|
||||||
|
restart => permanent,
|
||||||
|
shutdown => 2000,
|
||||||
|
type => worker,
|
||||||
|
modules => ['udp_server']
|
||||||
|
},
|
||||||
|
|
||||||
#{
|
#{
|
||||||
id => endpoint_sup_sup,
|
id => endpoint_sup_sup,
|
||||||
start => {'endpoint_sup_sup', start_link, []},
|
start => {'endpoint_sup_sup', start_link, []},
|
||||||
|
|||||||
@ -1,33 +1,110 @@
|
|||||||
%%%-------------------------------------------------------------------
|
%%%-------------------------------------------------------------------
|
||||||
%%% @author aresei
|
%%% @author anlicheng
|
||||||
%%% @copyright (C) 2023, <COMPANY>
|
%%% @copyright (C) 2026, <COMPANY>
|
||||||
%%% @doc
|
%%% @doc
|
||||||
%%%
|
%%%
|
||||||
%%% @end
|
%%% @end
|
||||||
%%% Created : 31. 8月 2023 13:48
|
%%% Created : 09. 5月 2026 18:02
|
||||||
%%%-------------------------------------------------------------------
|
%%%-------------------------------------------------------------------
|
||||||
-module(udp_server).
|
-module(udp_server).
|
||||||
-author("aresei").
|
-author("anlicheng").
|
||||||
|
|
||||||
%% API
|
-behaviour(gen_server).
|
||||||
-export([start_link/2, loop/2]).
|
|
||||||
|
|
||||||
-define(HEARTBEAT_VERSION, 1).
|
-define(HEARTBEAT_VERSION, 1).
|
||||||
-define(HEARTBEAT_NONCE_BYTES, 16).
|
-define(HEARTBEAT_NONCE_BYTES, 16).
|
||||||
-define(HEARTBEAT_MAC_BYTES, 32).
|
-define(HEARTBEAT_MAC_BYTES, 32).
|
||||||
|
|
||||||
start_link(Transport, Peer) ->
|
%% API
|
||||||
{ok, spawn_link(?MODULE, loop, [Transport, Peer])}.
|
-export([start_link/0]).
|
||||||
|
|
||||||
loop(Transport = {udp, Server, _Sock}, Peer) ->
|
%% gen_server callbacks
|
||||||
receive
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
|
||||||
{datagram, Server, Packet} ->
|
|
||||||
handle_heartbeat_packet(Packet, Peer),
|
-define(SERVER, ?MODULE).
|
||||||
loop(Transport, Peer);
|
|
||||||
Other ->
|
-record(state, {
|
||||||
logger:warning("[udp_server] ignore unknown message from peer: ~p, message: ~p", [Peer, Other]),
|
socket :: gen_udp:socket()
|
||||||
loop(Transport, Peer)
|
}).
|
||||||
end.
|
|
||||||
|
%%%===================================================================
|
||||||
|
%%% API
|
||||||
|
%%%===================================================================
|
||||||
|
|
||||||
|
%% @doc Spawns the server and registers the local name (unique)
|
||||||
|
-spec(start_link() ->
|
||||||
|
{ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
|
||||||
|
start_link() ->
|
||||||
|
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
|
||||||
|
|
||||||
|
%%%===================================================================
|
||||||
|
%%% gen_server callbacks
|
||||||
|
%%%===================================================================
|
||||||
|
|
||||||
|
%% @private
|
||||||
|
%% @doc Initializes the server
|
||||||
|
-spec(init(Args :: term()) ->
|
||||||
|
{ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
|
||||||
|
{stop, Reason :: term()} | ignore).
|
||||||
|
init([]) ->
|
||||||
|
{ok, UdpServerProps} = application:get_env(iot, udp_server),
|
||||||
|
Port = proplists:get_value(port, UdpServerProps),
|
||||||
|
{ok, Socket} = gen_udp:open(Port, [binary, {active, true}]),
|
||||||
|
{ok, #state{socket = Socket}}.
|
||||||
|
|
||||||
|
%% @private
|
||||||
|
%% @doc Handling call messages
|
||||||
|
-spec(handle_call(Request :: term(), From :: {pid(), Tag :: term()},
|
||||||
|
State :: #state{}) ->
|
||||||
|
{reply, Reply :: term(), NewState :: #state{}} |
|
||||||
|
{reply, Reply :: term(), NewState :: #state{}, timeout() | hibernate} |
|
||||||
|
{noreply, NewState :: #state{}} |
|
||||||
|
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
||||||
|
{stop, Reason :: term(), Reply :: term(), NewState :: #state{}} |
|
||||||
|
{stop, Reason :: term(), NewState :: #state{}}).
|
||||||
|
handle_call(_Request, _From, State = #state{}) ->
|
||||||
|
{reply, ok, State}.
|
||||||
|
|
||||||
|
%% @private
|
||||||
|
%% @doc Handling cast messages
|
||||||
|
-spec(handle_cast(Request :: term(), State :: #state{}) ->
|
||||||
|
{noreply, NewState :: #state{}} |
|
||||||
|
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
||||||
|
{stop, Reason :: term(), NewState :: #state{}}).
|
||||||
|
handle_cast(_Request, State = #state{}) ->
|
||||||
|
{noreply, State}.
|
||||||
|
|
||||||
|
%% @private
|
||||||
|
%% @doc Handling all non call/cast messages
|
||||||
|
-spec(handle_info(Info :: timeout() | term(), State :: #state{}) ->
|
||||||
|
{noreply, NewState :: #state{}} |
|
||||||
|
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
||||||
|
{stop, Reason :: term(), NewState :: #state{}}).
|
||||||
|
handle_info({udp, Socket, Ip, Port, Packet}, State = #state{socket = Socket}) ->
|
||||||
|
handle_heartbeat_packet(Packet, {Ip, Port}),
|
||||||
|
{noreply, State}.
|
||||||
|
|
||||||
|
%% @private
|
||||||
|
%% @doc This function is called by a gen_server when it is about to
|
||||||
|
%% terminate. It should be the opposite of Module:init/1 and do any
|
||||||
|
%% necessary cleaning up. When it returns, the gen_server terminates
|
||||||
|
%% with Reason. The return value is ignored.
|
||||||
|
-spec(terminate(Reason :: (normal | shutdown | {shutdown, term()} | term()),
|
||||||
|
State :: #state{}) -> term()).
|
||||||
|
terminate(_Reason, _State = #state{}) ->
|
||||||
|
ok.
|
||||||
|
|
||||||
|
%% @private
|
||||||
|
%% @doc Convert process state when code is changed
|
||||||
|
-spec(code_change(OldVsn :: term() | {down, term()}, State :: #state{},
|
||||||
|
Extra :: term()) ->
|
||||||
|
{ok, NewState :: #state{}} | {error, Reason :: term()}).
|
||||||
|
code_change(_OldVsn, State = #state{}, _Extra) ->
|
||||||
|
{ok, State}.
|
||||||
|
|
||||||
|
%%%===================================================================
|
||||||
|
%%% Internal functions
|
||||||
|
%%%===================================================================
|
||||||
|
|
||||||
-spec handle_heartbeat_packet(binary(), term()) -> ok.
|
-spec handle_heartbeat_packet(binary(), term()) -> ok.
|
||||||
handle_heartbeat_packet(Packet, Peer) when is_binary(Packet) ->
|
handle_heartbeat_packet(Packet, Peer) when is_binary(Packet) ->
|
||||||
@ -50,7 +127,7 @@ handle_heartbeat_packet(Packet, Peer) when is_binary(Packet) ->
|
|||||||
{ok, UUID :: binary(), Timestamp :: integer(), Payload :: binary(), Mac :: binary()} | error.
|
{ok, UUID :: binary(), Timestamp :: integer(), Payload :: binary(), Mac :: binary()} | error.
|
||||||
decode_heartbeat_packet(Packet = <<?HEARTBEAT_VERSION:8, UUIDLen:16, UUID:UUIDLen/binary,
|
decode_heartbeat_packet(Packet = <<?HEARTBEAT_VERSION:8, UUIDLen:16, UUID:UUIDLen/binary,
|
||||||
Timestamp:64/unsigned-big, _Nonce:?HEARTBEAT_NONCE_BYTES/binary, _Mac:?HEARTBEAT_MAC_BYTES/binary>>)
|
Timestamp:64/unsigned-big, _Nonce:?HEARTBEAT_NONCE_BYTES/binary, _Mac:?HEARTBEAT_MAC_BYTES/binary>>)
|
||||||
when UUIDLen > 0 ->
|
when UUIDLen > 0 ->
|
||||||
PayloadSize = byte_size(Packet) - ?HEARTBEAT_MAC_BYTES,
|
PayloadSize = byte_size(Packet) - ?HEARTBEAT_MAC_BYTES,
|
||||||
<<Payload:PayloadSize/binary, Mac:?HEARTBEAT_MAC_BYTES/binary>> = Packet,
|
<<Payload:PayloadSize/binary, Mac:?HEARTBEAT_MAC_BYTES/binary>> = Packet,
|
||||||
{ok, UUID, Timestamp, Payload, Mac};
|
{ok, UUID, Timestamp, Payload, Mac};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user