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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
| local skynet = require "skynet" local socket = require "socket" local netpack = require "netpack" local websocket = require "websocket" local log = require "log"
local pcall = pcall local string = string local assert = assert
local M = {}
function M.create_gate_send(pack) return function(gate,fd,name,tab) assert(fd) assert(name) assert(tab) local msg,err = pack(name,tab) if not msg then log.error("util_net_base.pack err ",name,tab,err) return end return socket.write(fd,netpack.pack(msg)) end end
function M.create_gate_broadcast(pack) return function(gate_list,fd_list,name,tab) assert(fd_list and #fd_list > 0) assert(name) assert(tab)
local msg,err = pack(name,tab) if not msg then log.error("util_net_base.pack err ",name,tab,err) return end
for i = 1,#fd_list do socket.write(fd_list[i], netpack.pack(msg)) end end end
function M.create_gate_unpack(unpack) return function(msg,sz) assert(msg) assert(sz) local msgstr = skynet.tostring(msg,sz) if sz < 2 then log.info("unpack invalid msg ",msgstr,sz) return nil end local packname,tab = unpack(msgstr) if not packname then log.error("unpack err ",tab) return end return packname,tab end end
function M.create_recv(read,unpack) return function(fd,dispatch) assert(fd) assert(dispatch) local is_cancel = false local total_msg = "" local function unpack_msg() local sz = total_msg:len() if sz < 2 then return nil end local pack_sz = (total_msg:byte(1) << 8) + total_msg:byte(2) if sz < pack_sz + 2 then return nil end local offset = 2 local msg = total_msg:sub(offset + 1,offset + pack_sz) total_msg = total_msg:sub(offset + 1 + pack_sz) return msg end skynet.fork(function() while not is_cancel do local ok,msg = pcall(read,fd) if not ok or msg == false then log.error("read faild ",fd,msg) break end total_msg = total_msg .. msg while total_msg:len() > 0 do local one_pack = unpack_msg() if not one_pack then break end skynet.fork(dispatch,fd,unpack(one_pack)) end end socket.close(fd) end) return function() is_cancel = true end end end
local function create_ws_gate_send(type) local send_type = 'send_' .. type return function(pack) return function(gate,fd,name,tab) assert(fd) assert(name) assert(tab) local msg,err = pack(name,tab) if not msg then log.error("util_net_base.pack err ",name,tab,err) return end local send_buffer = string.pack(">I2",msg:len()) .. msg if not gate then if websocket.is_close(fd) then log.warn("send not exists fd ",fd) else websocket.write(fd,send_buffer,type) end else skynet.send(gate,'lua',send_type,fd,send_buffer) end end end end
local function create_ws_gate_broadcast(type) local send_type = 'send_' .. type return function(pack) return function(gate_list,fd_list,name,tab) assert(gate_list and #gate_list > 0) assert(fd_list and #fd_list > 0) assert(name) assert(tab) local msg,err = pack(name,tab) if not msg then log.error("util_net_base.pack err ",name,tab,err) return end
local send_buffer = string.pack(">I2",msg:len()) .. msg for i = 1,#fd_list do local fd = fd_list[i] local gate = gate_list[i] skynet.send(gate,'lua',send_type,fd,send_buffer) end end end end
M.create_ws_gate_send_text = create_ws_gate_send('text')
M.create_ws_gate_send_binary = create_ws_gate_send('binary')
M.create_ws_gate_broadcast_text = create_ws_gate_broadcast('text')
M.create_ws_gate_broadcast_binary = create_ws_gate_broadcast('binary')
function M.create_ws_gate_unpack(unpack) return function(msg,sz) assert(msg) local msgstr = skynet.tostring(msg,sz) if sz < 2 then log.info("unpack invalid msg ",msg,sz) return end local msgsz = (msgstr:byte(1) << 8) + msgstr:byte(2) msgstr = msgstr:sub(3) sz = msgstr:len() if msgsz ~= sz then log.info("unpack invalid msg ",msgsz,sz) return end
local packname,tab = unpack(msgstr) if not packname then log.error("unpack err ",tab) return end
return packname,tab end end
return M
|