update sproto to support empty request

This commit is contained in:
Cloud Wu
2014-09-07 18:44:04 +08:00
parent db9ab88a6c
commit 01269c88ff
5 changed files with 51 additions and 34 deletions

View File

@@ -7,7 +7,6 @@ local proto = sprotoparser.parse [[
} }
handshake 1 { handshake 1 {
request {}
response { response {
msg 0 : string msg 0 : string
} }

View File

@@ -422,14 +422,16 @@ lprotocol(lua_State *L) {
} }
struct sproto_type * request = sproto_protoquery(sp, tag, SPROTO_REQUEST); struct sproto_type * request = sproto_protoquery(sp, tag, SPROTO_REQUEST);
if (request == NULL) { if (request == NULL) {
return 0; lua_pushnil(L);
} else {
lua_pushlightuserdata(L, request);
} }
lua_pushlightuserdata(L, request);
struct sproto_type * response = sproto_protoquery(sp, tag, SPROTO_RESPONSE); struct sproto_type * response = sproto_protoquery(sp, tag, SPROTO_RESPONSE);
if (response == NULL) { if (response == NULL) {
return 2; lua_pushnil(L);
} else {
lua_pushlightuserdata(L, response);
} }
lua_pushlightuserdata(L, response);
return 3; return 3;
} }

View File

@@ -329,10 +329,13 @@ import_protocol(struct sproto *s, struct protocol *p, const uint8_t * stream) {
p->p[SPROTO_REQUEST] = NULL; p->p[SPROTO_REQUEST] = NULL;
p->p[SPROTO_RESPONSE] = NULL; p->p[SPROTO_RESPONSE] = NULL;
int i; int i;
for (i=0;i<fn;i++) { int tag = 0;
for (i=0;i<fn;i++,tag++) {
int value = toword(stream + SIZEOF_FIELD * i); int value = toword(stream + SIZEOF_FIELD * i);
if (value & 1) if (value & 1) {
return NULL; tag += (value-1)/2;
continue;
}
value = value/2 - 1; value = value/2 - 1;
switch (i) { switch (i) {
case 0: // name case 0: // name
@@ -362,7 +365,7 @@ import_protocol(struct sproto *s, struct protocol *p, const uint8_t * stream) {
} }
} }
if (p->name == NULL || p->tag<0 || p->p[SPROTO_REQUEST] == NULL) { if (p->name == NULL || p->tag<0) {
return NULL; return NULL;
} }

View File

@@ -126,11 +126,19 @@ function rpc:dispatch(...)
if header.type then if header.type then
-- request -- request
local proto = queryproto(self.__proto, header.type) local proto = queryproto(self.__proto, header.type)
local result = core.decode(proto.request, content) if proto.request then
if header_tmp.session then local result = core.decode(proto.request, content)
return "REQUEST", proto.name, result, gen_response(self, proto.response, header_tmp.session) if header_tmp.session then
return "REQUEST", proto.name, result, gen_response(self, proto.response, header_tmp.session)
else
return "REQUEST", proto.name, result
end
else else
return "REQUEST", proto.name, result if header_tmp.session then
return "REQUEST", proto.name, nil, gen_response(self, proto.response, header_tmp.session)
else
return "REQUEST", proto.name
end
end end
else else
-- response -- response

View File

@@ -271,30 +271,35 @@ local function packtype(name, t, alltypes)
end end
local function packproto(name, p, alltypes) local function packproto(name, p, alltypes)
if p.request == nil then -- if p.request == nil then
error(string.format("Protocol %s need request", name)) -- error(string.format("Protocol %s need request", name))
-- end
if p.request then
local request = alltypes[p.request]
if request == nil then
error(string.format("Protocol %s request type %s not found", name, p.request))
end
end end
local request = alltypes[p.request] local tmp = {
if request == nil then "\4\0", -- 4 fields
error(string.format("Protocol %s request type %s not found", name, p.request)) "\0\0", -- name (id=0, ref=0)
end packvalue(p.tag), -- tag (tag=1)
local tmp }
if p.response then if p.request == nil and p.response == nil then
tmp = { tmp[1] = "\2\0"
"\4\0", -- 4 fields
"\0\0", -- name (id=0, ref=0)
packvalue(p.tag), -- tag (tag=1)
packvalue(alltypes[p.request]), -- request typename (tag=2)
packvalue(alltypes[p.response]), -- response typename (tag=3)
}
else else
tmp = { if p.request then
"\3\0", -- 3 fields table.insert(tmp, packvalue(alltypes[p.request])) -- request typename (tag=2)
"\0\0", -- name (id=0, ref=0) else
packvalue(p.tag), -- tag (tag=1) table.insert(tmp, "\1\0")
packvalue(alltypes[p.request]), -- request typename (tag=2) end
} if p.response then
table.insert(tmp, packvalue(alltypes[p.response])) -- request typename (tag=3)
else
tmp[1] = "\3\0"
end
end end
table.insert(tmp, packbytes(name)) table.insert(tmp, packbytes(name))
return packbytes(table.concat(tmp)) return packbytes(table.concat(tmp))