This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
WebsocketNetworkServer.js
362 lines (362 loc) · 17 KB
/
WebsocketNetworkServer.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
"use strict";
var ws = require('ws');
var inet = require('./INetwork');
var WebsocketNetworkServer = (function () {
function WebsocketNetworkServer() {
this.mPool = {};
}
WebsocketNetworkServer.log = function (msg) {
console.log("(" + new Date().toISOString() + ")" + msg);
};
WebsocketNetworkServer.prototype.onConnection = function (socket, appname) {
//it would be possible to enforce the client to send a certain introduction first
//to determine to which pool we add it -> for now only one pool is supported
this.mPool[appname].add(socket);
};
//
WebsocketNetworkServer.prototype.addSocketServer = function (websocketServer, appConfig) {
var _this = this;
if (this.mPool[appConfig.name] == null) {
this.mPool[appConfig.name] = new PeerPool(appConfig);
}
var name = appConfig.name;
websocketServer.on('connection', function (socket) { _this.onConnection(socket, name); });
};
return WebsocketNetworkServer;
}());
exports.WebsocketNetworkServer = WebsocketNetworkServer;
;
//Pool of client connects that are allowed to communicate to each other
var PeerPool = (function () {
function PeerPool(config) {
this.mConnections = new Array();
this.mServers = {};
this.mAddressSharing = false;
this.maxAddressLength = 256;
this.mAppConfig = config;
if (this.mAppConfig.address_sharing) {
this.mAddressSharing = this.mAppConfig.address_sharing;
}
}
PeerPool.prototype.hasAddressSharing = function () {
return this.mAddressSharing;
};
//add a new connection based on this websocket
PeerPool.prototype.add = function (socket) {
this.mConnections.push(new SignalingPeer(this, socket));
};
//Returns the SignalingClientConnection that opened a server using the given address
//or null if address not in use
PeerPool.prototype.getServerConnection = function (address) {
return this.mServers[address];
};
//Tests if the address is available for use.
//returns true in the following cases
//the address is longer than the maxAddressLength and the server the address is not yet in use or address sharing is active
PeerPool.prototype.isAddressAvailable = function (address) {
if (address.length <= this.maxAddressLength // only allow addresses shorter than maxAddressLength
&& (this.mServers[address] == null || this.mAddressSharing)) {
return true;
}
return false;
};
//Adds the server. No checking is performed here! logic should be solely in the connection class
PeerPool.prototype.addServer = function (client, address) {
if (this.mServers[address] == null) {
this.mServers[address] = new Array();
}
this.mServers[address].push(client);
};
//Removes an address from the server. No checks performed
PeerPool.prototype.removeServer = function (client, address) {
//supports address sharing. remove the client from the server list that share the address
var index = this.mServers[address].indexOf(client);
if (index != -1) {
this.mServers[address].splice(index, 1);
}
//delete the whole list if the last one left
if (this.mServers[address].length == 0) {
delete this.mServers[address];
}
};
//Removes a given connection from the pool
PeerPool.prototype.removeConnection = function (client) {
var index = this.mConnections.indexOf(client);
if (index != -1) {
this.mConnections.splice(index, 1);
}
else {
console.warn("Tried to remove unknown SignalingClientConnection. Bug?" + client);
}
};
PeerPool.prototype.count = function () {
return this.mConnections.length;
};
return PeerPool;
}());
var SignalingConnectionState;
(function (SignalingConnectionState) {
SignalingConnectionState[SignalingConnectionState["Uninitialized"] = 0] = "Uninitialized";
SignalingConnectionState[SignalingConnectionState["Connecting"] = 1] = "Connecting";
SignalingConnectionState[SignalingConnectionState["Connected"] = 2] = "Connected";
SignalingConnectionState[SignalingConnectionState["Disconnecting"] = 3] = "Disconnecting";
SignalingConnectionState[SignalingConnectionState["Disconnected"] = 4] = "Disconnected"; //means the instance is destroyed and unusable
})(SignalingConnectionState || (SignalingConnectionState = {}));
;
///note: all methods starting with "internal" might leave the system in an inconsistent state
///e.g. peerA is connected to peerB means peerB is connected to peerA but internalRemoveConnection
///could cause peerA being disconnected from peerB but peerB still thinking to be connected to peerA!!!
var SignalingPeer = (function () {
function SignalingPeer(pool, socket) {
var _this = this;
this.mState = SignalingConnectionState.Uninitialized;
this.mConnections = {};
//C# version uses short so 16384 is 50% of the positive numbers (maybe might make sense to change to ushort or int)
this.mNextIncomingConnectionId = new inet.ConnectionId(16384);
this.mConInfo = "[con info missing]";
this.mConnectionPool = pool;
this.mSocket = socket;
//(this.mSocket as any).maxPayload = 16;
this.mState = SignalingConnectionState.Connecting;
this.mConInfo = this.mSocket.upgradeReq.connection.remoteAddress + ":" + this.mSocket.upgradeReq.connection.remotePort;
WebsocketNetworkServer.log("[" + this.mConInfo + "]" + " connected ");
socket.on('message', function (message, flags) {
_this.onMessage(message, flags);
});
socket.on('error', function (error) {
console.error(error);
});
socket.on('close', function (code, message) { _this.onClose(code, message); });
socket.on('pong', function (data, flags) {
WebsocketNetworkServer.log("[" + _this.mConInfo + "]" + "INC: pong ");
});
this.mState = SignalingConnectionState.Connected;
this.mPingInterval = setInterval(function () { _this.doPing(); }, 30000);
}
SignalingPeer.prototype.doPing = function () {
if (this.mState == SignalingConnectionState.Connected && this.mSocket.readyState == ws.OPEN) {
this.mSocket.ping();
WebsocketNetworkServer.log("[" + this.mConInfo + "]" + "OUT: ping");
}
};
SignalingPeer.prototype.evtToString = function (evt) {
var output = "[";
output += "NetEventType: (";
output += inet.NetEventType[evt.Type];
output += "), id: (";
output += evt.ConnectionId.id;
if (evt.Info != null) {
output += "), Data: (";
output += evt.Info;
}
else if (evt.MessageData != null) {
var chars = new Uint16Array(evt.MessageData.buffer, evt.MessageData.byteOffset, evt.MessageData.byteLength / 2);
output += "), Data: (";
var binaryString = "";
for (var i = 0; i < chars.length; i++) {
binaryString += String.fromCharCode(chars[i]);
}
output += binaryString;
}
output += ")]";
return output;
};
SignalingPeer.prototype.onMessage = function (message, flags) {
try {
//unlike browsers ws will give a Uint8Array
var evt = inet.NetworkEvent.fromByteArray(message);
WebsocketNetworkServer.log("[" + this.mConInfo + "]" + "INC: " + this.evtToString(evt));
this.handleIncomingEvent(evt);
}
catch (err) {
WebsocketNetworkServer.log("[" + this.mConInfo + "]" + "Invalid message received: " + message + " \n Error: " + err);
}
};
SignalingPeer.prototype.sendToClient = function (evt) {
//this method is also called during cleanup after a disconnect
//check first if we are still connected
//bugfix: apprently 2 sockets can be closed at exactly the same time without
//onclosed being called immediately -> socket has to be checked if open
if (this.mState == SignalingConnectionState.Connected
&& this.mSocket.readyState == this.mSocket.OPEN) {
WebsocketNetworkServer.log("[" + this.mConInfo + "]" + "OUT: " + this.evtToString(evt));
var msg = inet.NetworkEvent.toByteArray(evt);
this.mSocket.send(msg);
}
};
SignalingPeer.prototype.onClose = function (code, error) {
this.mState = SignalingConnectionState.Disconnecting;
this.Cleanup();
};
//so far only used if the socket was disconnected
SignalingPeer.prototype.Cleanup = function () {
if (this.mPingInterval != null) {
clearInterval(this.mPingInterval);
}
this.mConnectionPool.removeConnection(this);
WebsocketNetworkServer.log("[" + this.mConInfo + "]" + "disconnected "
+ " " + this.mConnectionPool.count()
+ " connections left.");
//disconnect all connections
var test = this.mConnections; //workaround for not having a proper dictionary yet...
for (var v in this.mConnections) {
if (this.mConnections.hasOwnProperty(v))
this.disconnect(new inet.ConnectionId(+v));
}
//make sure the server address is freed
if (this.mServerAddress != null) {
this.stopServer();
}
this.mState = SignalingConnectionState.Disconnected;
};
SignalingPeer.prototype.handleIncomingEvent = function (evt) {
//update internal state based on the event
if (evt.Type == inet.NetEventType.NewConnection) {
//client wants to connect to another client
var address = evt.Info;
//the id this connection should be addressed with
var newConnectionId = evt.ConnectionId;
this.connect(address, newConnectionId);
}
else if (evt.Type == inet.NetEventType.ConnectionFailed) {
}
else if (evt.Type == inet.NetEventType.Disconnected) {
//peer tries to disconnect from another peer
var otherPeerId = evt.ConnectionId;
this.disconnect(otherPeerId);
}
else if (evt.Type == inet.NetEventType.ServerInitialized) {
this.startServer(evt.Info);
}
else if (evt.Type == inet.NetEventType.ServerInitFailed) {
}
else if (evt.Type == inet.NetEventType.ServerClosed) {
//stop server request
this.stopServer();
}
else if (evt.Type == inet.NetEventType.ReliableMessageReceived) {
this.sendData(evt.ConnectionId, evt.MessageData, true);
}
else if (evt.Type == inet.NetEventType.UnreliableMessageReceived) {
this.sendData(evt.ConnectionId, evt.MessageData, false);
}
};
SignalingPeer.prototype.internalAddIncomingPeer = function (peer) {
//another peer connected to this (while allowing incoming connections)
//store the reference
var id = this.nextConnectionId();
this.mConnections[id.id] = peer;
//event to this (the other peer gets the event via addOutgoing
this.sendToClient(new inet.NetworkEvent(inet.NetEventType.NewConnection, id, null));
};
SignalingPeer.prototype.internalAddOutgoingPeer = function (peer, id) {
//this peer successfully connected to another peer. id was generated on the
//client side
this.mConnections[id.id] = peer;
//event to this (the other peer gets the event via addOutgoing
this.sendToClient(new inet.NetworkEvent(inet.NetEventType.NewConnection, id, null));
};
SignalingPeer.prototype.internalRemovePeer = function (id) {
delete this.mConnections[id.id];
this.sendToClient(new inet.NetworkEvent(inet.NetEventType.Disconnected, id, null));
};
//test this. might cause problems
//the number is converted to string trough java script but we need get back the number
//for creating the connection id
SignalingPeer.prototype.findPeerConnectionId = function (otherPeer) {
for (var peer in this.mConnections) {
if (this.mConnections[peer] === otherPeer) {
return new inet.ConnectionId(+peer);
}
}
};
SignalingPeer.prototype.nextConnectionId = function () {
var result = this.mNextIncomingConnectionId;
this.mNextIncomingConnectionId = new inet.ConnectionId(this.mNextIncomingConnectionId.id + 1);
return result;
};
//public methods (not really needed but can be used for testing or server side deubgging)
//this peer initializes a connection to a certain address. The connection id is set by the client
//to allow tracking of the connection attempt
SignalingPeer.prototype.connect = function (address, newConnectionId) {
var serverConnections = this.mConnectionPool.getServerConnection(address);
//
if (serverConnections != null && serverConnections.length == 1) {
//inform the server connection about the new peer
//events will be send by these methods
//shared addresses -> connect to everyone listening
serverConnections[0].internalAddIncomingPeer(this);
this.internalAddOutgoingPeer(serverConnections[0], newConnectionId);
}
else {
//if address is not in use or it is in multi join mode -> connection fails
this.sendToClient(new inet.NetworkEvent(inet.NetEventType.ConnectionFailed, newConnectionId, null));
}
};
//join connection happens if another user joins a multi address. it will connect to every address
//listening to that room
SignalingPeer.prototype.connectJoin = function (address) {
var serverConnections = this.mConnectionPool.getServerConnection(address);
//in join mode every connection is incoming as everyone listens together
if (serverConnections != null) {
for (var _i = 0, serverConnections_1 = serverConnections; _i < serverConnections_1.length; _i++) {
var v = serverConnections_1[_i];
if (v != this) {
v.internalAddIncomingPeer(this);
this.internalAddIncomingPeer(v);
}
}
}
};
SignalingPeer.prototype.disconnect = function (connectionId) {
var otherPeer = this.mConnections[connectionId.id];
if (otherPeer != null) {
var idOfOther = otherPeer.findPeerConnectionId(this);
//find the connection id the other peer uses to talk to this one
this.internalRemovePeer(connectionId);
otherPeer.internalRemovePeer(idOfOther);
}
else {
}
};
SignalingPeer.prototype.startServer = function (address) {
//what to do if it is already a server?
if (this.mServerAddress != null)
this.stopServer();
if (this.mConnectionPool.isAddressAvailable(address)) {
this.mServerAddress = address;
this.mConnectionPool.addServer(this, address);
this.sendToClient(new inet.NetworkEvent(inet.NetEventType.ServerInitialized, inet.ConnectionId.INVALID, address));
if (this.mConnectionPool.hasAddressSharing()) {
//address sharing is active. connect to every endpoint already listening on this address
this.connectJoin(address);
}
}
else {
this.sendToClient(new inet.NetworkEvent(inet.NetEventType.ServerInitFailed, inet.ConnectionId.INVALID, address));
}
};
SignalingPeer.prototype.stopServer = function () {
if (this.mServerAddress != null) {
this.mConnectionPool.removeServer(this, this.mServerAddress);
this.sendToClient(new inet.NetworkEvent(inet.NetEventType.ServerClosed, inet.ConnectionId.INVALID, null));
this.mServerAddress = null;
}
//do nothing if it wasnt a server
};
//delivers the message to the local peer
SignalingPeer.prototype.forwardMessage = function (senderPeer, msg, reliable) {
var id = this.findPeerConnectionId(senderPeer);
if (reliable)
this.sendToClient(new inet.NetworkEvent(inet.NetEventType.ReliableMessageReceived, id, msg));
else
this.sendToClient(new inet.NetworkEvent(inet.NetEventType.UnreliableMessageReceived, id, msg));
};
SignalingPeer.prototype.sendData = function (id, msg, reliable) {
var peer = this.mConnections[id.id];
if (peer != null)
peer.forwardMessage(this, msg, reliable);
};
return SignalingPeer;
}());
//# sourceMappingURL=WebsocketNetworkServer.js.map