%%%------------------------------------------------------------------- %%% @author anlicheng %%% @copyright (C) 2026, %%% @doc %%% %%% @end %%% Created : 28. 2月 2026 22:07 %%%------------------------------------------------------------------- -module(maxwell_redis_server). -author("licheng5"). %% API -export([start_link/1, init/1]). %%-------------------------------------------------------------------- %% esockd callback %%-------------------------------------------------------------------- start_link(Port) when is_integer(Port) -> {ok, spawn_link(?MODULE, init, [Port])}. init(Port) -> {ok, LSocket} = gen_tcp:listen(Port, [ binary, {packet, 0}, {reuseaddr, true}, {backlog, 1024}, {active, false} ]), accept_loop(LSocket). accept_loop(LSocket) -> case gen_tcp:accept(LSocket) of {ok, Socket} -> logger:debug("accept socket: ~p", [Socket]), %% 每个连接一个进程 Pid = maxwell_redis_channel:start(Socket), ok = gen_tcp:controlling_process(Socket, Pid), accept_loop(LSocket); {error, closed} -> exit(tcp_closed); {error, Reason} -> logger:debug("[maxwell_redis_server] Accept error: ~p~n", [Reason]), accept_loop(LSocket) end.