GateServer的使用
skynet 提供了一个通用模板 lualib/snax/gateserver.lua 来启动一个网关服务器,通过 TCP 连接和客户端交换数据。
TCP 基于数据流,但一般我们需要以带长度信息的数据包的结构来做数据交换。gateserver 做的就是这个工作,把数据流切割成包的形式转发到可以处理它的地址。
skynet 提供了一个 lua 库 netpack ,用来把 tcp 流中的数据解析成 长度 + 内容的包。
gateserver就是使用netpack进行包解析。
在同一个目录建立7个文件(config,proto.lua,main.lua,mygate.lua,socket1.lua,service1.lua,client1.lua)
本例子的client1使用前两节的client,代码稍作修改。
client1.lua代码:
package.cpath = "luaclib/?.so"package.path = "lualib/?.lua;myexample/e5/?.lua"if _VERSION ~= "Lua 5.3" then error "Use lua 5.3"endlocal socket = require "clientsocket"local proto = require "proto"local sproto = require "sproto"local host = sproto.new(proto.s2c):host "package"local request = host:attach(sproto.new(proto.c2s))local fd = assert(socket.connect("127.0.0.1", 8888))local session = 0local function send_package(fd, pack) local package = string.pack(">s2", pack) socket.send(fd, package)endlocal function send_request(name, args) session = session + 1 local str = request(name, args, session) send_package(fd,str); print("Request:", session)endsend_request("handshake")send_request("say", { name = "soul", msg = "hello world" })while true do local str = socket.recv(fd) if str~=nil and str~="" then print("server says: "..str) end local readstr = socket.readstdin() if readstr then if readstr == "quit" then send_request("quit") else send_request("say", { name = "soul", msg = readstr }) end else socket.usleep(100) endend
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
socket1.lua代码:
local skynet = require "skynet"local common = require "common"local gateskynet.start(function() print("==========Socket Start=========") print("Listen socket :", "127.0.0.1", 8888) local conf = { address = "127.0.0.1", port = 8888, maxclient = 1024, nodelay = true, } gate=skynet.newservice("mygate") skynet.call(gate, "lua", "open" , conf)end)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
mygate.lua代码:
local skynet = require "skynet"local gateserver = require "snax.gateserver"local netpack = require "netpack"local common = require "common"local connection = {} local handler = {}local agentlist = {}function handler.message(fd, msg, sz) print("===========gate handler.message============"..fd) local c = connection[fd] local agent = agentlist[fd] if agent then print("接收到客户端消息,传给agent服务处理") else print("没有agent处理该消息") endendfunction handler.connect(fd, addr) print("===========gate handler.connect============") local c = { fd = fd, ip = addr, } connection[fd] = c gateserver.openclient(fd) agentlist[fd] = skynet.newservice("service1") skynet.call(agentlist[fd], "lua", "start", { fd = fd, addr = addr })endfunction handler.disconnect(fd) print(fd.."-断开连接")endfunction handler.error(fd, msg) print("异常错误")endgateserver.start(handler)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
service1.lua代码:
local common = require "common"local skynet = require "skynet"require "skynet.manager" local CMD = {}local client_fdlocal hostfunction CMD.start(conf) print("service1 CMD.start") endfunction CMD.disconnect() skynet.exit()endskynet.start(function() print("==========Service1 Start=========") skynet.dispatch("lua", function(session, address, cmd, ...) print("==========Service1 dispatch============"..cmd) local f = CMD[cmd] skynet.ret(skynet.pack(f(...))) end)end)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
项目源码:http://download.csdn.net/detail/uisoul/9806650
API参考文档:https://github.com/cloudwu/skynet/wiki/GateServer
0 0