-
Notifications
You must be signed in to change notification settings - Fork 0
/
netevt-socket.mjs
301 lines (231 loc) · 6.02 KB
/
netevt-socket.mjs
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
/**
* Author: JCloudYu
* Create: 2019/04/05
**/
import net from "net";
import events from "events";
const {Socket} = net;
const {EventEmitter} = events;
const _ZERO_BUFFER = Buffer.alloc(0);
const _MSG_BATCH_SIZE = 20;
const _MSG_SCHEDULER = new Set();
const _MSG_TIMEOUT = (()=>{
let _active_timeout = null;
return (callback, delay)=>{
if ( _active_timeout ) {
clearTimeout(_active_timeout);
}
return _active_timeout = setTimeout(callback, delay);
};
})();
const RESERVED_EVENT_NAMES = [ 'netevt', 'disconnected', 'connected', 'error' ];
export class IPCEvtSocket extends EventEmitter {
constructor(socket=null, serverInst=null) {
super();
this._socket = socket || new Socket();
this._socket._rel = this;
this._parent = serverInst || null;
this._chunk = _ZERO_BUFFER;
this._connected = false;
this._error = null;
this._serializer = ___NO_CONVERT;
this._deserializer = ___NO_CONVERT;
this._netevt_handler = ___HANDLE_NETEVT;
this._socket
.on( 'connect', ___HANDLE_CONNECT )
.on( 'close', ___HANDLE_CLOSE )
.on( 'error', ___HANDLE_ERROR )
.on( 'data', ___HANDLE_DATA );
}
connect(...args) {
return this._socket.connect(...args);
}
close() {
return this._socket.end();
}
emit(event, ...evtArgs) {
const {event:eventBuffer, args} = this._gen_raw_event(event, evtArgs);
this._send_raw_event(eventBuffer, args);
this._netevt_handler(this, event, args);
return true;
}
_emit(event, ...evtArgs) {
super.emit(event, ...evtArgs);
}
_gen_raw_event(event, evt_args=[]) {
event = `${event}`;
if ( RESERVED_EVENT_NAMES.indexOf(event) >= 0 ) {
throw new RangeError( `Event \`${event}\` is not allowed!` );
}
const rawData = [];
for(let data of evt_args) {
const raw = this._serializer(data);
if ( !Buffer.isBuffer(raw) ) {
throw new RangeError( `Given event argument must be an buffer!` );
}
rawData.push(raw);
}
return {
event: Buffer.from(event, 'utf8'),
args: rawData
};
}
_send_raw_event(rawEvent, rawData) {
if ( !Buffer.isBuffer(rawEvent) ) {
throw new RangeError( `First argument must be buffers!` );
}
for(const raw of rawData) {
if ( !Buffer.isBuffer(raw) ) {
throw new RangeError( `Second argument must be array of buffers!` );
}
}
const eventHeader = Buffer.alloc(2);
eventHeader.writeUInt16LE(rawEvent.length, 0);
const dataHeader = Buffer.alloc(1);
dataHeader.writeUInt8(rawData.length, 0);
// Write everything
this._socket.write(eventHeader);
this._socket.write(rawEvent);
this._socket.write(dataHeader);
for(const raw of rawData) {
const len = Buffer.alloc(4);
len.writeUInt32LE(raw.length, 0);
this._socket.write(len);
this._socket.write(raw);
}
}
get lastError() {
return this._error;
}
get connected() {
return this._connected;
}
get connecting() {
return this._socket.connecting;
}
}
// region [ Socket Event Handler ]
function ___HANDLE_CONNECT() {
// NOTE: This event will only occurs in socket.connect
const INST = this._rel;
INST._connected = true;
INST._emit( 'connected' );
}
/**
* @param {Boolean} withError
* @private
**/
function ___HANDLE_CLOSE(withError) {
const INST = this._rel;
INST._connected = false;
INST._emit( 'disconnected', withError ? (INST._error||null) : null );
}
/**
* @param {Error} error
* @private
**/
function ___HANDLE_ERROR(error) {
const INST = this._rel;
INST._connected = false;
INST._error = error;
INST._emit( 'error', error );
}
/**
* @param {Buffer} chunk
* @private
**/
function ___HANDLE_DATA(chunk) {
const INST = this._rel;
INST._chunk = Buffer.concat([INST._chunk, chunk]);
___SCHEDULE_EVENT_PROCESSOR(INST);
}
// endregion
// region [ Socket Data Processing Logic ]
/**
* @param {IPCEvtSocket} socket_inst
* @private
**/
function ___SCHEDULE_EVENT_PROCESSOR(socket_inst) {
_MSG_SCHEDULER.add(socket_inst);
_MSG_TIMEOUT(___PROCESS_MESSAGE);
}
function ___PROCESS_MESSAGE() {
for(let [instance] of _MSG_SCHEDULER.entries()) {
let repeat = _MSG_BATCH_SIZE;
let messages = [];
while ( repeat-- > 0 ) {
let result = ___EAT_MESSAGE(instance._chunk);
if ( !result ) {
_MSG_SCHEDULER.delete(instance);
break;
}
const {event, args, anchor} = result;
instance._chunk = instance._chunk.slice(anchor);
messages.push({event, args});
}
// Emit message
for (let {event, args} of messages) {
instance._netevt_handler(instance, event, args);
}
}
if ( _MSG_SCHEDULER.size > 0 ) {
_MSG_TIMEOUT(___PROCESS_MESSAGE);
}
}
function ___EAT_MESSAGE(chunk) {
if ( chunk.length <= 0 ) {
return false;
}
let content, result, anchor=0;
result = ___EAT_EVENT_TAG(chunk, anchor);
if ( !result ) { return false; }
({content, anchor} = result);
let event = content.toString( 'utf8' );
result = ___EAT_EVENT_ARGS(chunk, anchor);
if ( !result ) { return false; }
({content, anchor} = result);
return { event, args:content, anchor };
}
function ___EAT_EVENT_TAG(buff, anchor) {
if ( buff.length < (anchor + 2) ) {
return false;
}
let contentLength = buff.readUInt16LE(anchor);
anchor += 2;
if ( buff.length < (anchor + contentLength)) {
return false;
}
let content = buff.slice(anchor, anchor+contentLength);
anchor += contentLength;
return {content, anchor};
}
function ___EAT_EVENT_ARGS(buff, anchor) {
if ( buff.length < (anchor + 1) ) {
return false;
}
let contentLength = buff.readUInt8(anchor);
anchor += 1;
const rawArgs = [];
while( contentLength-- > 0 ) {
if ( buff.length < (anchor + 4) ) {
return false
}
const argLength = buff.readUInt32LE(anchor);
anchor += 4;
if ( buff.length < (anchor + argLength)) {
return false;
}
rawArgs.push(buff.slice(anchor, anchor + argLength));
anchor += argLength;
}
return {content:rawArgs, anchor};
}
function ___HANDLE_NETEVT(sender, event, raw_data) {
const args = [];
for(let arg of raw_data) {
args.push(sender._deserializer(arg))
}
sender._emit(event, ...args);
}
function ___NO_CONVERT(input) {return input;}
// endregion