-
Notifications
You must be signed in to change notification settings - Fork 374
/
net.lua
389 lines (323 loc) · 8.31 KB
/
net.lua
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
--[[
Copyright 2014-2015 The Luvit Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS-IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--]]
--[[lit-meta
name = "luvit/net"
version = "2.0.4"
dependencies = {
"luvit/timer@2.0.0",
"luvit/utils@2.0.0",
"luvit/core@2.0.0",
"luvit/stream@2.0.0",
}
license = "Apache 2"
homepage = "https://github.com/luvit/luvit/blob/master/deps/net.lua"
description = "Node-style net client and server module for luvit"
tags = {"luvit", "tcp", "pipe", "stream"}
]]
local uv = require('uv')
local timer = require('timer')
local utils = require('utils')
local Emitter = require('core').Emitter
local Duplex = require('stream').Duplex
--[[ Socket ]]--
local Socket = Duplex:extend()
function Socket:initialize(options)
Duplex.initialize(self)
if type(options) == 'number' then
options = { fd = options }
elseif options == nil then
options = {}
end
if options.handle then
self._handle = options.handle
elseif options.fd then
local typ = uv.guess_handle(options.fd);
if typ == 'TCP' then
self._handle = uv.new_tcp()
elseif typ == 'PIPE' then
self._handle = uv.new_pipe()
end
end
self._connecting = false
self._reading = false
self._destroyed = false
self:on('finish', utils.bind(self._onSocketFinish, self))
self:on('_socketEnd', utils.bind(self._onSocketEnd, self))
end
function Socket:_onSocketFinish()
if self._connecting then
return self:once('connect', utils.bind(self._onSocketFinish, self))
end
if not self.readable then
return self:destroy()
end
end
function Socket:_onSocketEnd()
self:once('end', function()
self:destroy()
end)
end
function Socket:bind(ip, port)
uv.tcp_bind(self._handle, ip, tonumber(port))
end
function Socket:address()
return uv.tcp_getpeername(self._handle)
end
function Socket:setTimeout(msecs, callback)
if msecs > 0 then
timer.enroll(self, msecs)
timer.active(self)
if callback then self:once('timeout', callback) end
elseif msecs == 0 then
timer.unenroll(self)
end
end
function Socket:_write(data, callback)
if not self._handle then return end
uv.write(self._handle, data, function(err)
if err then
self:destroy(err)
return callback(err)
end
callback()
end)
end
function Socket:_read(n)
local onRead
function onRead(err, data)
timer.active(self)
if err then
return self:destroy(err)
elseif data then
self:push(data)
else
self:push(nil)
self:emit('_socketEnd')
end
end
if self._connecting then
self:once('connect', utils.bind(self._read, self, n))
elseif not self._reading then
self._reading = true
uv.read_start(self._handle, onRead)
end
end
function Socket:shutdown(callback)
if self.destroyed == true and callback then
return callback()
end
if uv.is_closing(self._handle) and callback then
return callback()
end
uv.shutdown(self._handle, callback)
end
function Socket:nodelay(enable)
uv.tcp_nodelay(self._handle, enable)
end
function Socket:keepalive(enable, delay)
uv.tcp_keepalive(self._handle, enable, delay)
end
function Socket:getSendBufferSize()
return uv.send_buffer_size(self._handle)
end
function Socket:getRecvBufferSize()
return uv.recv_buffer_size(self._handle)
end
function Socket:setSendBufferSize(size)
assert(type(size) == "number" and size > 0, "Size must be a number greater than 0")
return uv.send_buffer_size(self._handle, size)
end
function Socket:setRecvBufferSize(size)
assert(type(size) == "number" and size > 0, "Size must be a number greater than 0")
return uv.recv_buffer_size(self._handle, size)
end
function Socket:pause()
Duplex.pause(self)
if not self._handle then return end
self._reading = false
uv.read_stop(self._handle)
end
function Socket:resume()
Duplex.resume(self)
self:_read(0)
end
function Socket:connect(...)
local args = {...}
local options = {}
local callback
if type(args[1]) == 'table' then
-- connect(options, [cb])
options = args[1]
callback = args[2]
else
-- connect(port, [host], [cb])
options.port = args[1]
if type(args[2]) == 'string' then
options.host = args[2];
callback = args[3]
else
callback = args[2]
end
end
callback = callback or function() end
if not options.host then
options.host = '0.0.0.0'
end
timer.active(self)
self._connecting = true
if not self._handle then
self._handle = uv.new_tcp()
end
local _, derr = uv.getaddrinfo(options.host, options.port, { socktype = "stream" }, function(err, res)
timer.active(self)
if err then
return self:destroy(err)
end
if self.destroyed then return end
local _, terr = uv.tcp_connect(self._handle, res[1].addr, res[1].port, function(err)
if err then
return self:destroy(err)
end
timer.active(self)
self._connecting = false
self:emit('connect')
if callback then callback() end
end)
if terr ~= nil then
self:destroy(terr)
end
end)
if derr ~= nil then
return self:destroy(derr)
end
return self
end
function Socket:destroy(exception, callback)
callback = callback or function() end
if self.destroyed == true or self._handle == nil then
return callback()
end
timer.unenroll(self)
self.destroyed = true
self.readable = false
self.writable = false
if uv.is_closing(self._handle) then
timer.setImmediate(callback)
else
uv.close(self._handle, function()
self:emit('close')
callback()
end)
end
if exception then
process.nextTick(function()
self:emit('error', exception)
end)
end
end
function Socket:listen(queueSize)
local onListen
queueSize = queueSize or 128
function onListen()
local client = uv.new_tcp()
uv.tcp_keepalive(client, true, 60)
uv.accept(self._handle, client)
self:emit('connection', Socket:new({ handle = client }))
end
uv.listen(self._handle, queueSize, onListen)
end
function Socket:getsockname()
return uv.tcp_getsockname(self._handle)
end
--[[ Server ]]--
local Server = Emitter:extend()
function Server:init(options, connectionListener)
if type(options) == 'function' then
connectionListener = options
options = {}
end
self._connectionListener = connectionListener
self:on('connection', self._connectionListener)
if options.handle then
self._handle = options.handle
end
end
function Server:destroy(err, callback)
self._handle:destroy(err, callback)
end
function Server:listen(port, ... --[[ ip, callback --]] )
local args = {...}
local ip, callback
if not self._handle then
self._handle = Socket:new({ handle = uv.new_tcp() })
end
-- Future proof
if type(args[1]) == 'function' then
callback = args[1]
else
ip = args[1]
callback = args[2]
end
ip = ip or '0.0.0.0'
self._handle:bind(ip, port)
self._handle:listen()
self._handle:on('connection', function(client)
self:emit('connection', client)
end)
if callback then
timer.setImmediate(callback)
end
return self
end
function Server:address()
if self._handle then
return self._handle:getsockname()
end
return
end
function Server:close(callback)
self:destroy(nil, callback)
end
-- Exports
local function createConnection(port, ... --[[ host, cb --]])
local args = {...}
local host
local options
local callback
local sock
-- future proof
if type(port) == 'table' then
options = port
port = options.port
host = options.host
callback = args[1]
else
host = args[1]
callback = args[2]
end
sock = Socket:new()
sock:connect(port, host, callback)
return sock
end
local function createServer(options, connectionListener)
local server = Server:new()
server:init(options, connectionListener)
return server
end
return {
Server = Server,
Socket = Socket,
createConnection = createConnection,
connect = createConnection,
createServer = createServer,
}