以前写了一个ranch的处理流程,http://www.cnblogs.com/tudou008/p/5197314.html ,就只有一张图,不是很清晰,现在有空做个源码分析。

ranch的源码(版本v1.2.1 下载链接https://github.com/ninenines/ranch.git

我们从一个最简单的例子开始 tcp_echo

 [root@erlang004 ranch-master]# pwd
/home/erlang/ranch-master
[root@erlang004 ranch-master]# ll -R examples/tcp_echo/
examples/tcp_echo/:
total
-rw-rw-r-- erlang erlang Jan : Makefile
-rw-rw-r-- erlang erlang Jan : README.md
-rw-rw-r-- erlang erlang Jan : relx.config
drwxrwxr-x erlang erlang May : src examples/tcp_echo/src:
total
-rw-rw-r-- erlang erlang Jan : echo_protocol.erl
-rw-rw-r-- erlang erlang Jan : tcp_echo_app.erl
-rw-rw-r-- erlang erlang Jan : tcp_echo.app.src
-rw-rw-r-- erlang erlang Jan : tcp_echo_sup.erl
首先查看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.
可以看到这里,启动了ranch:start_listener/6
而且后面启动了tcp_echo_sup:start_link/0,我们先看看tcp_echo_sup做了什么
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明显没有做任何业务,下面我们来详细查看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]). %...... 省略若干行 -spec start_listener(ref(), non_neg_integer(), module(), any(), module(), any())
-> supervisor:startchild_ret().
start_listener(Ref, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts)
when is_integer(NbAcceptors) andalso is_atom(Transport)
andalso is_atom(Protocol) ->
_ = code:ensure_loaded(Transport),
%% @todo Remove in Ranch 2.0 and simply require ssl.
_ = ensure_ssl(Transport),
case erlang:function_exported(Transport, name, 0) of
false ->
{error, badarg};
true ->
Res = supervisor:start_child(ranch_sup, child_spec(Ref, NbAcceptors,
Transport, TransOpts, Protocol, ProtoOpts)),
Socket = proplists:get_value(socket, TransOpts),
case Res of
{ok, Pid} when Socket =/= 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
%% will be no way to 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 when you change
%%% the controlling process of a listen socket because of a bug.
%%% The bug will be fixed in R16.
catch Transport:controlling_process(Socket, AcceptorsSup);
_ ->
ok
end,
Res
end.
%...... 省略若干行
 
start_listener在这里开始,
对比例子里面的参数发现,对应的值和意义如下
Ref,           :tcp_echo         表示应用的标记
NbAcceptors,      :1 应用启动的进程数(就是后面的ranch_acceptor的个数)
Transport,        :ranch_tcp     传输层的模块(ranch_tcp或者ranch_ssl,可以用户定义)
TransOpts,        :[{port, 5555}] 传输层的参数
Protocol,        :echo_protocol 应用层的处理模块,一般用户根据ranch_protocol编写
ProtoOpts       : []         应用层参数定义
这时ranch才慢慢走进我们的视野,下面我们慢慢分析.。。。。(未完待续)。
												

