包含标签 skynet 的文章

层级时间轮 | skynet 定时器

skynet定时器

要解析一个程序代码,先了解数据结构,这是基础,再看函数。 拿skynet定时器举例子。

数据结构

//定时器事件 用于抛出定时器事件到消息队列里。理解这个数据结构需要先了解skynet的框架原理,
//不理解这个数据结构也不影响下面的论述
struct timer_event {
	int32_t handle;
	int session;
};

//定时器节点
struct timer_node {
	struct timer_node *next;
	uint32_t expire;
};

//定时器链表
struct link_list {
	struct timer_node head;
	struct timer_node *tail;
};

//层级时间轮
//存放所有定时器的地方
struct timer {
	struct link_list near[TIME_NEAR]; //最近的定时器
	struct link_list t[4][TIME_LEVEL];//更久远的定时器
	struct spinlock lock;           //全局锁
	uint32_t time;                  //当前滴答数
	uint32_t starttime;             //程序开始时间 绝对时间 时间戳,单位 s 秒
	uint64_t current;               //当前时间 相对时间 1cs 厘秒 =  10ms 毫秒
	uint64_t current_point;         //系统(pc)运行时间 相对时间 单位: cs 厘秒
};

上面定时器的基本数据结构了解了,再来看下面的函数, 基本思路是一样的。

……

阅读全文

层级时间轮 | skynet 定时器

skynet定时器

要解析一个程序代码,先了解数据结构,这是基础,再看函数。 拿skynet定时器举例子。

数据结构

//定时器事件 用于抛出定时器事件到消息队列里。理解这个数据结构需要先了解skynet的框架原理,
//不理解这个数据结构也不影响下面的论述
struct timer_event {
	int32_t handle;
	int session;
};

//定时器节点
struct timer_node {
	struct timer_node *next;
	uint32_t expire;
};

//定时器链表
struct link_list {
	struct timer_node head;
	struct timer_node *tail;
};

//层级时间轮
//存放所有定时器的地方
struct timer {
	struct link_list near[TIME_NEAR]; //最近的定时器
	struct link_list t[4][TIME_LEVEL];//更久远的定时器
	struct spinlock lock;           //全局锁
	uint32_t time;                  //当前滴答数
	uint32_t starttime;             //程序开始时间 绝对时间 时间戳,单位 s 秒
	uint64_t current;               //当前时间 相对时间 1cs 厘秒 =  10ms 毫秒
	uint64_t current_point;         //系统(pc)运行时间 相对时间 单位: cs 厘秒
};

上面定时器的基本数据结构了解了,再来看下面的函数, 基本思路是一样的。

……

阅读全文

Skynet源码赏析四 | 消息调度

服务间消息流转

发送消息

数据结构message_queue *q是次级消息队列,每个服务都有与之绑定的唯一一个次级消息队列。当我们在lua层,调用skynet.send(...), 或者调用skynet.call(...)函数发送消息,其实调用的都是c.send(...),实际上调用的是c接口:

……

阅读全文

Skynet源码赏析三 | 服务启动

skynet启动一个C服务

查看怎么启动C服务的最好的办法是打断点,看源码。

启动logger日志服务

我拿日志服务来举例子,怎么启动日志服务的呢?

  1. 传参数name = "logger", param = nullstruct skynet_context *ctx = skynet_context_new(config->logservice, config->logger);;
  2. 从modules服务模块中取出名字为 logger的服务, struct skynet_module * result = _query(name);;
  3. 如果找不到则从cpath目录中找对应的.so文件,打开服务,并且存储到modules模块中。使用的函数是static void * _try_open(struct modules *m, const char * name);
  4. 创建服务实例m->create(),放到上下文skynet_contextinst中;
  5. skynet_context放到skynet_context list中, skynet_handle_register(ctx);;
  6. 创建logger服务对应的次级消息队列struct message_queue *queue,并且把消息队列push到全局消息队列中skynet_mq_create(ctx->handle);skynet_globalmq_push(queue);;
  7. 服务实例初始化m->init(),int r = skynet_module_instance_init(mod, inst, ctx, param);

总结下来,启动一个服务的过程就是,1.获取服务;2.创建服务实例;3.创建服务对应的上下文,并且把上下文放到skynet_context list管理模块中;4.绑定次级消息队列与对应的服务;5.服务初始化; 这五步过程。

……

阅读全文

Skynet源码赏析二 | 基础数据结构

module管理模块

我们所写的C服务编译成.so文件后放在cpath变量路径下,程序会加载路径cpath = root.."cservice/?.so"下的.so文件,通过static void * _try_open(struct modules *m, const char * name)函数第一次打开,然后被加载到全局变量static struct modules * M中。

……

阅读全文

Skynet源码赏析一 | 启动初始化的过程

入口文件

skynet_main.c

加载配置

struct skynet_config config;
config.thread =  optint("thread",8);
config.module_path = optstring("cpath","./cservice/?.so");
config.harbor = optint("harbor", 1);
config.bootstrap = optstring("bootstrap","snlua bootstrap");
config.daemon = optstring("daemon", NULL);
config.logger = optstring("logger", NULL);
config.logservice = optstring("logservice", "logger");
config.profile = optboolean("profile", 1);

从上述代码可以看到,配置都已经加载到变量struct skynet_config config;中了。

初始化

  1. 初始化skynet_context管理模块, skynet_handle_init(config->harbor);;
  2. 初始化static struct global_queue *Q消息队列, skynet_mq_init();;
  3. 初始化static struct modules * M管理模块, skynet_module_init(config->module_path);;
  4. 初始化定时器, skynet_timer_init();;

启动C服务

  1. 启动logger日志服务, struct skynet_context *ctx = skynet_context_new(config->logservice, config->logger);;
  2. 启动 snlua服务, ``;
  3. 通过snlua服务启动boostrap服务, bootstrap(ctx, config->bootstrap);;
  4. 启动线程:
  • 监听线程,检测线程是否阻塞。 create_thread(&pid[0], thread_monitor, m);;
  • 启动定时器线程。create_thread(&pid[1], thread_timer, m);;
  • 启动socket线程。create_thread(&pid[2], thread_socket, m);
  • 启动工作线程,用于lua服务消息的调度。create_thread(&pid[i+3], thread_worker, &wp[i]);;

启动lua服务

默认启动的lua服务,在boostrap.lua文件中可以看到都是通过snlua服务启动的。

……

阅读全文

Skynet | lua-protobuf

概述

在最新的skynet中我使用的是云风的pbc库来解析protobuf,该项目中的一个文件protobuf.lua 使用了module语法在lua5.1版本中已经删除,最新的skynet是lua5.4版本,于是我找到了解析protobuf的lua库–lua-protobuf。在最新的skynet中,集成lua-protobuf

……

阅读全文