use sproto instead of cjson

This commit is contained in:
Cloud Wu
2014-09-07 17:42:10 +08:00
parent 18af238256
commit db9ab88a6c
69 changed files with 9805 additions and 11465 deletions

View File

@@ -1,40 +1,75 @@
local skynet = require "skynet"
local jsonpack = require "jsonpack"
local netpack = require "netpack"
local socket = require "socket"
local sproto = require "sproto"
local bit32 = require "bit32"
local rpc
local CMD = {}
local REQUEST = {}
local client_fd
local function send_client(v)
socket.write(client_fd, netpack.pack(jsonpack.pack(0, {true, v})))
function REQUEST:get()
print("get", self.what)
local r = skynet.call("SIMPLEDB", "lua", "get", self.what)
return { result = r }
end
local function response_client(session,v)
socket.write(client_fd, netpack.pack(jsonpack.response(session,v)))
function REQUEST:set()
print("set", self.what, self.value)
local r = skynet.call("SIMPLEDB", "lua", "set", self.what, self.value)
return { ok = true }
end
function REQUEST:handshake()
return { msg = "Welcome to skynet" }
end
local function request(name, args, response)
local f = assert(REQUEST[name])
local r = f(args)
if response then
return response(r)
end
end
local function send_package(pack)
local size = #pack
local package = string.char(bit32.extract(size,8,8)) ..
string.char(bit32.extract(size,0,8))..
pack
socket.write(client_fd, package)
end
skynet.register_protocol {
name = "client",
id = skynet.PTYPE_CLIENT,
unpack = function (msg, sz)
return jsonpack.unpack(skynet.tostring(msg,sz))
return rpc:dispatch(msg, sz)
end,
dispatch = function (_, _, session, args)
local ok, result = pcall(skynet.call,"SIMPLEDB", "lua", table.unpack(args))
if ok then
response_client(session, { true, result })
dispatch = function (_, _, type, ...)
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
response_client(session, { false, "Invalid command" })
assert(type == "RESPONSE")
error "This example doesn't support request client"
end
end
}
function CMD.start(gate , fd)
function CMD.start(gate, fd, proto)
rpc = sproto.new(proto):rpc "package"
client_fd = fd
skynet.call(gate, "lua", "forward", fd)
send_client "Welcome to skynet"
end
skynet.start(function()

View File

@@ -1,8 +1,12 @@
package.cpath = "luaclib/?.so"
package.path = "lualib/?.lua;examples/?.lua"
local socket = require "clientsocket"
local cjson = require "cjson"
local bit32 = require "bit32"
local proto = require "proto"
local sproto = require "sproto"
local rpc = sproto.new(proto):rpc "package"
local fd = assert(socket.connect("127.0.0.1", 8888))
@@ -46,34 +50,39 @@ end
local session = 0
local function send_request(v)
local function send_request(name, args)
session = session + 1
local str = string.format("%d+%s",session, cjson.encode(v))
local str = rpc:request(name, args, session)
send_package(fd, str)
print("Request:", session)
end
local last = ""
while true do
local function dispatch_package()
while true do
local v
v, last = recv_package(last)
if not v then
break
end
local session,t,str = string.match(v, "(%d+)(.)(.*)")
assert(t == '-' or t == '+')
session = tonumber(session)
local result = cjson.decode(str)
print("Response:",session, result[1], result[2])
local t, session, response = rpc:dispatch(v)
assert(t == "RESPONSE" , "This example only support request , so here must be RESPONSE")
print("response session", session)
for k,v in pairs(response) do
print(k,v)
end
end
end
send_request("handshake")
while true do
dispatch_package()
local cmd = socket.readstdin()
if cmd then
local args = {}
string.gsub(cmd, '[^ ]+', function(v) table.insert(args, v) end )
send_request(args)
send_request("get", { what = cmd })
else
socket.usleep(100)
end
end
end

37
examples/proto.lua Normal file
View File

@@ -0,0 +1,37 @@
local sprotoparser = require "sprotoparser"
local proto = sprotoparser.parse [[
.package {
type 0 : integer
session 1 : integer
}
handshake 1 {
request {}
response {
msg 0 : string
}
}
get 2 {
request {
what 0 : string
}
response {
result 0 : boolean
}
}
set 3 {
request {
what 0 : string
value 1 : string
}
response {
ok 0 : boolean
}
}
]]
return proto

View File

@@ -1,5 +1,8 @@
package.path = "./examples/?.lua;" .. package.path
local skynet = require "skynet"
local netpack = require "netpack"
local proto = require "proto"
local CMD = {}
local SOCKET = {}
@@ -8,7 +11,7 @@ local agent = {}
function SOCKET.open(fd, addr)
agent[fd] = skynet.newservice("agent")
skynet.call(agent[fd], "lua", "start", gate, fd)
skynet.call(agent[fd], "lua", "start", gate, fd, proto)
end
local function close_agent(fd)