Quellcode durchsuchen

代码优化 添加tcp连接管理行为行为

master
AICells vor 5 Jahren
Ursprung
Commit
760fe1cec2
2 geänderte Dateien mit 47 neuen und 49 gelöschten Zeilen
  1. +41
    -49
      src/gen_ipc.erl
  2. +6
    -0
      src/gen_tcm.erl

+ 41
- 49
src/gen_ipc.erl Datei anzeigen

@ -60,7 +60,7 @@
?NOT_DEBUG ->
Debug;
_ ->
sys_debug(Debug, getProName(), SystemEvent)
sys:handle_debug(Debug, fun print_event/3, getProName(), SystemEvent)
end).
%%%==========================================================================
@ -69,7 +69,7 @@
%% gen:call
-type from() :: {To :: pid(), Tag :: term()}.
%%
%%
-type eventType() :: externalEventType() | timeoutEventType() | {'onevent', Subtype :: term()}.
-type externalEventType() :: {'call', From :: from()} | 'cast' | 'info'.
-type timeoutEventType() :: 'eTimeout' | 'sTimeout' | {'gTimeout', Name :: term()}.
@ -93,15 +93,15 @@
%% :
%% NextStatus and NewData are set.
%% actions()
%% action
%% action()
%% enter enter回调
%% doAfter after回调
%% "postpone" "true"
%%
%% postponed + + + Postpone事件 + LeftEvent
%% doAfter after回调
%% hibernate true
-type initAction() ::
{trap_exit, Bool :: isTrapExit()} | % gen_event模式下
{trap_exit, Bool :: isTrapExit()} | % gen_event模式下
eventAction().
-type eventAction() ::
@ -151,38 +151,38 @@
{'reply', From :: from(), Reply :: term()}.
-type eventCallbackResult() ::
{'reply', Reply :: term(), NewState :: term()} | % gen_server模式时快速响应进入消息接收
{'noreply', NewState :: term()} | % gen_server模式时快速响应进入消息接收
{'reply', Reply :: term(), NewState :: term(), Options :: hibernate | {doAfter, Args}} | % gen_server模式时快速响应进入消息接收
{'noreply', NewState :: term(), Options :: hibernate | {doAfter, Args}} | % gen_server模式时快速响应进入循环
{'nextStatus', NextStatus :: term(), NewState :: term()} | % {next_status,NextStatus,NewData,[]}
{'nextStatus', NextStatus :: term(), NewState :: term(), Actions :: [eventAction(), ...]} | % Status transition, maybe to the same status
{'reply', Reply :: term(), NewState :: term()} | % gen_server模式时快速响应进入消息接收
{'noreply', NewState :: term()} | % gen_server模式时快速响应进入消息接收
{'reply', Reply :: term(), NewState :: term(), Options :: hibernate | {doAfter, Args}} | % gen_server模式时快速响应进入消息接收
{'noreply', NewState :: term(), Options :: hibernate | {doAfter, Args}} | % gen_server模式时快速响应进入循环
{'nextStatus', NextStatus :: term(), NewState :: term()} | % {next_status,NextStatus,NewData,[]}
{'nextStatus', NextStatus :: term(), NewState :: term(), Actions :: [eventAction(), ...]} | % Status transition, maybe to the same status
commonCallbackResult(eventAction()).
-type afterCallbackResult() ::
{'nextStatus', NextStatus :: term(), NewState :: term()} | % {next_status,NextStatus,NewData,[]}
{'nextStatus', NextStatus :: term(), NewState :: term(), Actions :: [afterAction(), ...]} | % Status transition, maybe to the same status
{'noreply', NewState :: term()} | % gen_server模式时快速响应进入消息接收
{'noreply', NewState :: term(), Options :: hibernate} | % gen_server模式时快速响应进入消息接收
{'nextStatus', NextStatus :: term(), NewState :: term()} | % {next_status,NextStatus,NewData,[]}
{'nextStatus', NextStatus :: term(), NewState :: term(), Actions :: [afterAction(), ...]} | % Status transition, maybe to the same status
{'noreply', NewState :: term()} | % gen_server模式时快速响应进入消息接收
{'noreply', NewState :: term(), Options :: hibernate} | % gen_server模式时快速响应进入消息接收
commonCallbackResult(afterAction()).
-type enterCallbackResult() ::
commonCallbackResult(enterAction()).
-type commonCallbackResult(ActionType) ::
{'keepStatus', NewState :: term()} | % {keep_status,NewData,[]}
{'keepStatus', NewState :: term(), Actions :: [ActionType]} | % Keep status, change data
'keepStatusState' | % {keep_status_and_data,[]}
{'keepStatusState', Actions :: [ActionType]} | % Keep status and data -> only actions
{'repeatStatus', NewState :: term()} | % {repeat_status,NewData,[]}
{'repeatStatus', NewState :: term(), Actions :: [ActionType]} | % Repeat status, change data
'repeatStatusState' | % {repeat_status_and_data,[]}
{'repeatStatusState', Actions :: [ActionType]} | % Repeat status and data -> only actions
'stop' | % {stop,normal}
{'stop', Reason :: term()} | % Stop the server
{'stop', Reason :: term(), NewState :: term()} | % Stop the server
{'stopReply', Reason :: term(), Replies :: [replyAction(), ...]} | % Reply then stop the server
{'stopReply', Reason :: term(), Replies :: [replyAction(), ...], NewState :: term()}. % Reply then stop the server
{'keepStatus', NewState :: term()} | % {keep_status,NewData,[]}
{'keepStatus', NewState :: term(), Actions :: [ActionType]} | % Keep status, change data
'keepStatusState' | % {keep_status_and_data,[]}
{'keepStatusState', Actions :: [ActionType]} | % Keep status and data -> only actions
{'repeatStatus', NewState :: term()} | % {repeat_status,NewData,[]}
{'repeatStatus', NewState :: term(), Actions :: [ActionType]} | % Repeat status, change data
'repeatStatusState' | % {repeat_status_and_data,[]}
{'repeatStatusState', Actions :: [ActionType]} | % Repeat status and data -> only actions
'stop' | % {stop,normal}
{'stop', Reason :: term()} | % Stop the server
{'stop', Reason :: term(), NewState :: term()} | % Stop the server
{'stopReply', Reason :: term(), Replies :: [replyAction(), ...]} | % Reply then stop the server
{'stopReply', Reason :: term(), Replies :: [replyAction(), ...], NewState :: term()}. % Reply then stop the server
%%
%% gen_server init返回定时时间 Actions返回定时动作
@ -200,8 +200,7 @@
%% init返回actions包含 doAfter enter调用后
%% enter调用后调用该函数
%%
%% gen_server handle_continue回调 TODO
%% gen_server handle_continue回调
-callback handleAfter(AfterArgs :: term(), Status :: term(), State :: term()) ->
afterCallbackResult().
@ -217,7 +216,7 @@
-callback handleInfo(EventContent :: term(), Status :: term(), State :: term()) ->
eventCallbackResult().
%% Onevent actions nextEvent产生的超时事件 call cast info
%% Onevent actions nextEvent产生的超时事件 call cast info
%% erlang:start_timer生成超时事件发送的消息 gen_ipc定时器关键字重合
-callback handleOnevent(EventType :: term(), EventContent :: term(), Status :: term(), State :: term()) ->
eventCallbackResult().
@ -225,7 +224,7 @@
%% gen_event模式下
%%
%% gen_ipc来分组管理 gen_ipc进程管理 N
%% gen_event模式时 noticy
%% gen_event模式时 notify
-callback handleEpmEvent(EventContent :: term(), Status :: term(), State :: term()) ->
eventCallbackResult().
@ -347,6 +346,7 @@ stop(ServerRef) ->
stop(ServerRef, Reason, Timeout) ->
gen:stop(ServerRef, Reason, Timeout).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% start stop API end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% gen callbacks start %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
doModuleInit(Module, Args) ->
try
@ -383,13 +383,13 @@ init_it(Starter, Parent, ServerRef, Module, Args, Opts) ->
{'EXIT', Class, Reason, Stacktrace} ->
gen:unregister_name(ServerRef),
proc_lib:init_ack(Starter, {error, Reason}),
error_info(Class, Reason, Stacktrace, #cycleData{module = Module}, un_init, un_init, Debug, []),
error_info(Class, Reason, Stacktrace, #cycleData{module = Module}, 'Sun_init', '$un_init', Debug, []),
erlang:raise(Class, Reason, Stacktrace);
_Ret ->
gen:unregister_name(ServerRef),
Error = {badReturnFrom_doModuleInit, _Ret},
proc_lib:init_ack(Starter, {error, Error}),
error_info(error, Error, ?STACKTRACE(), #cycleData{module = Module}, un_init, un_init, Debug, []),
error_info(error, Error, ?STACKTRACE(), #cycleData{module = Module}, '$un_init', '$un_init', Debug, []),
exit(Error)
end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% gen callbacks end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@ -408,10 +408,10 @@ enter_loop(Module, Status, State, Opts, ServerOrActions) ->
end.
-spec enter_loop(Module :: module(), Status :: term(), State :: term(), Opts :: [enterLoopOpt()], Server :: serverName() | pid(), Actions :: [eventAction()]) -> no_return().
enter_loop(Module, Status, State, Opts, Server, Actions) ->
enter_loop(Module, Status, State, Opts, ServerName, Actions) ->
is_atom(Module) orelse error({atom, Module}),
Parent = gen:get_parent(),
Name = gen:get_proc_name(Server),
Name = gen:get_proc_name(ServerName),
Debug = gen:debug_options(Name, Opts),
HibernateAfterTimeout = gen:hibernate_after(Opts),
loopEntry(Parent, Debug, Module, Name, HibernateAfterTimeout, Status, State, Actions).
@ -1923,7 +1923,7 @@ error_info(Class, Reason, Stacktrace, #cycleData{isEnter = IsEnter, postponed =
#{
domain => [otp],
report_cb => fun gen_ipc:format_log/1,
error_logger => #{tag=>error}}
error_logger => #{tag => error}}
).
clientStacktrace([]) ->
@ -1931,16 +1931,12 @@ clientStacktrace([]) ->
clientStacktrace([{{call, {Pid, _Tag}}, _Req} | _]) when is_pid(Pid) ->
if
node(Pid) =:= node() ->
case
process_info(Pid, [current_stacktrace, registered_name])
of
case process_info(Pid, [current_stacktrace, registered_name]) of
undefined ->
{Pid, dead};
[{current_stacktrace, Stacktrace},
{registered_name, []}] ->
[{current_stacktrace, Stacktrace}, {registered_name, []}] ->
{Pid, {Pid, Stacktrace}};
[{current_stacktrace, Stacktrace},
{registered_name, Name}] ->
[{current_stacktrace, Stacktrace}, {registered_name, Name}] ->
{Pid, {Name, Stacktrace}}
end;
true ->
@ -2089,11 +2085,7 @@ format_status_default(Opt, Status_State) ->
%%---------------------------------------------------------------------------
%% Format debug messages. Print them as the call-back module sees
%% them, not as the real erlang messages. Use trace for that.
%%---------------------------------------------------------------------------
-compile({inline, [sys_debug/3]}).
sys_debug(Debug, NameStatus, Entry) ->
sys:handle_debug(Debug, fun print_event/3, NameStatus, Entry).
%%--------------------------------------------------------------------------
print_event(Dev, SystemEvent, Name) ->
case SystemEvent of

+ 6
- 0
src/gen_tcm.erl Datei anzeigen

@ -0,0 +1,6 @@
-module(gen_tcm).
-callback newConnect(Sock :: gen_tcp:socket(), Args :: term) ->
ignore |
{ok, Pid :: pid()} |
{error, Reason :: term()}.

Laden…
Abbrechen
Speichern