上一篇分析之后,本来第二部分分析是想分析start之后的逻辑的,这样会让人比较快速的理解skynet框架。但想想还是顺着代码启动的思路写下去会比较好,我觉得这样我自己更容易理解。
bootstrap是引导程序的意思,在skynet中,的确也是做了服务器工作的前置任务。
再skynet_start.c中
//启动logger服务
struct skynet_context *ctx = skynet_context_new(config->logservice, config->logger);
if (ctx == NULL) {
fprintf(stderr, "Can't launch %s service\n", config->logservice);
exit(1);
}
skynet_handle_namehandle(skynet_context_handle(ctx), "logger");
//启动配置中的bootstrap服务
bootstrap(ctx, config->bootstrap);
//调用start传入配置线程数量
start(config->thread);
查询配置
可以得知,传入的参数是"snlua bootsrtap",再看到函数的实现
static void
bootstrap(struct skynet_context * logger, const char * cmdline) {
int sz = strlen(cmdline);
char name[sz+1];
char args[sz+1];
sscanf(cmdline, "%s %s", name, args);
//name = snlua, args = bootstrap
struct skynet_context *ctx = skynet_context_new(name, args);
if (ctx == NULL) {
skynet_error(NULL, "Bootstrap error : %s\n", cmdline);
skynet_context_dispatchall(logger);
exit(1);
}
}
接下来看到skynet_context_new方法,在skynet_server.c
struct skynet_context *
skynet_context_new(const char * name, const char *param) {
//先查询snlua模块,这是一个C写的服务,在skynet_start.c中
//skynet_statc方法,skynet_module_init(config->module_path)进行初始化
struct skynet_module * mod = skynet_module_query(name);
if (mod == NULL)
return NULL;
//分析1
//这里创建一个新的lua虚拟机,所以会有各个服务的隔离
void *inst = skynet_module_instance_create(mod);
if (inst == NULL)
return NULL;
struct skynet_context * ctx = skynet_malloc(sizeof(*ctx));
CHECKCALLING_INIT(ctx)
ctx->mod = mod;
ctx->instance = inst;
ctx->ref = 2;
ctx->cb = NULL;
ctx->cb_ud = NULL;
ctx->session_id = 0;
ctx->logfile = NULL;
ctx->init = false;
ctx->endless = false;
ctx-&g