@aj-shadow/z-abs-funclayer-engine-server 0.0.0-aj-beta.221
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitattributes +26 -0
- package/LICENSE.txt +96 -0
- package/README.md +5 -0
- package/npm-shrinkwrap.json +13 -0
- package/package.json +10 -0
- package/project/server/_build/Server-FuncLayer-Engine-server.bld +12 -0
- package/project/server/_build/z-abs-funclayer-engine-server.prj +12 -0
- package/project/server/address/address-calculator.js +377 -0
- package/project/server/address/address-data.js +364 -0
- package/project/server/address/address-manager.js +251 -0
- package/project/server/address/address.js +176 -0
- package/project/server/address/addresses.js +122 -0
- package/project/server/address/dns-url-cache.js +130 -0
- package/project/server/address/interfaces.js +100 -0
- package/project/server/address/local-dns.js +35 -0
- package/project/server/address/login-report.js +56 -0
- package/project/server/address/networks.js +598 -0
- package/project/server/address/real-networks.js +102 -0
- package/project/server/address/subnet.js +60 -0
- package/project/server/communication/messages/messages-s-to-c/message-execution-started.js +17 -0
- package/project/server/communication/messages/messages-s-to-c/message-execution-stopped.js +14 -0
- package/project/server/communication/messages/messages-s-to-c/message-test-case-debug-paused.js +18 -0
- package/project/server/communication/messages/messages-s-to-c/message-test-case-log.js +25 -0
- package/project/server/communication/messages/messages-s-to-c/message-test-case-started.js +21 -0
- package/project/server/communication/messages/messages-s-to-c/message-test-case-state.js +18 -0
- package/project/server/communication/messages/messages-s-to-c/message-test-case-stopped.js +22 -0
- package/project/server/communication/messages/messages-s-to-c/message-test-stage-started.js +19 -0
- package/project/server/communication/messages/messages-s-to-c/message-test-stage-stopped.js +19 -0
- package/project/server/communication/messages/messages-s-to-c/message-test-suite-started.js +16 -0
- package/project/server/communication/messages/messages-s-to-c/message-test-suite-stopped.js +19 -0
- package/project/server/communication/messages/messages-s-to-w/message-test-case-debug-actor-index-ok.js +15 -0
- package/project/server/communication/messages/messages-w-to-s/message-test-case-debug-actor-index.js +15 -0
- package/project/server/debug/debug-step-over.js +54 -0
- package/project/server/debug/debugger.js +102 -0
- package/project/server/debug-dashboard/debug-dashboard.js +14 -0
- package/project/server/debug-dashboard/gauge-manager.js +44 -0
- package/project/server/debug-dashboard/gauge.js +37 -0
- package/project/server/engine/actor/actor-condition.js +335 -0
- package/project/server/engine/actor/actor-intercepting.js +32 -0
- package/project/server/engine/actor/actor-local.js +24 -0
- package/project/server/engine/actor/actor-mixins/asynch-mixin.js +103 -0
- package/project/server/engine/actor/actor-mixins/content-mixin.js +22 -0
- package/project/server/engine/actor/actor-mixins/logger-mixin.js +603 -0
- package/project/server/engine/actor/actor-mixins/process-mixin.js +35 -0
- package/project/server/engine/actor/actor-mixins/shared-data-mixin.js +109 -0
- package/project/server/engine/actor/actor-mixins/shared-execution-data-mixin.js +57 -0
- package/project/server/engine/actor/actor-mixins/stack-mixin-client.js +157 -0
- package/project/server/engine/actor/actor-mixins/stack-mixin-server.js +123 -0
- package/project/server/engine/actor/actor-mixins/stack-mixin.js +128 -0
- package/project/server/engine/actor/actor-mixins/test-data-mixin.js +402 -0
- package/project/server/engine/actor/actor-mixins/timer-mixin.js +44 -0
- package/project/server/engine/actor/actor-mixins/verify-mixin-base.js +668 -0
- package/project/server/engine/actor/actor-mixins/verify-mixin-mandatory.js +46 -0
- package/project/server/engine/actor/actor-mixins/verify-mixin-optional.js +44 -0
- package/project/server/engine/actor/actor-mixins/verify-mixin-value.js +71 -0
- package/project/server/engine/actor/actor-originating.js +32 -0
- package/project/server/engine/actor/actor-part-mixins/part-asynch-mixin.js +24 -0
- package/project/server/engine/actor/actor-part-mixins/part-logger-mixin.js +39 -0
- package/project/server/engine/actor/actor-part-mixins/part-shared-runtime-data-mixin.js +33 -0
- package/project/server/engine/actor/actor-part-mixins/part-timer-mixin.js +12 -0
- package/project/server/engine/actor/actor-part-mixins/part-verify-mixin-mandatory.js +15 -0
- package/project/server/engine/actor/actor-part-mixins/part-verify-mixin-optional.js +15 -0
- package/project/server/engine/actor/actor-part-mixins/part-verify-mixin-value.js +15 -0
- package/project/server/engine/actor/actor-part-post.js +37 -0
- package/project/server/engine/actor/actor-part-pre.js +37 -0
- package/project/server/engine/actor/actor-pending.js +79 -0
- package/project/server/engine/actor/actor-proxy.js +38 -0
- package/project/server/engine/actor/actor-results.js +36 -0
- package/project/server/engine/actor/actor-state-timeout.js +35 -0
- package/project/server/engine/actor/actor-sut.js +38 -0
- package/project/server/engine/actor/actor-terminating.js +32 -0
- package/project/server/engine/actor/actor.js +553 -0
- package/project/server/engine/actor-phase.js +26 -0
- package/project/server/engine/actor-state-machine-base.js +298 -0
- package/project/server/engine/actor-state-machine-data.js +383 -0
- package/project/server/engine/actor-state-machine-exec.js +137 -0
- package/project/server/engine/actor-state-machine-post.js +177 -0
- package/project/server/engine/actor-state-machine-pre.js +142 -0
- package/project/server/engine/actor-state-machine-process.js +88 -0
- package/project/server/engine/actor-state-machine-state.js +52 -0
- package/project/server/engine/actor-state-machine.js +185 -0
- package/project/server/engine/actor-state.js +35 -0
- package/project/server/engine/actor-worker.js +68 -0
- package/project/server/engine/actors-phase.js +49 -0
- package/project/server/engine/actors.js +317 -0
- package/project/server/engine/data/content-audio.js +18 -0
- package/project/server/engine/data/content-base.js +91 -0
- package/project/server/engine/data/content-binary.js +15 -0
- package/project/server/engine/data/content-cache.js +67 -0
- package/project/server/engine/data/content-data.js +254 -0
- package/project/server/engine/data/content-documents.js +18 -0
- package/project/server/engine/data/content-image.js +20 -0
- package/project/server/engine/data/content-other.js +19 -0
- package/project/server/engine/data/content-text.js +19 -0
- package/project/server/engine/data/content-undefined.js +14 -0
- package/project/server/engine/data/content-video.js +20 -0
- package/project/server/engine/data/content.js +60 -0
- package/project/server/engine/data/runtime-data-shared.js +75 -0
- package/project/server/engine/data/test-data.js +239 -0
- package/project/server/engine/execution-context-test-case.js +48 -0
- package/project/server/engine/execution-context-test-suite.js +66 -0
- package/project/server/engine/execution-context.js +93 -0
- package/project/server/engine/stage-setter.js +132 -0
- package/project/server/engine/stage.js +83 -0
- package/project/server/engine/test-case-load.js +25 -0
- package/project/server/engine/test-case-loader.js +125 -0
- package/project/server/engine/test-case-not-found.js +29 -0
- package/project/server/engine/test-case-result-type.js +42 -0
- package/project/server/engine/test-case.js +417 -0
- package/project/server/engine/test-output.js +116 -0
- package/project/server/engine/test-statistics-test-case.js +59 -0
- package/project/server/engine/test-statistics-test-suite.js +63 -0
- package/project/server/engine/test-suite.js +276 -0
- package/project/server/stack/api/base-64.js +16 -0
- package/project/server/stack/api/const-commands-decoder.js +15 -0
- package/project/server/stack/api/const-commands-encoder.js +15 -0
- package/project/server/stack/api/hmacSha1.js +14 -0
- package/project/server/stack/api/json-web-token.js +38 -0
- package/project/server/stack/api/sdp/sdp-decoder.js +70 -0
- package/project/server/stack/api/sdp/sdp-encoder.js +40 -0
- package/project/server/stack/api/sdp/sdp-media-attribute.js +13 -0
- package/project/server/stack/api/sdp/sdp-media-session.js +16 -0
- package/project/server/stack/api/sdp/sdp-message.js +17 -0
- package/project/server/stack/api/sdp/sdp-origin.js +17 -0
- package/project/server/stack/api/sdp/sdp-session-attribute.js +13 -0
- package/project/server/stack/api/sdp/sdp-timing.js +15 -0
- package/project/server/stack/api/text-protocol-header.js +35 -0
- package/project/server/stack/factory/stack-components-helpers.js +22 -0
- package/project/server/stack/factory/stack-components-template-base-actors.js +124 -0
- package/project/server/stack/factory/stack-components-template-base-test-cases.js +22 -0
- package/project/server/stack/factory/stack-components-templates-actors.js +15 -0
- package/project/server/stack/factory/stack-components-templates-test-cases.js +20 -0
- package/project/server/stack/managers/client-manager/client-manager.js +193 -0
- package/project/server/stack/managers/connection-manager/connection-data.js +82 -0
- package/project/server/stack/managers/connection-manager/connection-manager.js +177 -0
- package/project/server/stack/managers/connection-manager/connection-object.js +148 -0
- package/project/server/stack/managers/connection-manager/shared-data.js +14 -0
- package/project/server/stack/managers/manager.js +95 -0
- package/project/server/stack/managers/server-manager/server-manager.js +76 -0
- package/project/server/stack/managers/server-manager/server.js +384 -0
- package/project/server/stack/managers/shared-manager.js +117 -0
- package/project/server/stack/network/buffer-manager.js +190 -0
- package/project/server/stack/network/mc/mc-connection-client-shared.js +51 -0
- package/project/server/stack/network/mc/mc-connection-client.js +117 -0
- package/project/server/stack/network/mc/mc-connection-server-shared.js +51 -0
- package/project/server/stack/network/mc/mc-connection-server.js +58 -0
- package/project/server/stack/network/mc/mc-connection-shared.js +26 -0
- package/project/server/stack/network/mc/mc-connection.js +33 -0
- package/project/server/stack/network/mc/mc-server.js +90 -0
- package/project/server/stack/network/network-connection-shared.js +177 -0
- package/project/server/stack/network/network-connection.js +134 -0
- package/project/server/stack/network/network-helper.js +12 -0
- package/project/server/stack/network/network-server.js +36 -0
- package/project/server/stack/network/network-type.js +46 -0
- package/project/server/stack/network/tcp/tcp-connection-client-shared.js +18 -0
- package/project/server/stack/network/tcp/tcp-connection-client.js +71 -0
- package/project/server/stack/network/tcp/tcp-connection-server-shared.js +14 -0
- package/project/server/stack/network/tcp/tcp-connection-server.js +16 -0
- package/project/server/stack/network/tcp/tcp-connection-shared.js +55 -0
- package/project/server/stack/network/tcp/tcp-connection.js +167 -0
- package/project/server/stack/network/tcp/tcp-server.js +35 -0
- package/project/server/stack/network/tls/tls-connection-client-shared.js +18 -0
- package/project/server/stack/network/tls/tls-connection-client.js +76 -0
- package/project/server/stack/network/tls/tls-connection-server-shared.js +14 -0
- package/project/server/stack/network/tls/tls-connection-server.js +16 -0
- package/project/server/stack/network/tls/tls-connection-shared.js +63 -0
- package/project/server/stack/network/tls/tls-connection.js +176 -0
- package/project/server/stack/network/tls/tls-server.js +45 -0
- package/project/server/stack/network/transport.js +19 -0
- package/project/server/stack/network/udp/udp-connection-client-shared.js +47 -0
- package/project/server/stack/network/udp/udp-connection-client.js +93 -0
- package/project/server/stack/network/udp/udp-connection-server-shared.js +51 -0
- package/project/server/stack/network/udp/udp-connection-server.js +58 -0
- package/project/server/stack/network/udp/udp-connection-shared.js +26 -0
- package/project/server/stack/network/udp/udp-connection.js +33 -0
- package/project/server/stack/network/udp/udp-server.js +74 -0
- package/project/server/stack/pending/pending-const.js +22 -0
- package/project/server/stack/pending/pending-context-receive.js +18 -0
- package/project/server/stack/pending/pending-context-send.js +18 -0
- package/project/server/stack/pending/pending-context.js +18 -0
- package/project/server/stack/pending/pending-inner.js +107 -0
- package/project/server/stack/pending/pending-switch.js +146 -0
- package/project/server/stack/stacks/ascii-dictionary.js +289 -0
- package/project/server/stack/stacks/binary-log.js +88 -0
- package/project/server/stack/stacks/bit-byte.js +51 -0
- package/project/server/stack/stacks/browser-data.js +25 -0
- package/project/server/stack/stacks/cancel-object.js +16 -0
- package/project/server/stack/stacks/connection-client.js +228 -0
- package/project/server/stack/stacks/connection-instance.js +69 -0
- package/project/server/stack/stacks/connection-ip.js +374 -0
- package/project/server/stack/stacks/connection-server.js +169 -0
- package/project/server/stack/stacks/connection-web.js +113 -0
- package/project/server/stack/stacks/connection-worker-client.js +29 -0
- package/project/server/stack/stacks/connection-worker-connection.js +41 -0
- package/project/server/stack/stacks/connection-worker-server.js +29 -0
- package/project/server/stack/stacks/connection-worker.js +99 -0
- package/project/server/stack/stacks/connection.js +132 -0
- package/project/server/stack/stacks/decoder.js +83 -0
- package/project/server/stack/stacks/encoder.js +82 -0
- package/project/server/stack/stacks/ip-log.js +24 -0
- package/project/server/stack/stacks/message-selector.js +22 -0
- package/project/server/stack/stacks/msg-ip.js +18 -0
- package/project/server/stack/stacks/stack-content-cache.js +27 -0
- package/project/z-abs-funclayer-engine-server.tree +230 -0
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const NetworkType = require('../../network/network-type');
|
|
5
|
+
const NetworkConnectionShared = require('../../network/network-connection-shared');
|
|
6
|
+
const TcpServer = require('../../network/tcp/tcp-server');
|
|
7
|
+
const UdpServer = require('../../network/udp/udp-server');
|
|
8
|
+
const McServer = require('../../network/mc/mc-server');
|
|
9
|
+
const TlsServer = require('../../network/tls/tls-server');
|
|
10
|
+
const TcpConnectionServer = require('../../network/tcp/tcp-connection-server');
|
|
11
|
+
const TcpConnectionServerShared = require('../../network/tcp/tcp-connection-server-shared');
|
|
12
|
+
const UdpConnectionServer = require('../../network/udp/udp-connection-server');
|
|
13
|
+
const UdpConnectionServerShared = require('../../network/udp/udp-connection-server-shared');
|
|
14
|
+
const McConnectionServer = require('../../network/mc/mc-connection-server');
|
|
15
|
+
const McConnectionServerShared = require('../../network/mc/mc-connection-server-shared');
|
|
16
|
+
const TlsConnectionServer = require('../../network/tls/tls-connection-server');
|
|
17
|
+
const TlsConnectionShared = require('../../network/tls/tls-connection-shared');
|
|
18
|
+
const PendingConst = require('../../pending/pending-const');
|
|
19
|
+
const PendingContext = require('../../pending/pending-context');
|
|
20
|
+
const LogDataAction = require('z-abs-funclayer-engine-cs/clientServer/log/log-data-action');
|
|
21
|
+
const LogMsgServer = require('z-abs-funclayer-engine-cs/clientServer/log/msg/log-msg-server');
|
|
22
|
+
const LogDataStackType = require('z-abs-funclayer-engine-cs/clientServer/log/log-data-stack-type');
|
|
23
|
+
const LogObjectIp = require('z-abs-funclayer-engine-cs/clientServer/log/log-object-ip');
|
|
24
|
+
const FactoryNew = require('z-abs-corelayer-cs/clientServer/factory/factory-new');
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class Server {
|
|
28
|
+
static STATE_NONE = 0;
|
|
29
|
+
static STATE_STARTING = 1;
|
|
30
|
+
static STATE_STARTED = 2;
|
|
31
|
+
static STATE_STOPPING = 3;
|
|
32
|
+
static STATE_STOPPED = 4;
|
|
33
|
+
static STATES = ['STATE_NONE', 'STATE_STARTING', 'STATE_STARTED', 'STATE_STOPPING', 'STATE_STOPPED'];
|
|
34
|
+
|
|
35
|
+
constructor(id, actor, cbRemove) {
|
|
36
|
+
this.id = id;
|
|
37
|
+
this.actor = actor;
|
|
38
|
+
this.cbRemove = cbRemove;
|
|
39
|
+
this.connections = new Map();
|
|
40
|
+
this.pendingStartedConnections = [];
|
|
41
|
+
this.pendingAcceptedConnections = [];
|
|
42
|
+
this.acceptingConnectionCallbacks = [];
|
|
43
|
+
this.stoppingCallbacks = [];
|
|
44
|
+
this.srvAddress = null;
|
|
45
|
+
this.dstAddress = null;
|
|
46
|
+
this.networkServer = null;
|
|
47
|
+
this.state = Server.STATE_NONE;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
start(connection, options) {
|
|
51
|
+
this.srvAddress = connection.srvAddress;
|
|
52
|
+
this.dstAddress = connection.dstAddress;
|
|
53
|
+
connection.init(this);
|
|
54
|
+
const connectionDataLocal = connection.getConnectionDataLocal();
|
|
55
|
+
this._createServer(options, connection);
|
|
56
|
+
const pendingContext = new PendingContext(PendingConst.ACTOR);
|
|
57
|
+
connection.pendingStartContext(pendingContext, 'serverStart', () => {
|
|
58
|
+
// TODO: CANCEL PENDING server.start() NOT IMPLEMENTED
|
|
59
|
+
});
|
|
60
|
+
this.actor.logIp(() => this.actor.createLogMessage(`STARTING${connection.stackLog} local[${connectionDataLocal.getLog()}]`, null, new LogMsgServer(LogDataAction.STARTING, connection.name, connectionDataLocal), null), 'core', '99006ce3-5816-4a03-b813-220a9ad5a7d9');
|
|
61
|
+
this._setState(Server.STATE_STARTING);
|
|
62
|
+
connection.sharedManager.addServer(this.srvAddress, connection.messageSelector);
|
|
63
|
+
this.networkServer.init(this.srvAddress, this.dstAddress, options.transportLayer.transportProperties, (err) => {
|
|
64
|
+
if(err) {
|
|
65
|
+
this._setState(Server.STATE_NONE);
|
|
66
|
+
this.removeConnection(connection, () => {
|
|
67
|
+
connection.unlockAddress(this.srvAddress);
|
|
68
|
+
this.actor.logIp(() => this.actor.createLogMessage(`NOT STARTED${connection.stackLog} local[${connectionDataLocal.getLog()}]`, null, new LogMsgServer(LogDataAction.NOT_STARTED, connection.name, connectionDataLocal), null), 'core', '40b807ca-f7fc-4092-84ca-9450148645b4');
|
|
69
|
+
connection.pendingErrorContext(pendingContext, 'serverStart', err);
|
|
70
|
+
while(0 !== this.pendingStartedConnections.length) {
|
|
71
|
+
const pendingConnection = this.pendingStartedConnections.shift();
|
|
72
|
+
const pendingConnectionDataLocal = pendingConnection.connection.getConnectionDataLocal();
|
|
73
|
+
pendingConnection.connection.actor.logIp(() => pendingConnection.connection.actor.createLogMessage(`NOT ATTACHED${pendingConnection.connection.stackLog} local[${pendingConnectionDataLocal.getLog()}]`, undefined, new LogMsgServer(LogDataAction.NOT_ATTACHED, connection.name, pendingConnectionDataLocal), null), 'core', 'afc7033e-2570-4667-8b44-02e52bc986ef');
|
|
74
|
+
pendingConnection.connection.pendingErrorContext(pendingConnection.pendingContext, 'serverAttach', err);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
this._setState(Server.STATE_STARTED);
|
|
80
|
+
this.actor.logIp(() => this.actor.createLogMessage(`STARTED${connection.stackLog} local[${connectionDataLocal.getLog()}]`, null, new LogMsgServer(LogDataAction.STARTED, connection.name, connectionDataLocal), null), 'core', '7747f18e-61de-4a41-8cbc-c175bd1a26de');
|
|
81
|
+
connection.pendingDoneContext(pendingContext, 'serverStart', null);
|
|
82
|
+
while(0 !== this.pendingStartedConnections.length) {
|
|
83
|
+
const pendingConnection = this.pendingStartedConnections.shift();
|
|
84
|
+
const pendingConnectionDataLocal = pendingConnection.connection.getConnectionDataLocal();
|
|
85
|
+
pendingConnection.connection.actor.logIp(() => pendingConnection.connection.actor.createLogMessage(`ATTACHED${pendingConnection.connection.stackLog} local[${pendingConnectionDataLocal.getLog()}]`, null, new LogMsgServer(LogDataAction.ATTACHED, connection.name, pendingConnectionDataLocal), null), 'core', '768d00e9-600e-43dd-9848-831bda064870');
|
|
86
|
+
pendingConnection.connection.pendingDoneContext(pendingConnection.pendingContext, 'serverAttach', pendingConnection.connection);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
attachConnection(connection) {
|
|
93
|
+
connection.init(this);
|
|
94
|
+
let cancel = false;
|
|
95
|
+
const pendingContext = new PendingContext(PendingConst.ACTOR);
|
|
96
|
+
connection.pendingStartContext(pendingContext, 'serverAttach', () => {
|
|
97
|
+
cancel = true;
|
|
98
|
+
});
|
|
99
|
+
if(Server.STATE_STARTED === this.state) {
|
|
100
|
+
process.nextTick((cancel, connection, pendingContext) => {
|
|
101
|
+
if(!cancel) {
|
|
102
|
+
connection.pendingDoneContext(pendingContext, 'serverAttach', connection);
|
|
103
|
+
}
|
|
104
|
+
}, cancel, connection, pendingContext);
|
|
105
|
+
}
|
|
106
|
+
else if(Server.STATE_STARTING === this.state) {
|
|
107
|
+
this.pendingStartedConnections.push({
|
|
108
|
+
connection: connection,
|
|
109
|
+
pendingContext: pendingContext
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
addConnection(connection) {
|
|
115
|
+
this.connections.set(connection.id, connection);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
detachConnection(connection) {
|
|
119
|
+
this.connections.delete(connection.id);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
removeConnection(connection, done) {
|
|
123
|
+
this.connections.delete(connection.id);
|
|
124
|
+
connection.logIpStack(() => `DEL STACK: ${connection.name}`, LogDataStackType.DEL, '8622d6d0-1c42-4ebc-9c13-36adaf89ed48');
|
|
125
|
+
if(0 === this.connections.size) {
|
|
126
|
+
this._closeServer(connection, () => {
|
|
127
|
+
this.cbRemove();
|
|
128
|
+
done(null);
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
const connectionDataLocal = connection.getConnectionDataLocal();
|
|
133
|
+
connection.actor.logIp(() => connection.actor.createLogMessage(`DETACHED${connection.stackLog} local[${connectionDataLocal.getLog()}]`, null, new LogMsgServer(LogDataAction.DETACHED, connection.name, connectionDataLocal), null), 'core', 'aa1e6f3e-ae58-48a2-a993-2d2721faa643');
|
|
134
|
+
done(null);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
_findPendingAccepted(messageSelector) {
|
|
139
|
+
const foundIndex = this.pendingAcceptedConnections.findIndex((pendingAcceptedConnection) => {
|
|
140
|
+
return !(pendingAcceptedConnection instanceof NetworkConnectionShared) === !messageSelector;
|
|
141
|
+
});
|
|
142
|
+
if(-1 !== foundIndex) {
|
|
143
|
+
const connection = this.pendingAcceptedConnections[foundIndex];
|
|
144
|
+
this.pendingAcceptedConnections.splice(foundIndex, 1);
|
|
145
|
+
return connection
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
_findSharedAccepted(messageSelector) {
|
|
153
|
+
if(messageSelector) {
|
|
154
|
+
let foundConnection = null;
|
|
155
|
+
for(let [key, connection] of this.connections) {
|
|
156
|
+
if(connection.messageSelector && connection.networkConnection) {
|
|
157
|
+
foundConnection = connection;
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return foundConnection;
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
accept(connection, done) {
|
|
169
|
+
if(0 !== this.pendingAcceptedConnections.length) {
|
|
170
|
+
const foundNetworkConnection = this._findPendingAccepted(connection.messageSelector);
|
|
171
|
+
if(null !== foundNetworkConnection) {
|
|
172
|
+
if(foundNetworkConnection instanceof NetworkConnectionShared) {
|
|
173
|
+
foundNetworkConnection.ref();
|
|
174
|
+
}
|
|
175
|
+
done(foundNetworkConnection);
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
this.acceptingConnectionCallbacks.push({
|
|
179
|
+
connection: connection,
|
|
180
|
+
done: done
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
const foundConnection = this._findSharedAccepted(connection.messageSelector);
|
|
186
|
+
if(null !== foundConnection) {
|
|
187
|
+
connection.messageSelector._setId(foundConnection.networkConnection.ref());
|
|
188
|
+
done(foundConnection.networkConnection);
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
this.acceptingConnectionCallbacks.push({
|
|
192
|
+
connection: connection,
|
|
193
|
+
done: done
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
close(cbClose) {
|
|
200
|
+
let connections = this.connections.size;
|
|
201
|
+
if(0 === connections) {
|
|
202
|
+
let pendingAcceptedConnections = this.pendingAcceptedConnections.length;
|
|
203
|
+
if(0 === this.pendingAcceptedConnections.length) {
|
|
204
|
+
this._closeServer(null, () => {
|
|
205
|
+
cbClose();
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
this.pendingAcceptedConnections.forEach((connection) => {
|
|
210
|
+
if(0 === --pendingAcceptedConnections) {
|
|
211
|
+
this._closeServer(connection, () => {
|
|
212
|
+
cbClose();
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
this.connections.forEach((connection) => {
|
|
220
|
+
connection.closeForgottenConnections(() => {
|
|
221
|
+
if(0 === --connections) {
|
|
222
|
+
this._closeServer(connection, () => {
|
|
223
|
+
this.connections.clear();
|
|
224
|
+
cbClose();
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
_handleIncommingConnection(connection, networkConnection) {
|
|
233
|
+
if(0 !== this.acceptingConnectionCallbacks.length) {
|
|
234
|
+
const foundIndex = this.acceptingConnectionCallbacks.findIndex((acceptingConnectionCallback) => {
|
|
235
|
+
return !acceptingConnectionCallback.connection.messageSelector === !connection.messageSelector;
|
|
236
|
+
});
|
|
237
|
+
if(-1 !== foundIndex) {
|
|
238
|
+
const callbackData = this.acceptingConnectionCallbacks[foundIndex];
|
|
239
|
+
this.acceptingConnectionCallbacks.splice(foundIndex, 1);
|
|
240
|
+
if(callbackData.connection._shared()) {
|
|
241
|
+
const ref = networkConnection.ref();
|
|
242
|
+
if(callbackData.connection.messageSelector) {
|
|
243
|
+
callbackData.connection.messageSelector._setId(ref);
|
|
244
|
+
}
|
|
245
|
+
const callbackDatas = [];
|
|
246
|
+
for(let i = 0; i < this.acceptingConnectionCallbacks.length;) {
|
|
247
|
+
const acceptingConnectionCallback = this.acceptingConnectionCallbacks[i];
|
|
248
|
+
if(!!acceptingConnectionCallback.connection.messageSelector) {
|
|
249
|
+
callbackDatas.push(acceptingConnectionCallback);
|
|
250
|
+
acceptingConnectionCallback.connection.messageSelector._setId(networkConnection.ref());
|
|
251
|
+
this.acceptingConnectionCallbacks.splice(i, 1);
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
++i;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
callbackDatas.forEach((callbackData) => {
|
|
258
|
+
process.nextTick(callbackData.done, networkConnection);
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
callbackData.done(networkConnection);
|
|
262
|
+
}
|
|
263
|
+
else {
|
|
264
|
+
this.pendingAcceptedConnections.push(networkConnection);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
else {
|
|
268
|
+
this.pendingAcceptedConnections.push(networkConnection);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
_createServer(options, connection) {
|
|
273
|
+
switch(options.transportLayer.transportType) {
|
|
274
|
+
case NetworkType.UDP:
|
|
275
|
+
this.networkServer = FactoryNew.create(UdpServer, connection, (socket, remote) => {
|
|
276
|
+
let udpConnection = null;
|
|
277
|
+
const con = FactoryNew.create(UdpConnectionServer, connection.srvAddress, connection.dstAddress, this.networkServer, options.transportLayer.transportProperties, connection);
|
|
278
|
+
if(!connection._shared()) {
|
|
279
|
+
udpConnection = con;
|
|
280
|
+
}
|
|
281
|
+
else {
|
|
282
|
+
udpConnection = FactoryNew.create(UdpConnectionServerShared, con, 'udp server');
|
|
283
|
+
}
|
|
284
|
+
udpConnection.attach(socket);
|
|
285
|
+
udpConnection.setRemoteAddress(remote);
|
|
286
|
+
this._handleIncommingConnection(connection, udpConnection);
|
|
287
|
+
return udpConnection;
|
|
288
|
+
});
|
|
289
|
+
break;
|
|
290
|
+
case NetworkType.MC:
|
|
291
|
+
this.networkServer = FactoryNew.create(McServer, connection, (socket, remote) => {
|
|
292
|
+
let mcConnection = null;
|
|
293
|
+
const con = FactoryNew.create(McConnectionServer, connection.srvAddress, connection.dstAddress, this.networkServer, options.transportLayer.transportProperties, connection);
|
|
294
|
+
if(!connection._shared()) {
|
|
295
|
+
mcConnection = con;
|
|
296
|
+
}
|
|
297
|
+
else {
|
|
298
|
+
mcConnection = FactoryNew.create(McConnectionServerShared, con, 'mc server');
|
|
299
|
+
}
|
|
300
|
+
mcConnection.attach(socket);
|
|
301
|
+
mcConnection.setRemoteAddress(remote);
|
|
302
|
+
this._handleIncommingConnection(connection, mcConnection);
|
|
303
|
+
return mcConnection;
|
|
304
|
+
});
|
|
305
|
+
break;
|
|
306
|
+
case NetworkType.TLS:
|
|
307
|
+
this.networkServer = FactoryNew.create(TlsServer, connection, (socket) => {
|
|
308
|
+
let tlsConnection = null;
|
|
309
|
+
const con = FactoryNew.create(TlsConnectionServer, connection.srvAddress, connection.dstAddress, options.transportLayer.transportProperties, connection);
|
|
310
|
+
if(!connection._shared()) {
|
|
311
|
+
tlsConnection = con;
|
|
312
|
+
}
|
|
313
|
+
else {
|
|
314
|
+
tlsConnection = FactoryNew.create(TlsConnectionShared, con, 'tls server');
|
|
315
|
+
}
|
|
316
|
+
tlsConnection.attach(socket);
|
|
317
|
+
this._handleIncommingConnection(connection, tlsConnection);
|
|
318
|
+
});
|
|
319
|
+
break;
|
|
320
|
+
case NetworkType.TCP:
|
|
321
|
+
default:
|
|
322
|
+
this.networkServer = FactoryNew.create(TcpServer, connection, (socket) => {
|
|
323
|
+
let tcpConnection = null;
|
|
324
|
+
const con = FactoryNew.create(TcpConnectionServer, connection.srvAddress, connection.dstAddress, options.transportLayer.transportProperties, connection);
|
|
325
|
+
if(!connection._shared()) {
|
|
326
|
+
tcpConnection = con;
|
|
327
|
+
}
|
|
328
|
+
else {
|
|
329
|
+
tcpConnection = FactoryNew.create(TcpConnectionServerShared, con, 'tcp server');
|
|
330
|
+
}
|
|
331
|
+
tcpConnection.attach(socket);
|
|
332
|
+
this._handleIncommingConnection(connection, tcpConnection);
|
|
333
|
+
});
|
|
334
|
+
break;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
_closeNetworkServer(connection, done) {
|
|
339
|
+
this.networkServer.close(() => {
|
|
340
|
+
this._setState(Server.STATE_STOPPED);
|
|
341
|
+
const connectionDataLocal = connection.getConnectionDataLocal();
|
|
342
|
+
connection.actor.logIp(() => connection.actor.createLogMessage(`STOPPED${connection.stackLog} local[${connectionDataLocal.getLog()}]`, null, new LogMsgServer(LogDataAction.STOPPED, connection.name, connectionDataLocal), null), 'core', 'de9ce489-08bb-44b4-bf31-9aa41a178c7d');
|
|
343
|
+
this.networkServer = null;
|
|
344
|
+
done();
|
|
345
|
+
this.stoppingCallbacks.forEach((cbDone) => {
|
|
346
|
+
cbDone();
|
|
347
|
+
});
|
|
348
|
+
this.stoppingCallbacks = [];
|
|
349
|
+
});
|
|
350
|
+
/*this.pendingAcceptedConnections.forEach((connection) => {
|
|
351
|
+
connection.close((done) => {
|
|
352
|
+
});
|
|
353
|
+
});*/
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
_closeServer(connection, done) {
|
|
357
|
+
//ddb.info('STATE[' + this.actor.logName + ']:', Server.STATES[this.state]);
|
|
358
|
+
if(Server.STATE_STOPPING === this.state) {
|
|
359
|
+
this.stoppingCallbacks.push(done);
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
else if(Server.STATE_STARTED !== this.state) {
|
|
363
|
+
done();
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
const connectionDataLocal = connection.getConnectionDataLocal();
|
|
367
|
+
connection.actor.logIp(() => connection.actor.createLogMessage(`STOPPING${connection.stackLog} local[${connectionDataLocal.getLog()}]`, null, new LogMsgServer(LogDataAction.STOPPING, connection.name, connectionDataLocal), null), 'core', 'd5caafe5-e17f-4825-9620-4bffc2402c15');
|
|
368
|
+
this._setState(Server.STATE_STOPPING);
|
|
369
|
+
this.pendingAcceptedConnections.forEach((connection) => {
|
|
370
|
+
connection.close((done) => {
|
|
371
|
+
});
|
|
372
|
+
});
|
|
373
|
+
this._closeNetworkServer(connection, done);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
_setState(state) {
|
|
377
|
+
const currentState = this.state;
|
|
378
|
+
this.state = state;
|
|
379
|
+
//ddb.info('STATE[' + this.actor.logName + ']:', Server.STATES[currentState], '=>', Server.STATES[state]);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
module.exports = Server;
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class SharedManager {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.connections = new Map();
|
|
8
|
+
this.servers = [];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
hasServer(connectionType) {
|
|
12
|
+
let has = false;
|
|
13
|
+
this.servers.forEach((server) => {
|
|
14
|
+
if(server.messageSelector) {
|
|
15
|
+
has = has || (connectionType === server.messageSelector.type);
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
return has;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
getServers(connectionType, interceptor=false) {
|
|
22
|
+
const servers = [];
|
|
23
|
+
this.servers.forEach((server) => {
|
|
24
|
+
if(server.messageSelector) {
|
|
25
|
+
if(connectionType === server.messageSelector.type && (!interceptor || interceptor && server.messageSelector.interceptor)) {
|
|
26
|
+
if(server.messageSelector.onSelect) {
|
|
27
|
+
servers.push({
|
|
28
|
+
dstAddress: server.srvAddress,
|
|
29
|
+
members: JSON.stringify(server.messageSelector),
|
|
30
|
+
onSelectFunction: server.messageSelector.onSelect.toString()
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
return servers;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
reset() {
|
|
40
|
+
this.connections = new Map();
|
|
41
|
+
this.servers = [];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
addClient(actorName, local, remote, messageSelector) {
|
|
45
|
+
const key = this._makeKey('client', local.transportType, actorName, local, remote);
|
|
46
|
+
if(this.connections.has(key)) {
|
|
47
|
+
const data = this.connections.get(key);
|
|
48
|
+
data.clients.set(actorName, {
|
|
49
|
+
local: local,
|
|
50
|
+
remote: remote,
|
|
51
|
+
messageSelector: messageSelector
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
const clients = new Map();
|
|
56
|
+
clients.set(actorName, {
|
|
57
|
+
local: local,
|
|
58
|
+
remote: remote,
|
|
59
|
+
messageSelector: messageSelector
|
|
60
|
+
});
|
|
61
|
+
this.connections.set(key, {
|
|
62
|
+
clients: clients,
|
|
63
|
+
servers: new Map()
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
addServer(srvAddress, messageSelector) {
|
|
69
|
+
this.servers.push({
|
|
70
|
+
srvAddress: srvAddress,
|
|
71
|
+
messageSelector: messageSelector
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
updateServerConnection(actorName, local, remote, messageSelector) {
|
|
76
|
+
if(messageSelector) {
|
|
77
|
+
const key = this._makeKey('server', local.transportType, actorName, local, remote);
|
|
78
|
+
if(this.connections.has(key)) {
|
|
79
|
+
const data = this.connections.get(key);
|
|
80
|
+
data.servers.set(actorName, {
|
|
81
|
+
local: local,
|
|
82
|
+
remote: remote,
|
|
83
|
+
messageSelector: messageSelector
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
const servers = new Map();
|
|
88
|
+
servers.set(actorName, {
|
|
89
|
+
local: local,
|
|
90
|
+
remote: remote,
|
|
91
|
+
messageSelector: messageSelector
|
|
92
|
+
});
|
|
93
|
+
this.connections.set(key, {
|
|
94
|
+
clients: new Map(),
|
|
95
|
+
servers: servers
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
_makeKey(type, transportType, actorName, local, remote) {
|
|
102
|
+
if("client" === type) {
|
|
103
|
+
if('mc' === transportType) {
|
|
104
|
+
return `c_${actorName}_${local.host}:${local.port}_s_${remote.host}:${remote.port}`;
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
return `c_${local.host}:${local.port}_s_${remote.host}:${remote.port}`;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
else if("server" === type) {
|
|
111
|
+
return `c_${remote.host}:${remote.port}_s_${local.host}:${local.port}`;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
module.exports = SharedManager;
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class BufferManager {
|
|
6
|
+
static LF = '\n'.charCodeAt(0);
|
|
7
|
+
static CR = '\r'.charCodeAt(0);
|
|
8
|
+
static CRLF = '\r\n';
|
|
9
|
+
|
|
10
|
+
constructor(id) {
|
|
11
|
+
this.id = id;
|
|
12
|
+
this.buffers = [];
|
|
13
|
+
this.length = 0;
|
|
14
|
+
this.searchBufferIndex = 0;
|
|
15
|
+
this.bufferStartPos = 0;
|
|
16
|
+
this.bufferStopIndex = 0;
|
|
17
|
+
this.bufferStopPos = 0;
|
|
18
|
+
this.crEnding = false;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
_restore() {
|
|
22
|
+
this.buffers = [];
|
|
23
|
+
this.length = 0;
|
|
24
|
+
this.searchBufferIndex = 0;
|
|
25
|
+
this.bufferStartPos = 0;
|
|
26
|
+
this.bufferStopIndex = 0;
|
|
27
|
+
this.bufferStopPos = 0;
|
|
28
|
+
this.crEnding = false;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
getLine() {
|
|
32
|
+
let line = '';
|
|
33
|
+
if(0 === this.bufferStopIndex) {
|
|
34
|
+
const totalLength = this.bufferStopPos + 2;
|
|
35
|
+
line = this.buffers[0].toString('utf8', this.bufferStartPos, this.bufferStopPos);
|
|
36
|
+
if(this.buffers[0].length === totalLength) {
|
|
37
|
+
this.bufferStartPos = 0;
|
|
38
|
+
this.buffers.shift();
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
this.bufferStartPos = totalLength;
|
|
42
|
+
}
|
|
43
|
+
this.bufferStopPos = -1;
|
|
44
|
+
this.length -= (line.length + 2);
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
let buffersLength = 0;
|
|
48
|
+
for(let i = 0; i <= this.bufferStopIndex; ++i) {
|
|
49
|
+
buffersLength += this.buffers[i].length;
|
|
50
|
+
}
|
|
51
|
+
const contentLength = buffersLength - this.bufferStartPos - (this.buffers[this.bufferStopIndex].length - this.bufferStopPos);
|
|
52
|
+
line = Buffer.concat(this.buffers, buffersLength).toString('utf8', this.bufferStartPos, this.bufferStartPos + contentLength);
|
|
53
|
+
if(buffersLength === line.length + 2) {
|
|
54
|
+
this.bufferStartPos = 0;
|
|
55
|
+
this.buffers.splice(0, this.bufferStopIndex + 1);
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
this.bufferStartPos = this.bufferStopPos + 2;
|
|
59
|
+
this.buffers.splice(0, this.bufferStopIndex);
|
|
60
|
+
}
|
|
61
|
+
this.searchBufferIndex = 0;
|
|
62
|
+
this.bufferStopPos = -1;
|
|
63
|
+
this.length -= (contentLength + 2);
|
|
64
|
+
}
|
|
65
|
+
return line;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
findLine() {
|
|
69
|
+
if(0 === this.length) {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
// SEARCH FIRST BUFFER
|
|
73
|
+
const currentBuffer = this.buffers[this.searchBufferIndex];
|
|
74
|
+
if(0 === this.searchBufferIndex) {
|
|
75
|
+
const posEnd = currentBuffer.indexOf(BufferManager.CRLF, this.bufferStartPos);
|
|
76
|
+
if(-1 !== posEnd) {
|
|
77
|
+
this.bufferStopIndex = this.searchBufferIndex;
|
|
78
|
+
this.bufferStopPos = posEnd;
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
this.crEnding = BufferManager.CR === currentBuffer[currentBuffer.length - 1];
|
|
82
|
+
++this.searchBufferIndex;
|
|
83
|
+
}
|
|
84
|
+
for(; this.searchBufferIndex < this.buffers.length; ++this.searchBufferIndex) {
|
|
85
|
+
if(this.crEnding) {
|
|
86
|
+
if(BufferManager.LF === currentBuffer[0]) {
|
|
87
|
+
this.bufferStopIndex = this.searchBufferIndex;
|
|
88
|
+
this.bufferStopPos = -1;
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const posEnd = currentBuffer.indexOf(BufferManager.CRLF);
|
|
93
|
+
if(-1 !== posEnd) {
|
|
94
|
+
this.bufferStopIndex = this.searchBufferIndex;
|
|
95
|
+
this.bufferStopPos = posEnd;
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
this.crEnding = BufferManager.CR === currentBuffer[currentBuffer.length - 1];
|
|
99
|
+
}
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
receiveSize(size) {
|
|
104
|
+
if(0 === this.length || this.length < size) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
this.length -= size;
|
|
108
|
+
const firstBufferLength = this.buffers[0].length;
|
|
109
|
+
if(firstBufferLength - this.bufferStartPos === size) {
|
|
110
|
+
if(0 === this.bufferStartPos) {
|
|
111
|
+
const b = this.buffers.shift();
|
|
112
|
+
return b;
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
const pos = this.bufferStartPos;
|
|
116
|
+
this.bufferStartPos = 0;
|
|
117
|
+
const buffer = this.buffers.shift();
|
|
118
|
+
const b = buffer.slice(pos, pos + size);
|
|
119
|
+
return b;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
else if(firstBufferLength - this.bufferStartPos > size) {
|
|
123
|
+
const pos = this.bufferStartPos;
|
|
124
|
+
this.bufferStartPos = 0;
|
|
125
|
+
if(0 === pos) {
|
|
126
|
+
const b = this.buffers[0].slice(pos, pos + size);
|
|
127
|
+
this.buffers[0] = this.buffers[0].subarray(pos + size);
|
|
128
|
+
return b;
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
const resultBuffer = this.buffers[0].slice(pos, pos + size);
|
|
132
|
+
this.buffers[0] = this.buffers[0].subarray(pos + size, firstBufferLength);
|
|
133
|
+
return resultBuffer;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
let totalSize = firstBufferLength - this.bufferStartPos;
|
|
138
|
+
if(totalSize < 0) {
|
|
139
|
+
ddb.warning(totalSize, firstBufferLength, this.bufferStartPos);
|
|
140
|
+
}
|
|
141
|
+
let index = 0;
|
|
142
|
+
let previusTotalSize = 0;
|
|
143
|
+
while(totalSize < size) {
|
|
144
|
+
previusTotalSize = totalSize;
|
|
145
|
+
totalSize += this.buffers[++index].length;
|
|
146
|
+
}
|
|
147
|
+
if(0 !== this.bufferStartPos) {
|
|
148
|
+
this.buffers[0] = this.buffers[0].subarray(this.bufferStartPos, firstBufferLength);
|
|
149
|
+
}
|
|
150
|
+
const restSize = size - previusTotalSize;
|
|
151
|
+
if(this.buffers[index].length === restSize) {
|
|
152
|
+
this.bufferStartPos = 0;
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
this.buffers.splice(index, 0, this.buffers[index].slice(0, restSize));
|
|
156
|
+
this.bufferStartPos = restSize;
|
|
157
|
+
}
|
|
158
|
+
const buffer = Buffer.concat(this.buffers, size);
|
|
159
|
+
this.buffers.splice(0, index + 1);
|
|
160
|
+
return buffer;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
receive() {
|
|
165
|
+
if(0 === this.length) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
const buffers = this.buffers;
|
|
170
|
+
this._restore();
|
|
171
|
+
if(1 === buffers.length) {
|
|
172
|
+
return buffers[0];
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
return Buffer.concat(buffers);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
addBuffer(buffer) {
|
|
181
|
+
if(buffer.length === 0) {
|
|
182
|
+
throw new Error('');
|
|
183
|
+
}
|
|
184
|
+
this.buffers.push(buffer);
|
|
185
|
+
this.length += buffer.length;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
module.exports = BufferManager;
|