|
1 | | -local socket = require('socket.core') |
| 1 | +local ffi = require'ffi' |
| 2 | +local C = ffi.C |
| 3 | +ffi.cdef[[ |
| 4 | + void *malloc(size_t); |
| 5 | + void *realloc(void *, size_t); |
| 6 | + void free(void *); |
| 7 | + |
| 8 | + void Sleep(unsigned long); |
| 9 | + |
| 10 | + struct WSADATA { |
| 11 | + short wLoVer, wHiVer; |
| 12 | + unsigned short iMaxSock; |
| 13 | + long dont_care[128]; |
| 14 | + }; |
| 15 | + |
| 16 | + int WSAStartup(short, struct WSADATA *); |
| 17 | + void WSACleanup(); |
| 18 | + unsigned long WSAGetLastError(); |
| 19 | + |
| 20 | + struct sockaddr { |
| 21 | + unsigned short sa_family; |
| 22 | + char sa_data[14]; |
| 23 | + }; |
| 24 | + |
| 25 | + struct in_addr { |
| 26 | + unsigned long s_addr; |
| 27 | + }; |
| 28 | + |
| 29 | + struct sockaddr_in { |
| 30 | + short sin_family; |
| 31 | + unsigned short sin_port; |
| 32 | + struct in_addr sin_addr; |
| 33 | + char sin_zero[8]; |
| 34 | + }; |
| 35 | + |
| 36 | + unsigned short htons(unsigned short); |
| 37 | + unsigned long htonl(unsigned long); |
| 38 | + int inet_pton(int, const char *, void *); |
| 39 | + |
| 40 | + int socket(int, int, int); |
| 41 | + int bind(int, const struct sockaddr *, int); |
| 42 | + int listen(int, int); |
| 43 | + int accept(int); |
| 44 | + int recv(int, char *, int, int); |
| 45 | + int send(int, const char *, int, int); |
| 46 | + int setsockopt(int, int, int, const void *, int); |
| 47 | + int ioctlsocket(int, long, unsigned long *); |
| 48 | + void closesocket(int); |
| 49 | + |
| 50 | + struct sockbuf { |
| 51 | + unsigned int size, pos; |
| 52 | + char *ptr; |
| 53 | + }; |
| 54 | +]] |
| 55 | +local lib = ffi.load'ws2_32' |
| 56 | +local wdata = ffi.new('struct WSADATA') |
| 57 | +if lib.WSAStartup(0x0202, wdata) ~= 0 then |
| 58 | + error('WSAStartup failed') |
| 59 | +end |
| 60 | + |
2 | 61 | local httpcodes = require('httpcodes') |
3 | 62 | local no_body = { |
4 | 63 | ['GET'] = true, ['DELETE'] = true, |
@@ -52,21 +111,131 @@ local _M = { |
52 | 111 | } |
53 | 112 | _M.__index = _M |
54 | 113 |
|
55 | | -local function sassert(fd, ret, err) |
56 | | - if not ret then |
57 | | - fd:close() |
| 114 | +local function ensure(buf, need) |
| 115 | + if buf.size - buf.pos < need then |
| 116 | + local newsz = buf.size + need + 256 |
| 117 | + buf.ptr = C.realloc(buf.ptr, newsz) |
| 118 | + assert(buf.ptr ~= nil, 'realloc() failed') |
| 119 | + buf.size = newsz |
| 120 | + end |
| 121 | +end |
| 122 | + |
| 123 | +local sock_meta = { |
| 124 | + init = function(self, fd) |
| 125 | + self.fd = fd |
| 126 | + local val = ffi.new('long[1]', 0) |
| 127 | + if lib.setsockopt(fd, 0xffff, 0x0004, val, 4) ~= 0 then -- REUSEADDR |
| 128 | + error('setsockopt failed') |
| 129 | + end |
| 130 | + val[0] = 1 |
| 131 | + if lib.ioctlsocket(fd, -2147195266, val) ~= 0 then -- FIONBIO |
| 132 | + print(lib.WSAGetLastError()) |
| 133 | + error('ioctlsocket failed') |
| 134 | + end |
| 135 | + end, |
| 136 | + bind = function(self, addr) |
| 137 | + if lib.bind(self.fd, ffi.cast('const struct sockaddr *', addr), ffi.sizeof(addr)) ~= 0 then |
| 138 | + print(lib.WSAGetLastError()) |
| 139 | + error('bind() failed') |
| 140 | + end |
| 141 | + if lib.listen(self.fd, 128) ~= 0 then |
| 142 | + print(lib.WSAGetLastError()) |
| 143 | + error('listen() failed') |
| 144 | + end |
| 145 | + end, |
| 146 | + receive = function(self, t) |
| 147 | + if self._sockbuf == nil then |
| 148 | + self._sockbuf = ffi.new('struct sockbuf', { |
| 149 | + 32, 0, C.malloc(32) |
| 150 | + }) |
| 151 | + end |
| 152 | + |
| 153 | + if t == '*l' then |
| 154 | + local len |
| 155 | + repeat |
| 156 | + ensure(self._sockbuf, 1) |
| 157 | + len = lib.recv(self.fd, self._sockbuf.ptr + self._sockbuf.pos, 1, 0) |
| 158 | + if len > 0 then |
| 159 | + local ch = self._sockbuf.ptr[self._sockbuf.pos] |
| 160 | + if ch == 10 then |
| 161 | + local e = self._sockbuf.pos |
| 162 | + self._sockbuf.pos = 0 |
| 163 | + return ffi.string(self._sockbuf.ptr, e) |
| 164 | + elseif ch ~= 13 then |
| 165 | + self._sockbuf.pos = self._sockbuf.pos + 1 |
| 166 | + end |
| 167 | + end |
| 168 | + until len == -1 or len == 0 |
| 169 | + |
| 170 | + return nil, 'timeout' |
| 171 | + elseif t == '*a' then |
| 172 | + ensure(self._sockbuf, 128) |
| 173 | + local ret |
| 174 | + repeat |
| 175 | + ret = lib.recv(self.fd, self._sockbuf.ptr + self._sockbuf.pos, 128, 0) |
| 176 | + if ret > 0 then self._sockbuf.pos = self._sockbuf.pos + ret end |
| 177 | + until ret == -1 or ret == 0 |
| 178 | + |
| 179 | + return self._sockbuf.pos |
| 180 | + elseif type(t) == 'number' then |
| 181 | + ensure(self._sockbuf, t) |
| 182 | + local len = lib.recv(self.fd, self._sockbuf.ptr, t, 0) |
| 183 | + return ffi.string(self._sockbuf.ptr, len) |
| 184 | + end |
| 185 | + |
| 186 | + return nil, 'fuck' |
| 187 | + end, |
| 188 | + send = function(self, data, from) |
| 189 | + from = from or 0 |
| 190 | + local bs = #data - from |
| 191 | + local sent = from |
| 192 | + if bs > 0 then |
| 193 | + data = ffi.cast('char *', data) + from |
| 194 | + local ret = lib.send(self.fd, data, bs, 0) |
| 195 | + if ret < 0 then return 0, 'timeout', from end |
| 196 | + from = from + ret |
| 197 | + bs = bs - ret |
| 198 | + end |
| 199 | + |
| 200 | + local err |
| 201 | + if bs ~= 0 then |
| 202 | + err = 'timeout' |
| 203 | + else |
| 204 | + from = nil |
| 205 | + end |
| 206 | + |
| 207 | + return sent, err, from |
| 208 | + end, |
| 209 | + close = function(self) |
| 210 | + lib.closesocket(self.fd) |
| 211 | + if self._sockbuf then |
| 212 | + C.free(self._sockbuf.ptr) |
| 213 | + self._sockbuf.ptr = nil |
| 214 | + end |
58 | 215 | end |
59 | | - assert(ret, err) |
| 216 | +} |
| 217 | +sock_meta.__index = sock_meta |
| 218 | +sock_meta.accept = function(self) |
| 219 | + local cfd = lib.accept(self.fd) |
| 220 | + if cfd == -1 then return nil, true end |
| 221 | + local cl = setmetatable({}, sock_meta) |
| 222 | + cl:init(cfd) |
| 223 | + return cl |
60 | 224 | end |
61 | 225 |
|
62 | 226 | local function binder(ip, port) |
63 | 227 | if ip == '*' then ip = '0.0.0.0' end |
64 | | - local fd = assert(socket.tcp()) |
65 | | - fd:setoption('reuseaddr', true) |
66 | | - sassert(fd, fd:bind(ip, port)) |
67 | | - sassert(fd, fd:listen()) |
68 | | - fd:settimeout(0) |
69 | | - return fd |
| 228 | + local addr = ffi.new('struct sockaddr_in', { |
| 229 | + sin_family = 2, -- AF_INET |
| 230 | + sin_port = lib.htons(port), |
| 231 | + }) |
| 232 | + if lib.inet_pton(addr.sin_family, ip, addr.sin_addr) ~= 1 then |
| 233 | + error('Invalid IP specified') |
| 234 | + end |
| 235 | + local sock = setmetatable({}, sock_meta) |
| 236 | + sock:init(lib.socket(addr.sin_family, 1, 6)) |
| 237 | + sock:bind(addr) |
| 238 | + return sock |
70 | 239 | end |
71 | 240 |
|
72 | 241 | local function waitForLine(cl) |
@@ -295,7 +464,6 @@ function _M:doStep() |
295 | 464 |
|
296 | 465 | break |
297 | 466 | end |
298 | | - client:settimeout(0) |
299 | 467 | self.clients[client] = coroutine.create(self.clientHandler) |
300 | 468 | end |
301 | 469 |
|
|
322 | 490 |
|
323 | 491 | function _M:startLoop() |
324 | 492 | while self:doStep() do |
325 | | - socket.sleep(0.01) |
| 493 | + C.Sleep(10) |
326 | 494 | end |
327 | 495 | end |
328 | 496 |
|
|
0 commit comments