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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| local function handle_request (name, args, response) print ("--- 【S>>C】, request from server:", name)
local f = REQUEST[name] if f then local ret = f(nil, args) if ret and response then send_message (response (ret)) end else print("--- handle_request, not found func:"..s.name) end end
local function handle_response (id, args) local s = assert (session[id]) session[id] = nil local f = RESPONSE[s.name]
print ("--- 【S>>C】, response from server:", s.name)
if f then f (s.args, args) else print("--- handle_response, not found func:"..s.name) end end
local function handle_message (t, ...) if t == "REQUEST" then handle_request (...) else handle_response (...) end end
local function unpack (text) if not text then return end
local size = #text if size < 2 then return nil, text end local s = text:byte (1) * 256 + text:byte (2)
print(string.format("--- unpacking, realSize:%d, expectSize:%d",size, s)) if size < s + 2 then return nil, text end
return text:sub (3, 2 + s), text:sub (3 + s) end
local last = "" local function recv (last) local result result, last = unpack (last) if result then return result, last end
local r, err = network:recv() if r then print("--- socket recv, r, len:", #r, r) end if err then return nil, last end if r == "" then error (string.format ("socket closed")) end
return unpack (last .. r) end
function RpcMgr.schedulerReceive( ... ) local function dispatch_message () while true do local v v, last = recv(last) if not v then break end
handle_message (host:dispatch (v)) end end
dispatch_message() end
|