-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
53 lines (47 loc) · 1.16 KB
/
server.js
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
var TCPWrap = process.binding('tcp_wrap')
var net = require('net')
var Handle = require('./handle')
function noop () {}
module.exports = function (onConnect) {
var server = TCPWrap.constants ? new TCPWrap.TCP(TCPWrap.constants.SERVER) : new TCPWrap.TCP()
return {
listen: function (port, addr, cb) {
cb = cb || noop
var err
if (net.isIPv6(addr)) {
err = server.bind6(addr, port)
} else {
err = server.bind(addr, port)
}
if (err) {
server.close()
cb(err)
return
}
// 512 connections allowed in backlog
server.listen(511)
server.onconnection = function (err, client) {
if (err) {
return console.error(new Error('error connected:' + err))
}
onConnect(Handle(client, noop))
}
return server
},
address: function () {
if (server && server.getsockname) {
var out = {}
server.getsockname(out)
return out
} else if (this._pipeName) {
return this._pipeName
} else {
return null
}
},
close: function (cb) {
server.close(cb)
return server
}
}
}