Learning Skynet_examples from the beginning of zero

Source: Internet
Author: User
Tags assert lua

First, compile Skynet:

1, with ubuntu15.10 Direct

Make Linux
There will be an error in compiling Skynet. Install autoconf, Libreadline6, Libreadline6-dev after the success of the compilation.

sudo apt-get install autoconf
sudo apt-get install Libreadline6 Libreadline6-dev

II. related Code Analysis for examples:

1, about the examples/config inside a line:

Start = "Main"-	-Main script
It means to open the first Skynet service Main.lua by start= "main". This file Main.lua also in the examples folder:

Local Skynet = Require ' skynet ' local
Sprotoloader = Require ' sprotoloader ' local

max_client = Skynet.star

T (function ()
	skynet.error ("Server start")
	Skynet.uniqueservice ("Protoloader")
	if not skynet.getenv " Daemon ' then local
		console = skynet.newservice (' console ')
	end
	Skynet.newservice ("Debug_console", 8000)
	skynet.newservice ("SimpleDB") local
	watchdog = Skynet.newservice ("watchdog")
	Skynet.call (watchdog, "Lua", "start", {
		port = 8888,
		maxclient = max_client,
		Nodelay = True,
	})
	Skynet.error (" Watchdog listen on ", 8888)
	skynet.exit () end
)
The first line quotes Skynet, the library, which is written in Lua, which is Lualib/skynet.lua. These interfaces are defined inside:

1. Skynet.start () is used for the portal of the service, when loading the LUA service runs the code here, it calls the luaclib-src/lua_skynet.c inside the callback (), and finally invokes the Skynet framework Skynet_ Callback () to set the Destroy function.

2. Skynet.newservice () is used to start a LUA-written service, omitting the. Lua suffix name. It calls the Skynet.call () and then Skynet.call () calls luaclib-src/lua_skynet.c inside Send (), which eventually invokes the Skynet frame Skynet_send () into the queue.

3. Skynet.call () is used to send a message to the Skynet frame. Messages are pressed into the queue, waiting for the Skynet frame to be scheduled.

4. Skynet.exit () Removes the service and sends a message to the Skynet framework by Skynet.send () to remove the LUA service.

5). Skynet.monitor () is used to monitor the service to see if it is closed.

Main.lua has opened four services:

1). Service_mgr: This is a system module for managing services.

2). Console: This is the system's module for managing output.

3). SimpleDB: This is an example module for managing Key-value data.

4. Watchdog: This is an example module for monitoring socket ports and waiting for data.

Main.lua did not call another function, loading the service, it completed the task, so it finally called Skynet.exit () to kill itself.

Now Skynet has started the watchdog service, listening to port 8888 and waiting for the client to connect.

The following is the Skynet_start () Start function for the watchdog service:

Skynet.start (function ()
	skynet.dispatch ("Lua", function (session, source, CMD, subcmd, ...)
		if cmd = = "Socket" then local
			f = Socket[subcmd]
			f (...)
			--Socket API don ' t need return
		else local
			f = assert (Cmd[cmd])
			Skynet.ret (Skynet.pack (f (subcmd, ...)
		) End
	]

	gate = skynet.newservice ("gate") end
)
Skynet.dispatch () The callback function of this service, which is called by socket[] to call the function:

Socket.open (): Opens the agent service and starts, using gate to manage the socket.

Socket.close (): Shuts down the agent service.

Socket.error (): Print error message.

Socket.data (): There is data coming.
The following is the agent service code:

function Cmd.start (conf) local
	FD = conf.client local
	gate = conf.gate
	watchdog = Conf.watchdog
	--Slot 1 , 2 set at Main.lua
	host = Sprotoloader.load (1): Host "package"
	send_request = Host:attach (Sprotoloader.load (2))
	skynet.fork (function () while
		true do
			send_package (send_request "Heartbeat")
			Skynet.sleep (500)
		End
	)
	CLIENT_FD = FD
	skynet.call (Gate, "Lua", "forward", FD)
end
Skynet.start (function ()
<span style= "White-space:pre" >	</span>skynet.dispatch ("Lua", function (_,_, command, ...)
<span style= "White-space:pre" >		</span>local f = cmd[command]
<span style= "White-space:pre" >		</span>skynet.ret (Skynet.pack (f (...)
) <span style= "White-space:pre" >	</span>end) End
)
The Sock.open () is called Cmd.start () in front of the watchdog call, and the "Welcome to Skynet" is exported at the client.

The core of the agent is to register the protocol and send the data to the SimpleDB service according to the protocol.

Skynet.register_protocol {
	name = "Client",
	ID = skynet. Ptype_client,
	unpack = function (msg, SZ) return
		Host:dispatch (msg, SZ) end
	,
	dispatch = function (_, _, T Ype, ...)
		if type = = "Request" then local
			OK, result  = Pcall (Request, ...)
			If OK then
				if result then
					send_package (Result)
				end
			Else
				skynet.error (result)
			end
		Else
			assert (type = = "RESPONSE")
			error "This example doesn ' t support request Client" end
	end< c21/>}
The detailed part of the agreement looks at Lualib/skynet.lua.

Finally look at the SimpleDB service:

Local Skynet = require "skynet"
require "Skynet.manager"-	-Import skynet.register local
db = {} local

Co Mmand = {}

function command. Get (key) return
	Db[key]
end

function command. SET (key, value) Local last
	= Db[key]
	Db[key] = value return last end

Skynet.start (function ()
	Skynet.dispatch ("Lua", function (session, address, cmd, ...)
		Local F = command[string.upper (cmd)]
		if F then
			Skynet.ret (Skynet.pack (f (...)))
		else
			error (String.Format ("Unknown command%s", ToString (cmd)) end
	)
	skynet.register " SIMPLEDB "End
)
This is a simple way to deal with set and get.

The above is just a glance over the Skynet accompanying example, some Skynet provided by the Lua interface, other interfaces can view the Skynet.lua code.















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.