diff --git a/examples/proto.lua b/examples/proto.lua index bbc7ee94..5cf85b11 100644 --- a/examples/proto.lua +++ b/examples/proto.lua @@ -7,7 +7,6 @@ local proto = sprotoparser.parse [[ } handshake 1 { - request {} response { msg 0 : string } diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index c2ef29e7..602f86e9 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -422,14 +422,16 @@ lprotocol(lua_State *L) { } struct sproto_type * request = sproto_protoquery(sp, tag, SPROTO_REQUEST); 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); if (response == NULL) { - return 2; + lua_pushnil(L); + } else { + lua_pushlightuserdata(L, response); } - lua_pushlightuserdata(L, response); return 3; } diff --git a/lualib-src/sproto/sproto.c b/lualib-src/sproto/sproto.c index c701d092..0f519d14 100644 --- a/lualib-src/sproto/sproto.c +++ b/lualib-src/sproto/sproto.c @@ -329,10 +329,13 @@ import_protocol(struct sproto *s, struct protocol *p, const uint8_t * stream) { p->p[SPROTO_REQUEST] = NULL; p->p[SPROTO_RESPONSE] = NULL; int i; - for (i=0;iname == NULL || p->tag<0 || p->p[SPROTO_REQUEST] == NULL) { + if (p->name == NULL || p->tag<0) { return NULL; } diff --git a/lualib/sproto.lua b/lualib/sproto.lua index 423a5611..a6180b0b 100644 --- a/lualib/sproto.lua +++ b/lualib/sproto.lua @@ -126,11 +126,19 @@ function rpc:dispatch(...) if header.type then -- request local proto = queryproto(self.__proto, header.type) - local result = core.decode(proto.request, content) - if header_tmp.session then - return "REQUEST", proto.name, result, gen_response(self, proto.response, header_tmp.session) + if proto.request then + local result = core.decode(proto.request, content) + 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 - 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 else -- response diff --git a/lualib/sprotoparser.lua b/lualib/sprotoparser.lua index 1b9dcfcd..f6b24399 100644 --- a/lualib/sprotoparser.lua +++ b/lualib/sprotoparser.lua @@ -271,30 +271,35 @@ local function packtype(name, t, alltypes) end local function packproto(name, p, alltypes) - if p.request == nil then - error(string.format("Protocol %s need request", name)) +-- if p.request == nil then +-- 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 - local request = alltypes[p.request] - if request == nil then - error(string.format("Protocol %s request type %s not found", name, p.request)) - end - local tmp - if p.response then - tmp = { - "\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) - } + local tmp = { + "\4\0", -- 4 fields + "\0\0", -- name (id=0, ref=0) + packvalue(p.tag), -- tag (tag=1) + } + if p.request == nil and p.response == nil then + tmp[1] = "\2\0" else - tmp = { - "\3\0", -- 3 fields - "\0\0", -- name (id=0, ref=0) - packvalue(p.tag), -- tag (tag=1) - packvalue(alltypes[p.request]), -- request typename (tag=2) - } + if p.request then + table.insert(tmp, packvalue(alltypes[p.request])) -- request typename (tag=2) + else + table.insert(tmp, "\1\0") + end + if p.response then + table.insert(tmp, packvalue(alltypes[p.response])) -- request typename (tag=3) + else + tmp[1] = "\3\0" + end end + table.insert(tmp, packbytes(name)) return packbytes(table.concat(tmp))