erlang网络库
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

144 rader
4.5 KiB

  1. -module(ntPptAcceptor).
  2. -include("eNet.hrl").
  3. -include("ntCom.hrl").
  4. -compile(inline).
  5. -compile({inline_size, 128}).
  6. -export([
  7. start_link/5
  8. , handshake/3
  9. , init/1
  10. , handleMsg/2
  11. , init_it/2
  12. , system_code_change/4
  13. , system_continue/3
  14. , system_get_state/1
  15. , system_terminate/4
  16. ]).
  17. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% genActor start %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  18. -spec start_link(list(), timeout(), socket(), module(), [proc_lib:spawn_option()]) -> {ok, pid()}.
  19. start_link(SslOpts, HandshakeTimeout, LSock, ConMod, SpawnOpts) ->
  20. proc_lib:start_link(?MODULE, init_it, [self(), {SslOpts, HandshakeTimeout, LSock, ConMod}], infinity, SpawnOpts).
  21. init_it(Parent, Args) ->
  22. process_flag(trap_exit, true),
  23. modInit(Parent, Args).
  24. -spec system_code_change(term(), module(), undefined | term(), term()) -> {ok, term()}.
  25. system_code_change(State, _Module, _OldVsn, _Extra) ->
  26. {ok, State}.
  27. -spec system_continue(pid(), [], {module(), atom(), pid(), term()}) -> ok.
  28. system_continue(_Parent, _Debug, {Parent, State}) ->
  29. loop(Parent, State).
  30. -spec system_get_state(term()) -> {ok, term()}.
  31. system_get_state(State) ->
  32. {ok, State}.
  33. -spec system_terminate(term(), pid(), [], term()) -> none().
  34. system_terminate(Reason, _Parent, _Debug, _State) ->
  35. exit(Reason).
  36. modInit(Parent, Args) ->
  37. case init(Args) of
  38. {ok, State} ->
  39. proc_lib:init_ack(Parent, {ok, self()}),
  40. loop(Parent, State);
  41. {stop, Reason} ->
  42. proc_lib:init_ack(Parent, {error, Reason}),
  43. exit(Reason)
  44. end.
  45. loop(Parent, State) ->
  46. receive
  47. {system, From, Request} ->
  48. sys:handle_system_msg(Request, From, Parent, ?MODULE, [], {Parent, State});
  49. {'EXIT', Parent, Reason} ->
  50. exit(Reason);
  51. Msg ->
  52. case handleMsg(Msg, State) of
  53. {ok, NewState} ->
  54. loop(Parent, NewState);
  55. {stop, Reason} ->
  56. exit(Reason)
  57. end
  58. end.
  59. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% genActor end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  60. -record(state, {
  61. lSock
  62. , sslOpts
  63. , handshake_timeout
  64. , ref
  65. , conMod
  66. , sockMod
  67. }).
  68. -spec init(Args :: term()) -> ok.
  69. init({SslOpts, HandshakeTimeout, LSock, ConMod}) ->
  70. case prim_inet:async_accept(LSock, -1) of
  71. {ok, Ref} ->
  72. {ok, SockMod} = inet_db:lookup_socket(LSock),
  73. {ok, #state{lSock = LSock, sslOpts = SslOpts, handshake_timeout = HandshakeTimeout, ref = Ref, conMod = ConMod, sockMod = SockMod}};
  74. {error, Reason} ->
  75. ?ntErr("init prim_inet:async_accept error ~p~n", [Reason]),
  76. {stop, Reason}
  77. end.
  78. handleMsg({inet_async, LSock, Ref, Msg}, #state{lSock = LSock, sslOpts = SslOpts, handshake_timeout = HandshakeTimeout, ref = Ref, conMod = ConMod, sockMod = SockMod} = State) ->
  79. case Msg of
  80. {ok, Sock} ->
  81. %% make it look like gen_tcp:accept
  82. inet_db:register_socket(Sock, SockMod),
  83. try ConMod:newConn(Sock) of
  84. {ok, Pid} ->
  85. gen_tcp:controlling_process(Sock, Pid),
  86. Pid ! {?mSockReady, Sock, SslOpts, HandshakeTimeout},
  87. newAsyncAccept(LSock, State);
  88. {close, Reason} ->
  89. ?ntErr("handleMsg ConMod:newAcceptor return close ~p~n", [Reason]),
  90. catch port_close(Sock),
  91. newAsyncAccept(LSock, State);
  92. _Ret ->
  93. ?ntErr("ConMod:newAcceptor return error ~p~n", [_Ret]),
  94. {stop, error_ret}
  95. catch
  96. E:R:S ->
  97. ?ntErr("CliMod:newConnect crash: ~p:~p~n~p~n ~n ", [E, R, S]),
  98. newAsyncAccept(LSock, State)
  99. end;
  100. {error, closed} ->
  101. % ?ntErr("error, closed listen sock error ~p~n", [closed]),
  102. {stop, normal};
  103. {error, Reason} ->
  104. ?ntErr("listen sock error ~p~n", [Reason]),
  105. {stop, {lsock, Reason}}
  106. end;
  107. handleMsg(_Msg, State) ->
  108. ?ntErr("~p receive unexpected ~p msg: ~p", [?MODULE, self(), _Msg]),
  109. {ok, State}.
  110. newAsyncAccept(LSock, State) ->
  111. case prim_inet:async_accept(LSock, -1) of
  112. {ok, Ref} ->
  113. {ok, State#state{ref = Ref}};
  114. {error, Reason} ->
  115. ?ntErr("~p prim_inet:async_accept error ~p~n", [?MODULE, Reason]),
  116. {stop, Reason}
  117. end.
  118. handshake(Sock, SslOpts, Timeout) ->
  119. case ssl:handshake(Sock, SslOpts, Timeout) of
  120. {ok, _SslSock} = Ret ->
  121. Ret;
  122. {ok, SslSock, _Ext} -> %% OTP 21.0
  123. {ok, SslSock};
  124. {error, _} = Err -> Err
  125. end.