/
socket.js
193 lines (157 loc) · 4.22 KB
/
socket.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
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
const { Duplex } = require('stream')
const { isIP } = require('net')
const Denque = require('denque')
const isPort = require('./util/is-port')
const isSocket = require('./util/is-socket')
const pSocket = Symbol('socket')
const pSocketClosed = Symbol('socketClosed')
const pReadQueue = Symbol('readQueue')
const pReadable = Symbol('readable')
const pRemoteAddress = Symbol('remoteAddress')
const pRemotePort = Symbol('remotePort')
const pWritableClosed = Symbol('writableClosed')
const pReadableClosed = Symbol('readableClosed')
const pCloseTransport = Symbol('closeTransport')
/**
* This class implements stream-based dgram socket.
*/
module.exports = class Socket extends Duplex {
constructor(options = {}) {
super(options)
if (!isIP(options.remoteAddress)) {
throw new Error('Option `remoteAddress` should be a valid ip address.')
}
if (!isPort(options.remotePort)) {
throw new Error('Option `remotePort` should be a valid port.')
}
if (!isSocket(options.socket)) {
throw new Error('Option `socket` should be a valid dgram socket.')
}
this[pRemoteAddress] = options.remoteAddress
this[pRemotePort] = options.remotePort
this[pSocket] = options.socket
this[pReadQueue] = denque()
this[pSocketClosed] = false
this[pReadable] = false
this[pWritableClosed] = false
this[pReadableClosed] = false
this[pSocket].on('message', handleMessage.bind(this))
this[pSocket].once('close', () => {
this[pSocketClosed] = true
this.close()
})
this.once('finish', () => {
this[pWritableClosed] = true
})
this[pCloseTransport] = isBoolean(options.closeTransport) ? options.closeTransport : true
}
get remoteAddress() {
return this[pRemoteAddress]
}
get remotePort() {
return this[pRemotePort]
}
get localAddress() {
return this[pSocket].address().address
}
get localPort() {
return this[pSocket].address().port
}
_read() {
if (this[pReadQueue].isEmpty()) {
if (this[pSocketClosed]) {
this.push(null)
this[pReadable] = false
this[pReadableClosed] = true
} else {
this[pReadable] = true
}
} else {
this[pReadable] = this.push(this[pReadQueue].shift())
}
}
_write(chunk, encoding, callback) {
if (this[pWritableClosed]) {
return callback(new Error('Write after free.'))
}
if (!Buffer.isBuffer(chunk)) {
chunk = Buffer.from(chunk, encoding)
}
this[pSocket].send(chunk, this[pRemotePort], this[pRemoteAddress], callback)
}
_destroy(err, callback) {
if (!this[pWritableClosed]) {
this.end()
this[pWritableClosed] = true
}
if (!this[pReadableClosed]) {
this.push(null)
this[pReadableClosed] = true
}
if (!this[pSocketClosed]) {
this[pSocket].close()
this[pSocketClosed] = true
}
this[pReadQueue].clear()
this[pReadable] = false
callback(err)
}
unshift(packet) {
if (!Buffer.isBuffer(packet) || this[pReadableClosed]) {
return false
}
if (this[pReadable]) {
this[pReadable] = this.push(packet)
} else {
this[pReadQueue].unshift(packet)
}
return true
}
close() {
if (!this[pSocketClosed] && this[pCloseTransport]) {
this[pSocket].close()
this[pSocketClosed] = true
}
if (!this[pWritableClosed]) {
this.end()
this[pWritableClosed] = true
}
if (!this[pReadableClosed] && this[pReadQueue].isEmpty()) {
this.push(null)
this[pReadableClosed] = true
}
}
}
/**
* A tiny wrapper for `denque`.
*/
function denque(...args) {
return new Denque(...args)
}
/**
* Handler `message` event.
* @param {Buffer} message
* @param {object} rinfo
*/
function handleMessage(message, rinfo) {
if (this[pReadableClosed]) {
return
}
// Drop messages from unknown sender.
if (rinfo.address !== this[pRemoteAddress] || rinfo.port !== this[pRemotePort]) {
return
}
if (this[pReadable] && this[pReadQueue].isEmpty()) {
this[pReadable] = this.push(message)
} else {
this[pReadQueue].push(message)
}
}
/**
* Check if argument is boolean.
* @param {any} flag
* @returns {boolean}
*/
function isBoolean(flag) {
return typeof flag === 'boolean'
}