This commit is contained in:
anlicheng 2025-09-19 16:42:46 +08:00
parent 1fb4cbfe61
commit 9d91c0ef1e

View File

@ -9,13 +9,14 @@
-module(tcp_channel).
-author("licheng5").
-include("message.hrl").
-behaviour(ranch_protocol).
%% API
-export([pub/3, rpc_call/3, command/3]).
-export([start_link/2, stop/2]).
-export([start_link/3, stop/2]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, code_change/3, terminate/2]).
-export([init/3, handle_call/3, handle_cast/2, handle_info/2, code_change/3, terminate/2]).
-record(state, {
transport,
@ -59,19 +60,15 @@ stop(Pid, Reason) when is_pid(Pid) ->
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
start_link(Transport, Sock) ->
{ok, proc_lib:spawn_link(?MODULE, init, [[Transport, Sock]])}.
start_link(Ref, Transport, Opts) ->
{ok, proc_lib:spawn_link(?MODULE, init, [Ref, Transport, Opts])}.
init([Transport, Sock]) ->
lager: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(Ref, Transport, _Opts = []) ->
{ok, Socket} = ranch:handshake(Ref),
lager:debug("[sdlan_channel] get a new connection: ~p", [Socket]),
Transport:setopts(Socket, [{active, true}]),
% erlang:start_timer(?PING_TICKER, self(), ping_ticker),
gen_server:enter_loop(?MODULE, [], #state{transport = Transport, socket = Socket}).
handle_call(_Request, _From, State) ->
{reply, ok, State}.