skynet examples深入学习

来源:互联网 发布:c语言函数查询工具 编辑:程序博客网 时间:2024/05/22 09:54
examples/skynet.lua   skynet.start(function()skynet.error("Server start")skynet.uniqueservice("protoloader")if not skynet.getenv "daemon" thenlocal console = skynet.newservice("console")endskynet.newservice("debug_console",8000)skynet.newservice("simpledb")local watchdog = skynet.newservice("watchdog")skynet.call(watchdog, "lua", "start", {port = 8888,maxclient = max_client,nodelay = true,})skynet.error("Watchdog listen on", 8888)skynet.exit()end)

config 中配置 start = "main" 所以会从package.path中找到这个main.lua (万一有多个main.lua是不是加载第一个找到的呢?)

执行这个脚本前,启动了如下几个服务:

logger服务;

newservice( snlua, bootstrap):

     加载 snlua module;

     使用snlua动态库中的 create init init_cb几个流程完成了: 加载 bootstrap.lua,并执行一次的过程。

bootstrap.lua:使命:

     1. 使用skynet.start() 注册了一个服务启动函数,并投递一个请求初始化的消息,等待框架从dispatch_message那边回调。

     2. bootstrap中启动 launcher服务;
               2.1 config.harbor_id == 0 && config.standalone==nil : 启动cdummy 服务(name:".cslave"), datacenterd服务

               2.2 config.harbor_id [1,255]: cmaster服务 ; cslave服务(启动harbor服务), 若standalone有配置,启动 datacenterd服务

     注:standalone这家伙和字面意义不大一样。

  • standalone 如果把这个 skynet 进程作为主进程启动(skynet 可以由分布在多台机器上的多个进程构成网络),那么需要配置standalone 这一项,表示这个进程是主节点,它需要开启一个控制中心,监听一个端口,让其它节点接入。

     3.启动service_mgr服务, 命名 ".service"

     4.启动 config.start 服务。 可配置, 若未配置, 默认启动 "main.lua" 中的服务

     至此,使命完成,自我终结, skynet.exit() 。

-------------------------------------------------------------------------------------------------------------

正式进入main.lua流程:

1.启动protoloader 服务: 把开源的sproto库 require进来,并加载协议文件。

2.debug_console

3.simpledb服务。

4.watchdog服务,随后立马给自己发了继init(skynet.start 发出)之后的第二条消息start
至此,main服务使命结束,自我终结 skynet.exit(),
世界交给了 watchdog, debug_console, simpledb 三位主演.幕后的的先不关心吧。

---------------------------------------------------------------------------------------------------------------





原创粉丝点击