This commit is contained in:
anlicheng 2023-08-31 16:53:47 +08:00
parent cfedc75e1e
commit 41a50c04cc
2 changed files with 28 additions and 1 deletions

View File

@ -92,7 +92,7 @@ start_udp_server() ->
{ok, Props} = application:get_env(iot, udp_server), {ok, Props} = application:get_env(iot, udp_server),
Port = proplists:get_value(port, Props), Port = proplists:get_value(port, Props),
UdpOpts = [{udp_options, [binary, {reuseaddr, true}]}], UdpOpts = [{udp_options, [binary, {reuseaddr, true}]}],
{ok, _} = esockd:open_udp(iot_udp_server, Port, UdpOpts, {iot_udp_server, start_link, []}), {ok, _} = esockd:open_udp(iot_udp_server, Port, UdpOpts, {iot_udp_handler, start_link, []}),
lager:debug("[iot_app] the udp server start at: ~p", [Port]). lager:debug("[iot_app] the udp server start at: ~p", [Port]).
%% %%

View File

@ -0,0 +1,27 @@
%%%-------------------------------------------------------------------
%%% @author aresei
%%% @copyright (C) 2023, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 31. 8 2023 13:48
%%%-------------------------------------------------------------------
-module(iot_udp_handler).
-author("aresei").
%% API
-export([start_link/2, loop/2]).
start_link(Transport, Peer) ->
{ok, spawn_link(?MODULE, loop, [Transport, Peer])}.
loop(Transport = {udp, Server, _Sock}, Peer) ->
receive
{datagram, Server, <<Len:16, HostUUID:Len/binary>>} ->
lager:debug("[iot_udp_server] peer: ~p, get heartbeat of: ~p", [esockd:format(Peer), HostUUID]),
Pid = iot_host:get_pid(HostUUID),
iot_host:heartbeat(Pid),
loop(Transport, Peer);
{datagram, Server, _} ->
exit(normal)
end.