-
Notifications
You must be signed in to change notification settings - Fork 3
/
mtcp.js
346 lines (346 loc) · 11.5 KB
/
mtcp.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
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MSocket = void 0;
exports.createMTcpServer = createMTcpServer;
exports.connectMTcp = connectMTcp;
const net_1 = require("net");
const stream_1 = require("stream");
const HEADER_LENGTH = 4;
var PackageIndex;
(function (PackageIndex) {
PackageIndex[PackageIndex["length"] = 0] = "length";
PackageIndex[PackageIndex["pid"] = 2] = "pid";
})(PackageIndex || (PackageIndex = {}));
class MSocket extends stream_1.Duplex {
static PoolCount = 2;
cid;
poolCount = MSocket.PoolCount;
conns = [];
pendingConns = [];
readyState = "opening";
bytesRead = 0;
writableLength = 0;
bytesWritten = 0;
remoteAddress = "";
remotePort;
keepAlive = false;
keepAliveDelay = 0;
noDelay = false;
constructor(opts) {
super({
highWaterMark: 1,
allowHalfOpen: false, //和net.socket差不多
...opts,
});
}
setKeepAlive(enable = false, initialDelay = 0) {
this.keepAlive = enable;
this.keepAliveDelay = initialDelay;
for (let conn of this.conns)
conn.setKeepAlive(this.keepAlive, this.keepAliveDelay);
return this;
}
setNoDelay(noDelay = true) {
this.conns.forEach(conn => conn.setNoDelay(noDelay));
this.noDelay = noDelay;
return this;
}
//检查Alive,当所有的连接都被关闭时,加入end包,触发end事件
checkAlive() {
if (this.destroyed)
return;
if (this.conns.length)
return;
if (this.readableEnded)
return;
function closeConn(conn) {
conn.removeAllListeners("data");
conn.destroySoon();
}
this.pendingConns.forEach(closeConn);
this.pendingConns.length = 0;
//标记结束包(fin包)(标记可读的下一个包为end)
let nextPid = this.readPid;
while (this.packages[nextPid]) {
nextPid++;
if (nextPid === 65536)
nextPid = 0;
}
this.packages[nextPid] = null;
this._read(0);
}
destroySoon() {
this.destroy();
}
destroy(error) {
for (let conn of this.conns)
conn.destroySoon();
for (let conn of this.pendingConns)
conn.destroySoon();
this.readyState = "closed";
return super.destroy(error);
}
connect(port, host, connectionListener) {
for (let i = 0; i < this.poolCount; i++)
this.connectSub(port, host, connectionListener);
return this;
}
//连接子流
connectSub(port, host, connectionListener) {
//写入自己的id和主id
const writeid = (conn) => {
let buffer = Buffer.alloc(4);
buffer.writeUint16BE(conn.cid + 100);
buffer.writeUint16BE(this.cid, 2);
conn.write(buffer, err => {
if (err)
console.error("login err:", err.message);
else {
if (this.readyState === "opening") {
this.readyState = "open";
this.emit("connect");
if (connectionListener)
connectionListener();
}
}
});
};
let conn = (0, net_1.connect)(port, host);
this.pendingConns.push(conn);
conn.setKeepAlive(true, 10000);
conn.setNoDelay(true);
conn.on("error", err => {
if (this.readyState === "open" || this.readyState === "opening")
console.error(`mtcp conn(cid:${conn.cid},mid:${this.cid}:${port}) link error:${err.message} w:${conn.bytesWritten} r:${conn.bytesRead}`);
});
conn.once("data", (buffer) => {
if (!conn.cid) //未登录
{
conn.cid = buffer.readUint16BE(0);
if (!this.cid)
this.cid = conn.cid; //初始化up_cid
writeid(conn); //写入id 登录
conn.leftBuffer = buffer.subarray(2);
this.pendingConns.splice(this.pendingConns.indexOf(conn), 1);
this.addSocket(conn);
}
});
return this;
}
//连接成功的conn进入这里,接管data end close
//未连接的在pendingConns中
addSocket(conn) {
const read = () => {
while (conn.leftBuffer.length >= HEADER_LENGTH) {
let length = conn.leftBuffer.readUint16BE(PackageIndex.length) + 1;
let end = length + HEADER_LENGTH;
if (conn.leftBuffer.length < end)
break;
let pid = conn.leftBuffer.readUint16BE(PackageIndex.pid);
// console.log('pid: ', pid);
this.packages[pid] = conn.leftBuffer.subarray(HEADER_LENGTH, end);
this._read(0);
conn.leftBuffer = conn.leftBuffer.subarray(end);
}
};
conn.on("data", buffer => {
conn.leftBuffer = Buffer.concat([conn.leftBuffer, buffer]);
read();
});
if (conn.leftBuffer?.length)
read();
const OnClose = () => {
if (conn.deleted)
return;
this.conns.splice(this.conns.indexOf(conn), 1);
conn.deleted = true;
this.checkAlive(); //检查可用性
};
conn.on("end", () => {
OnClose();
});
conn.on("close", () => {
OnClose();
});
conn.on("drain", () => {
conn.full = false;
this.emit("drain");
});
this.conns.push(conn);
conn.setKeepAlive(this.keepAlive, this.keepAliveDelay);
conn.setNoDelay(this.noDelay);
}
pause() {
for (let conn of this.conns)
conn.pause();
return super.pause();
}
resume() {
for (let conn of this.conns)
conn.resume();
return super.resume();
}
//#region 读包与发包索引
packages = [];
readPid = 0;
writePid = 0;
get pid() {
let pid = this.writePid;
this.writePid++;
if (this.writePid === 65536)
this.writePid = 0;
return pid;
}
//#endregion
_read(size) {
while (true) {
let pkg = this.packages[this.readPid];
if (pkg === undefined)
return;
this.push(pkg);
if (pkg)
this.bytesRead += pkg.length;
else
return;
delete this.packages[this.readPid];
this.readPid++;
if (this.readPid === 65536)
this.readPid = 0;
}
}
//写入流:写完了
end(...args) {
if (this.readyState !== "closed")
this.readyState = "readOnly";
// if (args.length === 2 || args.length === 3) console.log(args);
for (let conn of this.conns)
conn.end();
for (let conn of this.pendingConns)
conn.end();
// this.destroy(); 不需要destroy 因为end后 这个连接会从连接池移除. 所有的连接都结束后,自然会销毁
return super.end(...args);
}
_write(buffer, encoding, callback) {
if (this.conns.length === 0) {
this.emit("error", new Error("conns 0"));
return;
}
if (this.readyState !== "open") {
this.emit("error", new Error("not open"));
return;
}
//创建一个打包后的buffer数据
function makePackageBuffer(pid, data) {
const dataLength = data.length;
const buffer = Buffer.alloc(HEADER_LENGTH + dataLength);
buffer.writeUint16BE(pid, PackageIndex.pid);
buffer.writeUint16BE(dataLength - 1, PackageIndex.length);
data.copy(buffer, HEADER_LENGTH);
return buffer;
}
while (buffer.length > 65536) {
this.writeBufferPack(makePackageBuffer(this.pid, buffer.subarray(0, 65536)));
buffer = buffer.subarray(65536);
}
if (buffer.length)
this.writeBufferPack(makePackageBuffer(this.pid, buffer));
if (this.conns.some(conn => !conn.full))
callback();
else
this.once("drain", callback);
}
_final(callback) {
callback();
}
_writeConnIndex = 0;
writeBufferPack(buffer, retryCount = 0) {
if (this.destroyed)
return;
if (this.conns.length === 0) {
this.emit("error", new Error("writeBufferPack conn0"));
return;
}
if (this._writeConnIndex >= this.conns.length)
this._writeConnIndex = 0;
let conn = this.conns[this._writeConnIndex];
if (conn.full) //选择合理的连接
{
this._writeConnIndex++;
if (this._writeConnIndex >= this.conns.length)
this._writeConnIndex = 0;
conn = this.conns[this._writeConnIndex];
}
let sendOk = conn.write(buffer, err => {
if (err) {
console.log("write err:", err.message);
if (retryCount < 2)
this.writeBufferPack(buffer, ++retryCount); //重试
else
this.destroy();
}
else
this.writableLength -= buffer.length; //计算写入数量
});
this.writableLength += buffer.length; //计算写入数量
if (!sendOk)
conn.full = true;
}
}
exports.MSocket = MSocket;
function createMTcpServer(connectionListener) {
let cid_index = 1;
let msocketMap = new Map();
return (0, net_1.createServer)((conn) => {
while (msocketMap.has(cid_index)) {
cid_index++;
if (cid_index === 65000)
cid_index = 1;
}
conn.cid = cid_index;
conn.on("error", (err) => {
console.log(`error:mid:${conn.mid} cid:${conn.cid} ${err.message}`);
});
let buffer = Buffer.alloc(2);
buffer.writeUint16BE(conn.cid);
conn.write(buffer, err => {
if (err)
console.log(`login write error cid:${conn.cid} ${err.message}`);
});
conn.once("data", buffer => {
if (buffer.length < 4) {
conn.end();
return;
}
let id = buffer.readUint16BE();
if (id !== conn.cid + 100) {
conn.end();
return;
}
conn.leftBuffer = buffer.subarray(4);
let mid = buffer.readUint16BE(2);
conn.mid = mid;
let ms = msocketMap.get(mid);
if (!ms) {
ms = new MSocket();
msocketMap.set(mid, ms);
ms.addSocket(conn); //必须提前给它(因为连接成功后 就可以用了 就要发送数据了)
ms.readyState = "open";
if (connectionListener)
connectionListener(ms);
ms.on("close", () => {
msocketMap.delete(mid);
// console.log(`close ms mid:${mid},rem:${msocketMap.size}`);
});
}
else {
if (ms.readyState === "open" || ms.readyState === "opening")
ms.addSocket(conn);
else
console.log("状态错误!", ms.readyState);
}
});
});
}
function connectMTcp(obj, cb) {
return new MSocket().connect(obj.port, obj.host, cb);
}