From ab74389f078eed4e00df1d708ec678901c584b34 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Thu, 22 Jan 2026 16:00:52 +0800 Subject: [PATCH] fix --- apps/sdlan/src/sdlan_channel.erl | 2 +- apps/sdlan/src/sdlan_stun.erl | 11 +++++++- apps/sdlan/src/sdlan_stun_sup.erl | 46 +++++++++++++++++++++++++++++++ apps/sdlan/src/sdlan_sup.erl | 10 +++++++ config/sys-dev.config | 20 ++++++++++++-- 5 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 apps/sdlan/src/sdlan_stun_sup.erl diff --git a/apps/sdlan/src/sdlan_channel.erl b/apps/sdlan/src/sdlan_channel.erl index aa9ff11..2353cca 100644 --- a/apps/sdlan/src/sdlan_channel.erl +++ b/apps/sdlan/src/sdlan_channel.erl @@ -164,7 +164,7 @@ handle_info({tcp, Sock, <>}, S Transport:send(Sock, Reply), logger:debug("[sdlan_channel] client_id: ~p, mac: ~p, alloc ip: ~p, register will send ack, aes_key: ~p, enc_aes_key: ~p", [ClientId, sdlan_util:format_mac(Mac), sdlan_ipaddr:int_to_ipv4(NetAddr), AesKey, EncodedAesKey]), - + %% 设置节点的在线状态 Result = sdlan_api:node_online(ClientId, NetworkId, sdlan_ipaddr:int_to_ipv4(NetAddr)), logger:debug("[sdlan_channel] client_id: ~p, set none online, result is: ~p", [ClientId, Result]), diff --git a/apps/sdlan/src/sdlan_stun.erl b/apps/sdlan/src/sdlan_stun.erl index 686f503..7284b20 100644 --- a/apps/sdlan/src/sdlan_stun.erl +++ b/apps/sdlan/src/sdlan_stun.erl @@ -56,7 +56,16 @@ init([Port]) -> %% 需要提高进程的调度优先级 erlang:process_flag(priority, max), - {ok, Socket} = gen_udp:open(Port, [binary, {active, true}, {recbuf, 5 * 1024 * 1024}, {sndbuf, 5 * 1024 * 1024}]), + Opts = [ + binary, + {reuseaddr, true}, + {reuseport, true}, + {active, true}, + {recbuf, 5 * 1024 * 1024}, + {sndbuf, 5 * 1024 * 1024} + ], + + {ok, Socket} = gen_udp:open(Port, Opts), inet_udp:controlling_process(Socket, self()), logger:debug("[sdlan_stun] start at port: ~p", [Port]), diff --git a/apps/sdlan/src/sdlan_stun_sup.erl b/apps/sdlan/src/sdlan_stun_sup.erl new file mode 100644 index 0000000..9081291 --- /dev/null +++ b/apps/sdlan/src/sdlan_stun_sup.erl @@ -0,0 +1,46 @@ +%%%------------------------------------------------------------------- +%% @doc sdlan top level supervisor. +%% @end +%%%------------------------------------------------------------------- +-module(sdlan_stun_sup). + +-behaviour(supervisor). + +-export([start_link/0]). + +-export([init/1]). + +-define(SERVER, ?MODULE). + +start_link() -> + supervisor:start_link({local, ?SERVER}, ?MODULE, []). + +%% sup_flags() = #{strategy => strategy(), % optional +%% intensity => non_neg_integer(), % optional +%% period => pos_integer()} % optional +%% child_spec() = #{id => child_id(), % mandatory +%% start => mfargs(), % mandatory +%% restart => restart(), % optional +%% shutdown => shutdown(), % optional +%% type => worker(), % optional +%% modules => modules()} % optional +init([]) -> + SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600}, + + {ok, StunServerProps} = application:get_env(sdlan, stun_servers), + Port = proplists:get_value(port, StunServerProps), + AcceptorNums = proplists:get_value(acceptor_nums, StunServerProps), + + Specs = lists:map(fun(Id) -> + Name = sdlan_stun:get_name(Id), + #{ + id => Name, + start => {sdlan_stun, start_link, [Name, Port]}, + restart => permanent, + shutdown => 2000, + type => worker, + modules => ['sdlan_stun'] + } + end, lists:seq(1, AcceptorNums)), + + {ok, {SupFlags, Specs}}. \ No newline at end of file diff --git a/apps/sdlan/src/sdlan_sup.erl b/apps/sdlan/src/sdlan_sup.erl index 1361b3e..06ee8e8 100644 --- a/apps/sdlan/src/sdlan_sup.erl +++ b/apps/sdlan/src/sdlan_sup.erl @@ -45,6 +45,7 @@ init([]) -> type => worker, modules => ['sdlan_network_coordinator'] }, + #{ id => sdlan_network_sup, start => {sdlan_network_sup, start_link, []}, @@ -52,6 +53,15 @@ init([]) -> shutdown => 2000, type => supervisor, modules => ['sdlan_network_sup'] + }, + + #{ + id => sdlan_stun_sup, + start => {sdlan_stun_sup, start_link, []}, + restart => permanent, + shutdown => 2000, + type => supervisor, + modules => ['sdlan_stun_sup'] } ], diff --git a/config/sys-dev.config b/config/sys-dev.config index 3fdc3b5..de31ff0 100644 --- a/config/sys-dev.config +++ b/config/sys-dev.config @@ -18,8 +18,24 @@ %% 网络带宽, 单位为: kb {band_width, 2048}, - {stun_servers, [{'sdlan_stun:1:1', 1265}, {'sdlan_stun:1:2', 1266}]}, - {stun_assist, {{47,98,178,3}, 1266}}, + %% stun类型探测相当于有个类型 + {stun_servers, [ + {port, 1265}, + {acceptor_nums, 50} + ]}, + + {stun_port_assist, [ + {port, 1266} + ]}, + + {stun_peer_assist, [ + {ip, {47,98,178,3}}, + {port, 1266} + ]}, + + %{stun_assist, {{47,98,178,3}, 1266}}, + %{stun_servers, [{'sdlan_stun:1:1', 1265}, {'sdlan_stun:1:2', 1266}]}, + %{stun_assist, {{47,98,178,3}, 1266}}, % {stun_servers, [{'sdlan_stun:2:1', 1265}, {'sdlan_stun:2:2', 1266}]},