当前位置: 首页> 科技> 名企 > skynet 实操篇

skynet 实操篇

时间:2025/7/13 10:53:59来源:https://blog.csdn.net/Vjunjun/article/details/140779278 浏览次数:0次

文章目录

  • 概述
    • demo
    • 启动文件skynet_start
    • 配置文件
    • main.lua
    • start函数
    • thread_worker
    • skynet_context_message_dispatch
    • skynet_mq_pop
    • dispatch_message
  • 小结

概述

上一篇写完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 = 64skynet.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")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,结束。

关键字:skynet 实操篇

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: