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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
| local base = _G local socket = require("socket")
TIMEOUT = 60 ERROR_TIMEOUT = "timeout"
local handlert = {}
local function newset() local reverse = {} local set = {}
return base.setmetatable(set, {__index = { insert = function(set, value) if not reverse[value] then table.insert(set, value) reverse[value] = #set end end, remove = function(set, value) local index = reverse[value] if index then reverse[value] = nil local top = table.remove(set) if top ~= value then reverse[top] = index set[index] = top end end end }}) end
local function seq_start(self, func) return func() end
function handlert.sequential() return { tcp = socket.tcp, start = seq_start } end
function newhandler(mode) mode = mode or "coroutine" return handlert[mode]() end
local function cowrap(dispatcher, tcp, error) if not tcp then return nil, error end
tcp:settimeout(0) local metat = {__index = function(table, key) table[key] = function(...) return tcp[key](tcp, select(2, ...)) end return table[key] end}
local zero = false local wrap = {}
function wrap:settimeout(value, mode) if value == 0 then zero = true else zero = false end
return 1 end
function wrap:send(data, index, last) index = (index or 1) - 1 local result, error while true do if coroutine.yield(dispatcher.sending, tcp) == ERROR_TIMEOUT then print("timeout while sending") return nil, ERROR_TIMEOUT end result, error, index = tcp:send(data, index + 1, last) if result ~= ERROR_TIMEOUT then return result, error, index end end end
function wrap:receive(pattern, partial) local error = ERROR_TIMEOUT local value while true do if coroutine.yield(dispatcher.receiving, tcp) == ERROR_TIMEOUT then return nil, ERROR_TIMEOUT end
value, error, partial = tcp:receive(pattern, partial) if (error ~= ERROR_TIMEOUT) or zero then return value, error, partial end end end
function wrap:connect(host, port) local result, error = tcp:connect(host, port) if error == ERROR_TIMEOUT then if coroutine.yield(dispatcher.sending, tcp) == ERROR_TIMEOUT then return nil, ERROR_TIMEOUT end
result, error = tcp:connect(host, port) if result or error == "already connected" then return 1 else return nil, "non-blocking connect failed" end else return result, error end end
function wrap:accept() while true do if coroutine.yield(dispatcher.receiving, tcp) == ERROR_TIMEOUT then return nil, ERROR_TIMEOUT end
local client, error = tcp:accept() if error ~= ERROR_TIMEOUT then return cowrap(dispatcher, client, error) end end end
function wrap:close() dispatcher.stamp[tcp] = nil dispatcher.sending.conns:remove(tcp) dispatcher.sending.routines[tcp] = nil dispatcher.receiving.conns:remove(tcp) dispatcher.receiving.routines[tcp] = nil
return tcp:close() end
return base.setmetatable(wrap, metat) end
local cometat = { __index = {} }
function schedule(co, status, op, tcp) if status then if co and op then op.conns:insert(tcp) op.routines[tcp] = co op.stamp[tcp] = socket.gettime() end else base.error(op) end end
function kick(op, tcp) op.routines[tcp] = nil op.conns:remove(tcp) end
function wakeup(op, tcp) local routine = op.routines[tcp] if routine then kick(op, tcp) return routine, coroutine.resume(routine) else return nil, true end end
function abort(op, tcp) local routine = op.routines[tcp] if routine then kick(op, tcp) coroutine.resume(routine, ERROR_TIMEOUT) end end
function cometat.__index:step() local readable, writable = socket.select(self.receiving.conns, self.sending.conns, 1) for _, tcp in base.ipairs(readable) do schedule(wakeup(self.receiving, tcp)) end
for _, tcp in base.ipairs(writable) do schedule(wakeup(self.sending, tcp)) end
local now = socket.gettime() for tcp, stamp in base.ipairs(self.stamp) do if tcp.class == "tcp{client}" and now - stamp > TIMEOUT then abort(self.sending, tcp) abort(self.receiving, tcp) end end end
function cometat.__index:start(func) local routine = coroutine.create(func) schedule(routine, coroutine.resume(routine)) end
function handlert.coroutine() local stamp = {} local dispatcher = { stamp = stamp, sending = { name = "sending", conns = newset(), routines = {}, stamp = stamp }, receiving = { name = "receiving", conns = newset(), routines = {}, stamp = stamp } } function dispatcher.tcp() return cowrap(dispatcher, socket.tcp()) end
return base.setmetatable(dispatcher, cometat) end
return newhandler
|