Ranch Source Analysis (i)

Source: Internet
Author: User

Previously wrote a ranch process, http://www.cnblogs.com/tudou008/p/5197314.html, there is only one picture, not very clear, now have time to do a source analysis.

Ranch source code (version v1.2.1 download link https://github.com/ninenines/ranch.git)

Let's start with one of the simplest examples Tcp_echo

1[Email protected] ranch-master]#pwd2/home/erlang/ranch-Master3[Email protected] ranch-master]# Ll-r examples/tcp_echo/4examples/tcp_echo/:5Total -6-rw-rw-r--1Erlang Erlang AboutJan -  .: theMakefile7-rw-rw-r--1Erlang Erlang543Jan -  .: thereadme.md8-rw-rw-r--1Erlang Erlang -Jan -  .: theRelx.config9Drwxrwxr-x2Erlang Erlang4096May6  the: -srcTen  Oneexamples/tcp_echo/src: ATotal - --rw-rw-r--1Erlang Erlang572Jan -  .: theEcho_protocol.erl --rw-rw-r--1Erlang Erlang346Jan -  .: theTcp_echo_app.erl the-rw-rw-r--1Erlang Erlang284Jan -  .: thetcp_echo.app.src --rw-rw-r--1Erlang Erlang370Jan -  .: theTcp_echo_sup.erl
First View Tcp_echo_app.erl
%%feel free to use, reuse and abuse the code in this file.%%@private-Module(Tcp_echo_app).-behaviour (application).%%API.-Export([START/2]).-Export([STOP/1]).%%API.Start (_type, _args)-{OK, _}=Ranch:start_listener(Tcp_echo, 1, Ranch_tcp, [{port,5555}], Echo_protocol, []), Tcp_echo_sup:start_link (). Stop (_state)-OK.
Can see here, start the Ranch:start_listener
And after we started Tcp_echo_sup:start_link, let's see what Tcp_echo_sup did.
Tcp_echo_sup.erl
%%feel free to use, reuse and abuse the code in this file.%%@private-Module(tcp_echo_sup).-behaviour (supervisor).%%API.-Export([start_link/0]).%%supervisor.-Export([INIT/1]).%%API.-spec Start_link (){OK, pid ()}.start_link ()-Supervisor:start_link ({local,? MODULE},?MODULE, []).%%supervisor.init ([])-{OK, {{one_for_one,10, 10}, []}.
Tcp_echo_sup obviously did not do any business, let's look at the details below Ranch.erl

-Module(Ranch).-Export([START_LISTENER/6]).-Export([STOP_LISTENER/1]).-Export([CHILD_SPEC/6]).-Export([ACCEPT_ACK/1]).-Export([REMOVE_CONNECTION/1]).-Export([GET_ADDR/1]).-Export([GET_PORT/1]).-Export([GET_MAX_CONNECTIONS/1]).-Export([SET_MAX_CONNECTIONS/2]).-Export([GET_PROTOCOL_OPTIONS/1]).-Export([SET_PROTOCOL_OPTIONS/2]).-Export([FILTER_OPTIONS/3]).-Export([SET_OPTION_DEFAULT/3]).-Export([REQUIRE/1]).#...... Omit several lines-spec Start_listener (ref (), Non_neg_integer (), module (), any (), module (), any ())-Supervisor:startchild_ret (). Start_listener (Ref, Nbacceptors, Transport, transopts, Protocol, protoopts) whenIs_integer (nbacceptors)AndAlsoIs_atom (Transport)AndAlsoIs_atom (Protocol)    _ =code:ensure_loaded (Transport),%%@todo Remove in Ranch 2.0 and simply require SSL._ =Ensure_ssl (Transport), CaseErlang:function_exported (Transport, name, 0) of        false-{error, badarg}; true-Res=Supervisor:start_child (Ranch_sup, Child_spec (Ref, Nbacceptors, Transport, transopts, Protocol, Pro toopts)), Socket=proplists:get_value (socket, transopts), CaseRes of{OK, Pid} whenSocket =/= undefined%%Give ownership of the socket to Ranch_acceptors_sup                    %%To make sure the socket stays open as long as the                    %%Listener is alive. If The socket closes however there                    %%'ll be no it's recover because we don ' t know how                    %%To open it again.Children =Supervisor:which_children (Pid), {_, Acceptorssup, _, _}= Lists:keyfind (Ranch_acceptors_sup, 1, children),%%% note:the catch is here because SSL crashes                    %%% The controlling process of a listen socket because of a bug.                    %%% The bug would be a fixed in R16.                    Catchtransport:controlling_process (Socket, acceptorssup); _ -OKEnd, ResEnd.

#...... Omit several lines
The Start_listener starts here,
In comparison with the parameters found in the example, the corresponding values and meanings are as follows
Ref,: Tcp_echo         represents the applied tag
Nbacceptors, : 1 The number of processes initiated by the application (that is, the number of ranch_acceptor behind)
Transport: module of the RANCH_TCP Transport layer (RANCH_TCP or RANCH_SSL, can be user-defined)
Transopts, : [{port, 5555}] parameters of the transport layer
Protocol, : echo_protocol Application Layer Processing module, the general user according to Ranch_protocol writing
Protoopts: [] Application layer parameter definition
Then ranch slowly into our vision, below we slowly analysis ... (not to be continued).

Ranch Source Analysis (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.