Skynet Source Analysis: Services

Source: Internet
Author: User

Skynet is a lightweight server-side framework built for multiplayer online games, using C+lua implementations. One of the benefits of using this framework is that it basically requires only LUA, which is rarely used for development, which in some way improves development efficiency.

The example of Skynet is how to invoke the server:
Simpledb.lua:skynet.register "SimpleDB" to register a service in Skynet
Agent.lua:skynet.call ("SIMPLEDB", "text", text) call the corresponding service
Main.lua:skynet.newservice ("SimpleDB") Start a service
The above functions are all within the \lualib\skynet.lua file

Here are a few of the functions that are often used when writing services.

NewService (name, ...) starts a new service named name.
UniqueService (name, ...) starts a unique service and returns the service address that was started if the service was started.
QueryService (name) queries the address of a unique service initiated by UniqueService and waits if the service has not been started.
LocalName (name) returns the address of the named service registered with register in the same process.

NewService can start multiple services in a single process, which applies to stateless services.
UniqueService is similar to a single piece (singleton) in design mode, which applies to services that require uniqueness. For example, write a log, just want to write a copy. Or the data that is shared globally. Message mechanism skynet design Overview The module is called a service. "The service room is free to send messages. Each module can register a callback function with the Skynet framework to receive messages sent to it. "also mentions" to start a conforming C module from a dynamic library (so file) and bind a digital ID that never repeats (even if the module exits) as its handle. Skynet provides a name service and can also make a readable name for a particular service, rather than using an ID to refer to it. ID and run temporal correlation, there is no guarantee to start the service every time, there is a consistent ID, but the name can. "The two files to be analyzed today skynet_handle.c and skynet_handle.h are the implementation of name services.
Skynet_handle.c actually did two core things, one to assign a handle to the service, and two to associate handle with name.

Associating handle with name is easier to understand, actually using an array, associating using binary to find the name of the array, inserting an element if the name does not exist, and then associating the name with the handle. When inserting an element, if the array space is insufficient, the expansion is twice times the original.

Assigning handle to the service is slightly more complicated, and actually using a slot array, the array subscript uses a hash, and the array element points to the context of the service. This hash algorithm is relatively simple and rough, is to see from Handle_indx to Slot_size, look at the middle there is no idle subscript (that is, the subscript pointing to null), if the loop is over or not, the slot is enlarged one times, or not to expand one more times, Until the empty space is found, or the slot length exceeds the limit.

After taking the handle, we also attach the harbor ID to the high 8 bits of handle.


Reference: Skynet source Analysis (3)--Handle of the message name and ID

Skynet Tutorials (3)--Aliases for services

Skynet Source Analysis: Services

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.