skynet start the process and call service

 

Write pictures described here 
3. Basic principles 
3.1 startup process 
Start Process 
1.skynet-src / skynet_main.c This is the main () function is located, is mainly set about lua environment, the default configuration, open the config configuration file, and modify the default configuration. Last call skynet_start () function, which skynet_start.c file in. 
2.skynet-src / skynet_start.c this document was initialized Skynet various modules, including harbor node, handle the service ID, the message queue MQ, module loading DLL, clock Timer, socket and socket load some service logger logs service, master primary service, harbor node services, snlua lua loading service module; and finally start thread includes several _moitor, _timer, _socket thread starts and the number of threads in accordance with the n-th working. 
Use snlua load the first customer service 
Load User Service

ctx = skynet_context_new(“snlua”, config->start);

// use snlua load config-> start the service, config-> start point to the configuration file config start = "main" line.

3.2 call service

This is mainly achieved in the C language, the code skynet_context_new skynet-src / skynet_server.c of () function, this function is mainly to instantiate service dynamic link library "_create ()" and "_init ()", as well as to service to create a private message queue. And filled into struct skynet_context structure. 
skynet_send (), send messages to the queue, waiting callback function and services.

4.服务 
4.1 C语言写服务 
service-src/server_logger.c 简介 
1、logger_create() 创建服务 
2、logger_init() 初始化服务 
3、logger_release() 释放服务 
4、_logger() 回调函数,这个名字由skynet_callback(_logger)决定。

server_logger.c

Write pictures described here
一个服务中的这四个函数就是Skynet 服务动态链接库的API。这里的logger_create(),其中logger表示的是logger.so的名字,Skynet的module会提取logger.so的名字作并加上”_create”来识别服务中的函数地址。函数的执行顺序是先执行”_create()”再执行”_init()”。而”_release”由skynet_context_release()调用来释放。而回调函数这是其他服务调用这个服务时会去调用它进行处理。服务的主要任务实现就在回调函数中处理。

注:回调函数简介 
1.基本定义: 
在计算机程序设计中,回调函数,或简称回调,是指通过函数参数传递到其它代码的,某一块可执行代码的引用。这一设计允许了底层代码调用在高层定义的子程序。 
2.回调机制: 
⑴定义一个回调函数; 
⑵提供函数实现的一方在初始化时,将回调函数的函数指针注册给调用者; 
⑶当特定的事件或条件发生的时候,调用者使用函数指针调用回调函数对事件进行处理。 
3.使用时机 
先假设有这样一种情况:我们要编写一个库,它提供了某些排序算法的实现(如冒泡排序、快速排序、shell排序、shake排序等等),为了能让库更加通 
用,不想在函数中嵌入排序逻辑,而让使用者来实现相应的逻辑;或者,能让库可用于多种数据类型(int、float、string),此时,该怎么办呢?可以使用函数指针,并进行回调。 
回调可用于通知机制。例如,有时要在A程序中设置一个计时器,每到一定时间,A程序会得到相应的通知,但通知机制的实现者对A程序一无所知。那么,就需一个具有特定原型的函数指针进行回调,通知A程序事件已经发生。实际上,API使用一个回调函数SetTimer()来通知计时器。如果没有提供回调函数,它还会把一个消息发往程序的消息队列。 
另一个使用回调机制的API函数是EnumWindow(),它枚举屏幕上所有的顶层窗口,每个窗口都可以通过它调用另一个程序提供的函数,并传递窗口的处理程序。例如:如果被调用者返回一个值,就继续进行迭代;否则,退出。EnumWindow()并不关心被调用者在何处,也不关心被调用者用它传递的处理程序做了什么,它只关心返回值,因为基于返回值,它将继续执行或退出。

4.2 lua language services 
with the Lua language used to write a service, the most simple example in test / testsocket.lua this service, it is the main achievement of retroreflective (echo) function, what the client sends data to the server, the server is like a mirror the same data is reflected back to the client. 
skynet.start () to start a service

Examples of analysis: 
Write pictures described here

Write pictures described here

Guess you like

Origin www.cnblogs.com/decode1234/p/11208617.html