| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
local base = _G |
| 7 |
local table = require("table") |
| 8 |
local string = require("string") |
| 9 |
local socket = require("socket") |
| 10 |
local coroutine = require("coroutine") |
| 11 |
module("dispatch") |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
TIMEOUT = 60 |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
local handlert = {} |
| 25 |
|
| 26 |
|
| 27 |
function newhandler(mode) |
| 28 |
mode = mode or "coroutine" |
| 29 |
return handlert[mode]() |
| 30 |
end |
| 31 |
|
| 32 |
local function seqstart(self, func) |
| 33 |
return func() |
| 34 |
end |
| 35 |
|
| 36 |
|
| 37 |
function handlert.sequential() |
| 38 |
return { |
| 39 |
tcp = socket.tcp, |
| 40 |
start = seqstart |
| 41 |
} |
| 42 |
end |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
if string.sub(base._VERSION, -3) == "5.1" then |
| 52 |
local function _protect(co, status, ...) |
| 53 |
if not status then |
| 54 |
local msg = ... |
| 55 |
if base.type(msg) == 'table' then |
| 56 |
return nil, msg[1] |
| 57 |
else |
| 58 |
base.error(msg, 0) |
| 59 |
end |
| 60 |
end |
| 61 |
if coroutine.status(co) == "suspended" then |
| 62 |
return _protect(co, coroutine.resume(co, coroutine.yield(...))) |
| 63 |
else |
| 64 |
return ... |
| 65 |
end |
| 66 |
end |
| 67 |
|
| 68 |
function socket.protect(f) |
| 69 |
return function(...) |
| 70 |
local co = coroutine.create(f) |
| 71 |
return _protect(co, coroutine.resume(co, ...)) |
| 72 |
end |
| 73 |
end |
| 74 |
end |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
local function newset() |
| 80 |
local reverse = {} |
| 81 |
local set = {} |
| 82 |
return base.setmetatable(set, {__index = { |
| 83 |
insert = function(set, value) |
| 84 |
if not reverse[value] then |
| 85 |
table.insert(set, value) |
| 86 |
reverse[value] = #set |
| 87 |
end |
| 88 |
end, |
| 89 |
remove = function(set, value) |
| 90 |
local index = reverse[value] |
| 91 |
if index then |
| 92 |
reverse[value] = nil |
| 93 |
local top = table.remove(set) |
| 94 |
if top ~= value then |
| 95 |
reverse[top] = index |
| 96 |
set[index] = top |
| 97 |
end |
| 98 |
end |
| 99 |
end |
| 100 |
}}) |
| 101 |
end |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
local function cowrap(dispatcher, tcp, error) |
| 107 |
if not tcp then return nil, error end |
| 108 |
|
| 109 |
tcp:settimeout(0) |
| 110 |
|
| 111 |
|
| 112 |
local metat = { __index = function(table, key) |
| 113 |
table[key] = function(...) |
| 114 |
return tcp[key](tcp,select(2,...)) |
| 115 |
end |
| 116 |
return table[key] |
| 117 |
end} |
| 118 |
|
| 119 |
local zero = false |
| 120 |
|
| 121 |
local wrap = { } |
| 122 |
|
| 123 |
|
| 124 |
function wrap:settimeout(value, mode) |
| 125 |
if value == 0 then zero = true |
| 126 |
else zero = false end |
| 127 |
return 1 |
| 128 |
end |
| 129 |
|
| 130 |
function wrap:send(data, first, last) |
| 131 |
first = (first or 1) - 1 |
| 132 |
local result, error |
| 133 |
while true do |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
if coroutine.yield(dispatcher.sending, tcp) == "timeout" then |
| 138 |
return nil, "timeout" |
| 139 |
end |
| 140 |
|
| 141 |
result, error, first = tcp:send(data, first+1, last) |
| 142 |
|
| 143 |
|
| 144 |
if error ~= "timeout" then return result, error, first end |
| 145 |
end |
| 146 |
end |
| 147 |
|
| 148 |
|
| 149 |
function wrap:receive(pattern, partial) |
| 150 |
local error = "timeout" |
| 151 |
local value |
| 152 |
while true do |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
if coroutine.yield(dispatcher.receiving, tcp) == "timeout" then |
| 157 |
return nil, "timeout" |
| 158 |
end |
| 159 |
|
| 160 |
value, error, partial = tcp:receive(pattern, partial) |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
if (error ~= "timeout") or zero then |
| 165 |
return value, error, partial |
| 166 |
end |
| 167 |
end |
| 168 |
end |
| 169 |
|
| 170 |
function wrap:connect(host, port) |
| 171 |
local result, error = tcp:connect(host, port) |
| 172 |
if error == "timeout" then |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
if coroutine.yield(dispatcher.sending, tcp) == "timeout" then |
| 178 |
return nil, "timeout" |
| 179 |
end |
| 180 |
|
| 181 |
result, error = tcp:connect(host, port) |
| 182 |
if result or error == "already connected" then return 1 |
| 183 |
else return nil, "non-blocking connect failed" end |
| 184 |
else return result, error end |
| 185 |
end |
| 186 |
|
| 187 |
function wrap:accept() |
| 188 |
while 1 do |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
if coroutine.yield(dispatcher.receiving, tcp) == "timeout" then |
| 194 |
return nil, "timeout" |
| 195 |
end |
| 196 |
local client, error = tcp:accept() |
| 197 |
if error ~= "timeout" then |
| 198 |
return cowrap(dispatcher, client, error) |
| 199 |
end |
| 200 |
end |
| 201 |
end |
| 202 |
|
| 203 |
function wrap:close() |
| 204 |
dispatcher.stamp[tcp] = nil |
| 205 |
dispatcher.sending.set:remove(tcp) |
| 206 |
dispatcher.sending.cortn[tcp] = nil |
| 207 |
dispatcher.receiving.set:remove(tcp) |
| 208 |
dispatcher.receiving.cortn[tcp] = nil |
| 209 |
return tcp:close() |
| 210 |
end |
| 211 |
return base.setmetatable(wrap, metat) |
| 212 |
end |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
local cometat = { __index = {} } |
| 219 |
|
| 220 |
function schedule(cortn, status, operation, tcp) |
| 221 |
if status then |
| 222 |
if cortn and operation then |
| 223 |
operation.set:insert(tcp) |
| 224 |
operation.cortn[tcp] = cortn |
| 225 |
operation.stamp[tcp] = socket.gettime() |
| 226 |
end |
| 227 |
else base.error(operation) end |
| 228 |
end |
| 229 |
|
| 230 |
function kick(operation, tcp) |
| 231 |
operation.cortn[tcp] = nil |
| 232 |
operation.set:remove(tcp) |
| 233 |
end |
| 234 |
|
| 235 |
function wakeup(operation, tcp) |
| 236 |
local cortn = operation.cortn[tcp] |
| 237 |
|
| 238 |
if cortn then |
| 239 |
kick(operation, tcp) |
| 240 |
return cortn, coroutine.resume(cortn) |
| 241 |
|
| 242 |
else |
| 243 |
return nil, true |
| 244 |
end |
| 245 |
end |
| 246 |
|
| 247 |
function abort(operation, tcp) |
| 248 |
local cortn = operation.cortn[tcp] |
| 249 |
if cortn then |
| 250 |
kick(operation, tcp) |
| 251 |
coroutine.resume(cortn, "timeout") |
| 252 |
end |
| 253 |
end |
| 254 |
|
| 255 |
|
| 256 |
function cometat.__index:step() |
| 257 |
|
| 258 |
local readable, writable = socket.select(self.receiving.set, |
| 259 |
self.sending.set, 1) |
| 260 |
|
| 261 |
|
| 262 |
for _, tcp in base.ipairs(readable) do |
| 263 |
schedule(wakeup(self.receiving, tcp)) |
| 264 |
end |
| 265 |
|
| 266 |
for _, tcp in base.ipairs(writable) do |
| 267 |
schedule(wakeup(self.sending, tcp)) |
| 268 |
end |
| 269 |
|
| 270 |
|
| 271 |
local now = socket.gettime() |
| 272 |
for tcp, stamp in base.pairs(self.stamp) do |
| 273 |
if tcp.class == "tcp{client}" and now - stamp > TIMEOUT then |
| 274 |
abort(self.sending, tcp) |
| 275 |
abort(self.receiving, tcp) |
| 276 |
end |
| 277 |
end |
| 278 |
end |
| 279 |
|
| 280 |
function cometat.__index:start(func) |
| 281 |
local cortn = coroutine.create(func) |
| 282 |
schedule(cortn, coroutine.resume(cortn)) |
| 283 |
end |
| 284 |
|
| 285 |
function handlert.coroutine() |
| 286 |
local stamp = {} |
| 287 |
local dispatcher = { |
| 288 |
stamp = stamp, |
| 289 |
sending = { |
| 290 |
name = "sending", |
| 291 |
set = newset(), |
| 292 |
cortn = {}, |
| 293 |
stamp = stamp |
| 294 |
}, |
| 295 |
receiving = { |
| 296 |
name = "receiving", |
| 297 |
set = newset(), |
| 298 |
cortn = {}, |
| 299 |
stamp = stamp |
| 300 |
}, |
| 301 |
} |
| 302 |
function dispatcher.tcp() |
| 303 |
return cowrap(dispatcher, socket.tcp()) |
| 304 |
end |
| 305 |
return base.setmetatable(dispatcher, cometat) |
| 306 |
end |
| 307 |
|