skynet 实操篇

avatar
作者
猴君
阅读量:0

文章目录

概述

上一篇写完skynet入门篇,这一篇写点实操性质的。

demo

examples
对于一个开源框架,大部分都有他们自己的demo。先来看下这个examples下边有什么。

启动文件skynet_start

skynet_start
先看下这个函数,首先是配置文件,然后执行的是各种初始化,最后执行了start函数。

配置文件

配置文件
先看这个,start后边有个main;先看这个main.lua;看看里边有什么好玩的。

main.lua

local skynet = require "skynet" local sprotoloader = require "sprotoloader"  local max_client = 64  skynet.start(function() 	skynet.error("Server start") 	skynet.uniqueservice("protoloader") 	if not skynet.getenv "daemon" then 		local console = skynet.newservice("console") 	end 	skynet.newservice("debug_console",8000) 	skynet.newservice("simpledb") 	local watchdog = skynet.newservice("watchdog") 	local addr,port = skynet.call(watchdog, "lua", "start", { 		port = 8888, 		maxclient = max_client, 		nodelay = true, 	}) 	skynet.error("Watchdog listen on " .. addr .. ":" .. port) 	skynet.exit() end)   

看这个main,调用start函数,然后是启用服务protoloader,console服务,接下来是其他的几个服务。

start函数

lua里先调用start函数,看看这个里边有什么

start函数
这个里边东西还是有点多的。从大的方面,有几个线程:thread_monitor, thread_timer, thread_socket;然后就是thread_worker,这个线程是按权重分的。看看里边有什么。

thread_worker

thread_worker

skynet_context_message_dispatch 这个函数是个消息分发,分发完;执行的wait操作,等待激活。接下来看看消息分发。

skynet_context_message_dispatch

skynet_context_message_dispatch
这个就是从消息队列中取出消息,然后最后执行dispatch_message。

skynet_mq_pop

skynet_mq_pop

dispatch_message

dispatch_message

最后调用了

reserve_msg = ctx->cb(ctx, ctx->cb_ud, type, msg->session, msg->source, msg->data, sz); 

小结

这篇主要梳理了skynet的流程,从skynet_start到配置文件,然后是各种类的初始化等操作,start,thread_work,到最后消息分发结束之后的挂起;等等。这些才是skynet从开始到最后的一个流程,当然,examples下面还有很多其他的内容,都可以按照这个思路去分析分析;当然,有兴趣,可以一起来学习学习。OK,结束。

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!