mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-25 12:43:09 +00:00
msggate example
This commit is contained in:
@@ -1,5 +1,18 @@
|
||||
package.cpath = "luaclib/?.so"
|
||||
|
||||
--[[ Status code
|
||||
|
||||
200 OK
|
||||
|
||||
400 Bad Request (通常是登陆协议错误)
|
||||
401 Unauthorized (通常是登陆服务器或游戏服务器验证错误)
|
||||
403 Forbidden (通常是连接游戏服务器的 index 已经过期)
|
||||
404 Not Found (通常是游戏服务器未获得登陆服务器的通知)
|
||||
406 Not Acceptable (通常是登陆服务器转发游戏服务器拒绝登陆)
|
||||
412 Precondition Failed (通常是遗漏了和游戏服务器前次通讯的请求)
|
||||
|
||||
]]
|
||||
|
||||
local socket = require "clientsocket"
|
||||
local cjson = require "cjson"
|
||||
local crypt = require "crypt"
|
||||
@@ -43,6 +56,7 @@ local hmac = crypt.hmac64(challenge, secret)
|
||||
socket.writeline(fd, crypt.base64encode(hmac))
|
||||
|
||||
local token = {
|
||||
server = "sample",
|
||||
user = "hello",
|
||||
pass = "password",
|
||||
}
|
||||
@@ -53,6 +67,49 @@ socket.writeline(fd, crypt.base64encode(etoken))
|
||||
|
||||
print(readline())
|
||||
|
||||
socket.close(fd)
|
||||
|
||||
----- connect to game server
|
||||
|
||||
local input = {}
|
||||
local fd = assert(socket.connect("127.0.0.1", 8888))
|
||||
|
||||
local function readpackage()
|
||||
local line = table.remove(input, 1)
|
||||
if line then
|
||||
return line
|
||||
end
|
||||
|
||||
while true do
|
||||
local status
|
||||
status, last = socket.recv(fd, last, input)
|
||||
if status == nil then
|
||||
error "Server closed"
|
||||
end
|
||||
if not status then
|
||||
socket.usleep(100)
|
||||
else
|
||||
local line = table.remove(input, 1)
|
||||
if line then
|
||||
return line
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local index = 0
|
||||
local request = 0
|
||||
local handshake = string.format("%s@%s:%d:%d", crypt.base64encode(token.user), crypt.base64encode(token.server) , index, request)
|
||||
local hmac = crypt.hmac64(crypt.hashkey(handshake), secret)
|
||||
|
||||
socket.send(fd, handshake .. ":" .. crypt.base64encode(hmac))
|
||||
|
||||
print(readpackage())
|
||||
|
||||
socket.send(fd , "echo")
|
||||
print(readpackage())
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
local login = require "loginserver"
|
||||
local json = require "cjson"
|
||||
local crypt = require "crypt"
|
||||
local skynet = require "skynet"
|
||||
|
||||
local server = {
|
||||
host = "127.0.0.1",
|
||||
@@ -8,19 +9,40 @@ local server = {
|
||||
name = "login_master",
|
||||
}
|
||||
|
||||
local server_list = {}
|
||||
local user_online = {}
|
||||
|
||||
function server.auth_handler(token)
|
||||
token = json.decode(token)
|
||||
assert(token.user)
|
||||
assert(token.pass == "password")
|
||||
return "sample", token.user
|
||||
return token.server, token.user
|
||||
end
|
||||
|
||||
function server.login_handler(server, uid, secret)
|
||||
print(string.format("%s@%s is login, secret is %s", uid, server, crypt.hexencode(secret)))
|
||||
local u = user_online[uid]
|
||||
if u then
|
||||
local gameserver = server_list[u.server]
|
||||
skynet.call(gameserver, "lua", "kick", server, uid)
|
||||
end
|
||||
local gameserver = assert(server_list[server])
|
||||
skynet.call(gameserver, "lua", "login", server, uid, secret)
|
||||
end
|
||||
|
||||
function server.logout_handler(server, uid)
|
||||
local CMD = {}
|
||||
|
||||
function CMD.register_gate(source, name)
|
||||
server_list[name] = source
|
||||
end
|
||||
|
||||
function CMD.logout(source, uid, server)
|
||||
print(string.format("%s@%s is logout", uid, server))
|
||||
end
|
||||
|
||||
function server.command_handler(command, source, ...)
|
||||
local f = assert(CMD[command])
|
||||
return f(source, ...)
|
||||
end
|
||||
|
||||
login(server)
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
local skynet = require "skynet"
|
||||
|
||||
skynet.start(function()
|
||||
skynet.newservice "logind"
|
||||
local loginserver = skynet.newservice "logind"
|
||||
local gate = skynet.newservice "msggate"
|
||||
skynet.call(gate, "lua", "open" , {
|
||||
port = 8888,
|
||||
maxclient = 64,
|
||||
loginserver = loginserver,
|
||||
servername = "sample",
|
||||
})
|
||||
end)
|
||||
|
||||
27
examples/login/msgagent.lua
Normal file
27
examples/login/msgagent.lua
Normal file
@@ -0,0 +1,27 @@
|
||||
local skynet = require "skynet"
|
||||
|
||||
skynet.register_protocol {
|
||||
name = "client",
|
||||
id = skynet.PTYPE_CLIENT,
|
||||
unpack = skynet.tostring,
|
||||
}
|
||||
|
||||
local gate
|
||||
|
||||
local CMD = {}
|
||||
|
||||
function CMD.init(source , uid, server)
|
||||
gate = source
|
||||
skynet.error(string.format("%s is coming", uid))
|
||||
end
|
||||
|
||||
skynet.start(function()
|
||||
skynet.dispatch("lua", function(_, source, command, ...)
|
||||
local f = assert(CMD[command])
|
||||
skynet.ret(skynet.pack(f(source, ...)))
|
||||
end)
|
||||
|
||||
skynet.dispatch("client", function(_,_, msg)
|
||||
skynet.ret(msg)
|
||||
end)
|
||||
end)
|
||||
187
examples/login/msggate.lua
Normal file
187
examples/login/msggate.lua
Normal file
@@ -0,0 +1,187 @@
|
||||
local skynet = require "skynet"
|
||||
local gateserver = require "gateserver"
|
||||
local netpack = require "netpack"
|
||||
local crypt = require "crypt"
|
||||
local socketdriver = require "socketdriver"
|
||||
local datacenter = require "datacenter"
|
||||
|
||||
local users = {}
|
||||
|
||||
skynet.register_protocol {
|
||||
name = "client",
|
||||
id = skynet.PTYPE_CLIENT,
|
||||
}
|
||||
|
||||
local handler = {}
|
||||
local handshake = {}
|
||||
local connection = {}
|
||||
local agent = {}
|
||||
local login_master
|
||||
|
||||
-- launch an agent service to handle message from client
|
||||
local function launch_agent(c)
|
||||
local agent = assert(skynet.newservice "msgagent")
|
||||
skynet.call(agent, "lua", "init", c.uid, c.server)
|
||||
return agent
|
||||
end
|
||||
|
||||
function handler.connect(fd, addr)
|
||||
skynet.error(string.format("new connection from %s (fd=%d)", addr, fd))
|
||||
handshake[fd] = true
|
||||
gateserver.openclient(fd)
|
||||
end
|
||||
|
||||
local function auth(fd, msg, sz)
|
||||
-- base64(base64(uid)@base64(server):index:request:base64(hmac)
|
||||
local message = netpack.tostring(msg, sz)
|
||||
local username, index, request , hmac = string.match(message, "([^:]*):([^:]*):([^:]*):([^:]*)")
|
||||
local content = users[username]
|
||||
if content == nil then
|
||||
return "404 Not Found"
|
||||
end
|
||||
local idx = assert(tonumber(index))
|
||||
local req = assert(tonumber(request))
|
||||
hmac = crypt.base64decode(hmac)
|
||||
|
||||
if idx < content.index then
|
||||
return "403 Forbidden"
|
||||
end
|
||||
if req > content.request or req < content.reserved then
|
||||
return "412 Precondition Failed"
|
||||
end
|
||||
|
||||
local text = string.format("%s:%s:%s", username, index, request)
|
||||
local v = crypt.hmac64(crypt.hashkey(text), content.secret)
|
||||
if v ~= hmac then
|
||||
return "401 Unauthorized"
|
||||
end
|
||||
|
||||
content.index = idx
|
||||
for i=content.reserved, request do
|
||||
content.response[i] = nil
|
||||
end
|
||||
content.reserved = request
|
||||
content.reqest = request
|
||||
connection[fd] = content
|
||||
|
||||
if content.fd then
|
||||
local last_fd = content.fd
|
||||
connection[last_fd] = nil
|
||||
gateserver.closeclient(last_fd)
|
||||
end
|
||||
content.fd = fd
|
||||
|
||||
if content.agent == nil then
|
||||
content.agent = true
|
||||
local ok, agent = pcall(launch_agent, content)
|
||||
if ok then
|
||||
content.agent = agent
|
||||
else
|
||||
content.agent = nil
|
||||
skynet.error(string.format("Launch agent %s failed : %s", content.uid, agent))
|
||||
connection[fd] = nil
|
||||
gateserver.closeclient(fd)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function handler.message(fd, msg, sz)
|
||||
if handshake[fd] then
|
||||
local ok, result = pcall(auth, fd, msg, sz)
|
||||
if not ok then
|
||||
result = "400 Bad Request"
|
||||
end
|
||||
|
||||
local close = result ~= nil
|
||||
|
||||
if result == nil then
|
||||
result = "200 OK"
|
||||
end
|
||||
|
||||
socketdriver.send(fd, netpack.pack(result))
|
||||
|
||||
if close then
|
||||
gateserver.closeclient(fd)
|
||||
end
|
||||
handshake[fd] = nil
|
||||
else
|
||||
local c = connection[fd]
|
||||
if c == nil or c.agent == nil then
|
||||
local message = netpack.tostring(msg, sz)
|
||||
skynet.error(string.format("Unknown fd = %d, message (%s) size = %d", fd, crypt.hexencode(message):sub(1,80), #message))
|
||||
return
|
||||
end
|
||||
c.request = c.request + 1
|
||||
local ret = c.response[c.request]
|
||||
if ret == nil then
|
||||
local ok, msg, sz = pcall(skynet.rawcall, c.agent, "client", msg, sz)
|
||||
if ok then
|
||||
ret = netpack.pack_string(msg, sz)
|
||||
else
|
||||
skynet.error(string.format("%s request error : %s", c.username, msg))
|
||||
ret = netpack.pack_string ""
|
||||
end
|
||||
c.response[c.request] = ret
|
||||
end
|
||||
socketdriver.send(c.fd, ret)
|
||||
end
|
||||
end
|
||||
|
||||
function handler.close(fd)
|
||||
handshake[fd] = nil
|
||||
local c = connection[fd]
|
||||
if c then
|
||||
c.fd = nil
|
||||
end
|
||||
end
|
||||
|
||||
handler.error = handler.close
|
||||
|
||||
function handler.open(source, conf)
|
||||
login_master = assert(conf.loginserver)
|
||||
local servername = assert(conf.servername)
|
||||
skynet.call(login_master, "lua", "register_gate", servername)
|
||||
end
|
||||
|
||||
|
||||
local CMD = {}
|
||||
|
||||
function CMD.login(source, server, uid, secret)
|
||||
local username = crypt.base64encode(uid) .. '@' .. crypt.base64encode(server)
|
||||
users[username] = {
|
||||
server = server,
|
||||
uid = uid,
|
||||
secret = secret,
|
||||
index = 0,
|
||||
request = 0,
|
||||
reserved = 0,
|
||||
response = {},
|
||||
}
|
||||
end
|
||||
|
||||
function CMD.logout(source)
|
||||
local c = agent[source]
|
||||
if c then
|
||||
skynet.call(login_master, "lua", "logout", c.server, c.uid)
|
||||
if c.fd then
|
||||
gateserver.closeclient(c.fd)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function CMD.kick(source, server, uid)
|
||||
local username = crypt.base64encode(uid) .. '@' .. crypt.base64encode(server)
|
||||
local u = users[username]
|
||||
if u and u.agent then
|
||||
skynet.call(u.agent, "logout")
|
||||
skynet.kill(u.agent)
|
||||
u.agent = nil
|
||||
end
|
||||
end
|
||||
|
||||
function handler.command(cmd, source, ...)
|
||||
local f = assert(CMD[cmd])
|
||||
return f(source, ...)
|
||||
end
|
||||
|
||||
gateserver.start(handler)
|
||||
Reference in New Issue
Block a user