ranch 源码分析(一)的更多相关文章

  1. ranch 源码分析(完)

    接上 ranch 源码分析(三) 在上一次,根据ranch源码把大概流程理了一遍,下面我们将一些细节解释一下. ranch只是一个服务的框架,它提供了传输层协议代码(ranch_tcp 和ranch_ ...

  2. ranch 源码分析(三)

    接上ranch 源码分析(二) 上次讲到了ranch_conns_sup和ranch_acceptors_sup这2个ranch的核心模块,我们接着分析 首先查看ranch_conns_sup.erl ...

  3. ranch 源码分析(二)

    接上ranch 源码分析(一) 上次讲到了ranch.erl的start_listener函数,下面我们详细分析下这个函数 -module(ranch). %...... 省略若干行 -spec st ...

  4. cowboy源码分析(一)

    前段时间导读了ranch的源码,具体见ranch 源码分析(一), 现在整理了下ranch框架下经典应用cowboy. 源码地方:https://github.com/ninenines/cowboy ...

  5. cowboy源码分析(二)

    接 cowboy源码分析(一) 下面我们重点看看cowboy_protocol.erl代码 -module(cowboy_protocol). %% API. -export([start_link/ ...

  6. ABP源码分析一:整体项目结构及目录

    ABP是一套非常优秀的web应用程序架构,适合用来搭建集中式架构的web应用程序. 整个Abp的Infrastructure是以Abp这个package为核心模块(core)+15个模块(module ...

  7. HashMap与TreeMap源码分析

    1. 引言     在红黑树--算法导论(15)中学习了红黑树的原理.本来打算自己来试着实现一下,然而在看了JDK(1.8.0)TreeMap的源码后恍然发现原来它就是利用红黑树实现的(很惭愧学了Ja ...

  8. nginx源码分析之网络初始化

    nginx作为一个高性能的HTTP服务器,网络的处理是其核心,了解网络的初始化有助于加深对nginx网络处理的了解,本文主要通过nginx的源代码来分析其网络初始化. 从配置文件中读取初始化信息 与网 ...

  9. zookeeper源码分析之五服务端(集群leader)处理请求流程

    leader的实现类为LeaderZooKeeperServer,它间接继承自标准ZookeeperServer.它规定了请求到达leader时需要经历的路径: PrepRequestProcesso ...

随机推荐

  1. 【摘】Fiddler工具使用介绍

    摘自:https://www.cnblogs.com/miantest/p/7289694.html Fiddler基础知识 Fiddler是强大的抓包工具,它的原理是以web代理服务器的形式进行工作 ...

  2. js--单选按钮赋值

    var sex='${userInfo.sex}'; if(sex=="女"){ $("input[name=sex][value='女']").attr(&q ...

  3. Oracle实用操作

    查询用户下所有表:select * from tab; 删除表: drop table 表名; 但是删除表后还是会查询到BIN开头的垃圾表,drop后的表存在于回收站: 清空回收站所有表:  purg ...

  4. 访问GitLab的PostgreSQL数据库,查询、修改、替换等操作

    1.登陆gitlab的安装服务查看配置文件 cat /var/opt/gitlab/gitlab-rails/etc/database.yml production: adapter: postgre ...

  5. 关于容器类型数据的强转一共:str() list() set() tuple() dict() 都可以转换成对应的数据类型 /Number 数据类型的强转一共: int() bool() flaot() complex() 都可以转换成对应的数据类型

    # ###强制转换成字典类型 # 多级容器数据:该类型是容器数据,并且里面的元素还是容器类型数据 # ###二级容器 # 二级列表 listvar = [1,3,4,5,[6,7,8,9]] res ...

  6. openshift 容器云从入门到崩溃之十《容器监控-数据展示》

    POD资源历史曲线(CPU.内存.网络) 监控方案heapster+hawkular-metrics+hawkular-cassandra heapster负责收集数据 hawkular-cassan ...

  7. 腾讯云主机及CentOS7.2简单上手体验

    前段时间拜读了崔庆才老师的<Python③网络爬虫开发实战>受益良多,对于初学爬虫的新手来说,本书真可谓是通俗易懂,非常适合新手入门.但是受制于没有服务器环境,书中很多例子难以模拟!最近正 ...

  8. 深入理解Java虚拟机3-chap4-5-斗之气10段

    一.虚拟机性能监控与故障处理 1.JDK的命令行工具:对jdk/lib/tools.jar的薄包装,Linux下可能是Shell编写,执行类似于Linux中的命令 2.可视化工具JConsole 打开 ...

  9. 50.JQ---jQuery 常用小技巧

    1. 禁止右键点击 $(document).ready(function(){ $(document).bind("contextmenu",function(e){ return ...

  10. 关于UI适配的文档

    第一部分:原理 (1)根据当前屏幕尺寸与开发预设屏幕尺寸尺寸得出以下参数. 1 XRatio:当前屏幕尺寸与开发尺寸的X轴比例 2 YRtaio:当前屏幕尺寸与开发尺寸的Y轴比例 3minRatio: ...