fix tcp_server

This commit is contained in:
anlicheng 2025-04-29 23:47:50 +08:00
parent b30a8a91df
commit 067ead0f60
4 changed files with 115 additions and 15 deletions

View File

@ -37,6 +37,24 @@ init([]) ->
% modules => ['efka_agent']
%},
#{
id => 'efka_tcp_sup',
start => {'efka_tcp_sup', start_link, []},
restart => permanent,
shutdown => 2000,
type => supervisor,
modules => ['efka_tcp_sup']
},
#{
id => 'efka_tcp_server',
start => {'efka_tcp_server', start_link, [18080]},
restart => permanent,
shutdown => 2000,
type => worker,
modules => ['efka_tcp_server']
},
#{
id => 'efka_server_sup',
start => {'efka_server_sup', start_link, []},

View File

@ -6,7 +6,7 @@
%%% @end
%%% Created : 10. 12 2020 11:17
%%%-------------------------------------------------------------------
-module(tcp_channel).
-module(efka_tcp_channel).
-author("licheng5").
-behaviour(gen_server).
@ -14,13 +14,12 @@
-define(PING_TICKER, 15000).
%% API
-export([start_link/2]).
-export([start_link/1]).
-export([publish_command/4, send_event/3, stop/2, move_network/3]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-record(state, {
transport,
socket,
%%
is_registered = false,
@ -62,19 +61,15 @@ stop(Pid, Reason) when is_pid(Pid) ->
%% esockd callback
%%--------------------------------------------------------------------
start_link(Transport, Sock) ->
{ok, proc_lib:spawn_link(?MODULE, init, [[Transport, Sock]])}.
start_link(Socket) ->
{ok, proc_lib:spawn_link(?MODULE, init, [Socket])}.
init([Transport, Sock]) ->
efka_logger:debug("[sdlan_channel] get a new connection: ~p", [Sock]),
case Transport:wait(Sock) of
{ok, NewSock} ->
Transport:setopts(Sock, [{active, true}]),
erlang:start_timer(?PING_TICKER, self(), ping_ticker),
gen_server:enter_loop(?MODULE, [], #state{transport = Transport, socket = NewSock});
{error, Reason} ->
{stop, Reason}
end.
init(Socket) ->
efka_logger:debug("[sdlan_channel] get a new connection: ~p", [Socket]),
ok = inet:setopts(Socket, [{active, true}, {packet, 4}]),
erlang:start_timer(?PING_TICKER, self(), ping_ticker),
gen_server:enter_loop(?MODULE, [], #state{socket = Socket}).
handle_call(_Request, _From, State) ->
{reply, ok, State}.

View File

@ -0,0 +1,43 @@
%%%-------------------------------------------------------------------
%%% @author anlicheng
%%% @copyright (C) 2025, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 29. 4 2025 23:24
%%%-------------------------------------------------------------------
-module(efka_tcp_server).
-author("anlicheng").
%% API
-export([start_link/1, init/1]).
start_link(Port) ->
{ok, spawn_link(?MODULE, init, [Port])}.
%%
init(Port) ->
case gen_tcp:listen(Port, [binary, {active, false}, {reuseaddr, true}]) of
{ok, ListenSocket} ->
efka_logger:debug("Server started on port ~p~n", [Port]),
main_loop(ListenSocket);
{error, Reason} ->
efka_logger:debug("Failed to start server: ~p~n", [Reason]),
exit(Reason)
end.
main_loop(ListenSocket) ->
case gen_tcp:accept(ListenSocket) of
{ok, Socket} ->
efka_logger:debug("New client connected: ~p~n", [Socket]),
%
efka_tcp_sup:start_child(Socket),
%
main_loop(ListenSocket);
{error, closed} ->
efka_logger:debug("Server socket closed~n", []),
exit(tcp_closed);
{error, Reason} ->
efka_logger:debug("Accept error: ~p~n", [Reason]),
exit(Reason)
end.

View File

@ -0,0 +1,44 @@
%%%-------------------------------------------------------------------
%% @doc efka top level supervisor.
%% @end
%%%-------------------------------------------------------------------
-module(efka_tcp_sup).
-behaviour(supervisor).
-export([start_link/0, start_child/1]).
-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, {SupFlags, []}}.
%% internal functions
start_child(Socket) ->
supervisor:start_child(?MODULE, #{
id => make_ref(),
start => {efka_tcp_channel, start_link, [Socket]},
restart => permanent,
shutdown => 2000,
type => worker,
modules => ['efka_tcp_channel']
}).