Skynet 进程启动
初始化配置
skynet 进程启动时需要指定配置文件,启动后读取配置文件中的内容并存储在内存中。配置文件格式是 k = v 且 k 必须是字符串而 v 必须是字符串或者 lua boolean 类型。
通过 L 读取配置,随后把配置存储在 skynet_env.c 模块中新创建的 lua state 中,每个配置项都是一个全局变量。
1// skynet_main.c 2int main { 3 skynet_env_init(); 4 err = lua_pcall(L, 1, 1, 0); 5 if (err) { 6 fprintf(stderr,"%s\n",lua_tostring(L,-1)); 7 lua_close(L); 8 return 1; 9 } 10 _init_env(L); 11}
创建第一个服务
skynet 进程启动时,创建的第一个服务就是日志服务,服务名是 config-logservice 。skynet 提供的默认服务是 service_logger.c 。由于此服务被直接通过 skynet_context_new 创建,如果想提供自定义日志服务,需要写个 C 服务替换 service_logger.c 。
如果日志服务创建失败,则整个进程退出。
1// skynet_start.c 2void skynet_start { 3 struct skynet_context *ctx = skynet_context_new(config->logservice, config->logger); 4 if (ctx == NULL) { 5 fprintf(stderr, "Can't launch %s service\n", config->logservice); 6 exit(1); 7 } 8 skynet_handle_namehandle(skynet_context_handle(ctx), "logger"); 9}
创建 skynet 需要的 lua 服务和配置中 start 服务
启动 config->bootstrap lua 服务,进行初始化,创建 skynet 需要的 lua 服务。
创建 launcher 服务,管理之后通过 skynet.newservice 创建的服务。
创建 service_mgr 服务,提供管理功能。
创建配置中的 start 服务,供业务逻辑进行初始化。 最后该服务退出。
1// skynet_start.c 2void skynet_start { 3 bootstrap(ctx, config->bootstrap); // snlua bootstrap 4} 5 6 7-- service/bootstrap.lua 8skynet.start(function() 9 -- 创建 launcher 服务 10 local launcher = assert(skynet.launch("snlua","launcher")) 11 skynet.name(".launcher", launcher) 12 -- 创建配置中的 start 服务 13 pcall(skynet.newservice,skynet.getenv "start" or "main") 14 -- 提供管理的服务 15 skynet.newservice "service_mgr" 16 -- 创建完其他服务后,本服务退出 17 skynet.exit() 18end)
创建子线程
skynet 目前创建了四种线程,分别是 thread_monitor, thread_timer, thread_socket, thread_worker 。消息的调度和派发是在 thread_worker 线程中执行的,且 worker 线程根据权重 weight 决定处理队列中消息的数量。
1static void start { 2 // 创建监控,定时器,网络线程 3 create_thread(&pid[0], thread_monitor, m); 4 create_thread(&pid[1], thread_timer, m); 5 create_thread(&pid[2], thread_socket, m); 6 7 // 创建 worker 线程 8 static int weight[] = { 9 -1, -1, -1, -1, 0, 0, 0, 0, 10 1, 1, 1, 1, 1, 1, 1, 1, 11 2, 2, 2, 2, 2, 2, 2, 2, 12 3, 3, 3, 3, 3, 3, 3, 3, }; 13 struct worker_parm wp[thread]; 14 for (i=0;i<thread;i++) { 15 wp[i].m = m; 16 wp[i].id = i; 17 if (i < sizeof(weight)/sizeof(weight[0])) { 18 wp[i].weight= weight[i]; 19 } else { 20 wp[i].weight = 0; 21 } 22 create_thread(&pid[i+3], thread_worker, &wp[i]); 23 } 24 25 // 等待所有线程执行完毕后,则退出 26 for (i=0;i<thread+3;i++) { 27 pthread_join(pid[i], NULL); 28 } 29}
skynet_start.c start 函数执行到 pthread_join 时,skynet 初始化完毕。
注意:在 worker 线程工作之前,框架就已经创建了服务并且有向服务发送消息。