This repository was archived by the owner on Oct 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
155 lines (128 loc) · 3.04 KB
/
index.js
File metadata and controls
155 lines (128 loc) · 3.04 KB
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
const EventEmitter = require("events");
/**
* Send and receive messages via socket
*/
class SocketWrapper extends EventEmitter {
/**
* Create instance
* @param {object} [socket] Socket to attach to
*/
constructor(socket) {
super();
this._socket = null;
this._listener = null;
this._incoming = new Buffer(0);
this._outgoing = new Buffer(0);
this._writing = false;
if (socket) this.attach(socket);
}
/**
* Get incoming buffer
* @type {Buffer|null}
*/
get incoming() {
return this._incoming;
}
/**
* Get outgoing buffer
* @type {Buffer|null}
*/
get outgoing() {
return this._outgoing;
}
/**
* Is wrapper attached to a socket?
* @return {boolean}
*/
get isAttached() {
return this._socket !== null;
}
/**
* Attach wrapper to a socket
* @param {object} socket Socket to attach to
*/
attach(socket) {
if (this.isAttached) return;
let onData = data => {
if (!this._incoming) return;
if (data) {
this._incoming = Buffer.concat([this._incoming, data]);
this.emit("read", data);
}
while (this._incoming.length >= 4) {
let size = this._incoming.readUInt32BE(0);
if (this._incoming.length < 4 + size) break;
let message = this._incoming.slice(4, 4 + size);
this._incoming = this._incoming.slice(4 + size);
this.emit("receive", message);
}
};
this._socket = socket;
this._listener = onData.bind(this);
socket.on("data", this._listener);
if (this._outgoing.length) this._write();
}
/**
* Detach from a socket
*/
detach() {
if (!this.isAttached) return;
this._socket.removeListener("data", this._listener);
this._socket = null;
this._listener = null;
this.clear();
}
/**
* Send data
* @param {Buffer} [data] Data to send
*/
send(data) {
if (!this._outgoing) return;
if (data) {
let message = Buffer.allocUnsafe(4 + data.length);
message.writeUInt32BE(data.length, 0);
data.copy(message, 4);
this._outgoing = Buffer.concat([this._outgoing, message]);
this.emit("send", data);
}
this._write();
}
/**
* Clear buffers
*/
clear() {
if (this._incoming) this._incoming = new Buffer(0);
if (this._outgoing) this._outgoing = new Buffer(0);
}
/**
* Destroy wrapper
*/
destroy() {
this.detach();
this._incoming = null;
this._outgoing = null;
}
/**
* Actually send data via socket
*/
_write() {
if (this._writing || !this._socket || !this._outgoing) return;
if (this._socket.destroyed) {
this.detach();
return;
}
if (!this._outgoing.length) {
this.emit("flush");
return;
}
this._writing = true;
let buffer = this._outgoing.slice();
this._outgoing = new Buffer(0);
this.emit("write", buffer);
this._socket.write(buffer, undefined, () => {
this._writing = false;
this._write();
});
}
}
module.exports = SocketWrapper;