-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.js
568 lines (430 loc) · 15.2 KB
/
Server.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/**
* Copyright (c) 2012 Ivo Wetzel.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// Imports --------------------------------------------------------------------
var BISON = require('./lib/bison'),
ObjectList = require('./lib/ObjectList'),
WebSocketServer = require('./lib/WebSocket'),
Maple = require('./Maple');
// Main Server ----------------------------------------------------------------
// ----------------------------------------------------------------------------
Maple.Server = Maple.Class(function(clientClass, messageTypes) {
// Clients
this._socket = null;
this._clients = new ObjectList();
this._clientClass = clientClass || Maple.ServerClient;
this._messageTypes = messageTypes || {};
}, {
$version: '0.2',
/**
* {Booelan} Starts the server in case it's not already running.
*
* Returns `true` on success.
*/
start: function(options) {
if (this.isRunning()) {
return false;
}
// Traffic
this._oldBytesSend = 0;
this._bytesSend = 0;
this._bytesTime = Date.now();
this._bytesStats = [];
this._bytesStatId = 0;
this._bytesSendPerSecond = 0;
// Ticking
this._tickTime = 0;
this._tickRate = Math.floor(1000 / (options.tickRate || 30));
this._tickCount = 0;
this._tickInterval = null;
// Game Time
this._startTime = -1;
this._realTime = 0;
this._frameTime = -1;
// More logic stuff
this._randomSeed = 500000 + Math.floor((Math.random() * 1000000));
this._randomState = 0;
this._logicRate = options.logicRate || 1; // Used for throttling logic ticks
this._syncRate = options.syncRate || 30; // Send down tick count to clients every X updates
this._isRunning = false;
var that = this;
this._tickInterval = setInterval(function() {
that._update();
}, this._tickRate);
this._frameTime = Date.now();
this._tickCount = 1;
this._isRunning = true;
if (options.socket !== false) {
this._createSocket();
this._socket.listen(options.port);
}
return true;
},
/**
* {Boolean} Returns whether the server is running or not.
*/
isRunning: function() {
return this._isRunning;
},
/**
* Broadcase a message.
*/
broadcast: function(type, data, clients, excluded) {
// Add type and tick to the message, unless we're just sending a
// single Number for tick syncing
if (type !== null) {
var msg = [this.messageTypeToId(type), this.getTick()];
if (data !== undefined) {
msg.push.apply(msg, data);
}
data = msg;
}
data = BISON.encode(data);
// Fall back to everyone!
if (!clients) {
clients = this._clients;
}
this._clients.each(function(client) {
if (!excluded || excluded.indexOf(client) === -1) {
this._bytesSend += client.sendRaw(data);
}
}, this);
},
stop: function() {
if (!this.isRunning()) {
return false;
}
this._isRunning = false;
clearInterval(this._tickInterval);
this.broadcast(Maple.Message.STOP);
this._clients.each(function(client) {
client.close();
});
if (this._socket) {
this._socket.close();
}
return true;
},
_createSocket: function() {
// Socket
this._socket = new WebSocketServer();
// Setup socket callbacks
var that = this;
this._socket.on('data', function(conn, raw, binary) {
that._data(conn, raw);
});
this._socket.on('end', function(conn) {
var client = that._clients.get(conn.clientId);
if (client) {
that._clients.remove(client);
that.disconnected(client);
}
});
this._socket.on('request', function(req, res) {
that.requested(req, res);
});
},
// Handling of incoming data and game logic -------------------------------
_data: function(conn, raw) {
// Do some basic filtering to prevent easy ways
// of breaking the server
var msg;
try {
msg = BISON.decode(raw);
} catch(e) {
this._error(conn, Maple.Error.INVALID_DATA);
return;
}
if (msg.length < 2 || !(msg instanceof Array)) {
this._error(conn, Maple.Error.MESSAGE_TOO_SHORT);
return;
}
// Get message details
var type = msg[0],
tick = msg[1],
data = msg.slice(2),
client = this._clients.get(conn.clientId);
// More checks for new connections
if (type === Maple.Message.CONNECT) {
if (!client) {
// Throw up on unsupported version
if (typeof data[0] !== 'string' || data[0] !== Maple.Server.$version) {
this._error(conn, Maple.Error.UNSUPPORTED_VERSION);
// Add client to list and give the id to the connection
} else {
client = new this._clientClass(this, conn, !!data[1]);
conn.clientId = this._clients.add(client);
this._bytesSend += client.send(Maple.Message.START, [
this._tickRate,
this._logicRate,
this._syncRate,
this._randomSeed,
this._messageTypes
]);
this.connected(client);
}
} else {
this._error(conn, Maple.Error.ALREADY_CONNECTED);
this._clients.remove(client);
}
} else if (client) {
// Handle time sync and ping detection
// we simply echo back here
if (type === Maple.Message.PING) {
client._ping = Math.ceil(data[1]);
this._bytesSend += client.send(Maple.Message.PONG, [data[0]]);
} else {
type = this.messageTypeFromId(type);
if (this.message(client, type, tick, data) !== true) {
client.message(type, tick, data);
}
}
}
},
_error: function(conn, type) {
this._bytesSend += conn.send(BISON.encode([Maple.Message.ERROR, 0, type]));
conn.close();
},
_update: function() {
var now = Date.now();
this._realTime += (now - this._frameTime);
while(this._tickTime < this._realTime && this.isRunning()) {
if (this._startTime === -1) {
this._startTime = Date.now();
this.started();
}
// Sync clients tick count
if (this._tickCount % this._syncRate === 0) {
this.broadcast(null, this._tickCount % 250, this._players);
}
if (this._tickCount % this._logicRate === 0) {
this.update(this._tickTime, this._tickCount);
}
this._tickCount++;
this._randomState = this._tickCount;
this._tickTime += this._tickRate;
}
if (now > this._bytesTime + 100) {
this._bytesStats[this._bytesStatId] = this._bytesSend - this._oldBytesSend;
this._oldBytesSend = this._bytesSend;
this._bytesStatId++;
this._bytesStatId = this._bytesStatId % 10;
this._bytesSendPerSecond = this._bytesStats.reduce(function(p, c) {
return p + c;
}) / 10;
this._bytesTime = now;
}
this._frameTime = now;
},
// Abstract methods -------------------------------------------------------
/**
* Callback for when the "game" is started.
*/
started: function() {
},
/**
* The game update callback.
*
* - @t {Integer} is the current game time.
* - @tick {Integer} is the current tick count.
*/
update: function(time, tick) {
},
/**
* Callback for when the "game" is stopped.
*/
stopped: function() {
},
/**
* Callback for when a client connects to the server.
*
* @client {Server.Client}
*/
connected: function(client) {
},
/**
* Callback for when a client disconnects from the server.
*
* @client {Server.Client}
*/
disconnected: function(client) {
},
/**
* {Boolean} Callback for any messages received from a @client.
*
* - @client {Server.Client} The client
* - @type {Integer} Message type
* - @tick {Integer} Client side tick at which the message was send.
* - @data {Array} Message data
*
* Return `true` to indicate that the message was handled and prevent it
* from being forwarded to {Maple.Client#message}.
*/
message: function(client, type, tick, data) {
},
/**
* Handler for HTTP Request which reach the server on the port its bound to.
*
* You can use this like the vanilla HTTP interface from Node.
*/
requested: function(req, res) {
},
// Getter -----------------------------------------------------------------
/**
* {Integer} Returns the "synced" game time in milliseconds.
*
* Please note that this is not 100% accurate and should only be used for
* client side logic like drawing and effects.
*
* For synced actions between server and clinet always use the `tick` count.
*/
getTime: function() {
return this._tickTime;
},
/**
* {Integer} Returns the number of synced game ticks that happend since the
* server was started.
*/
getTick: function() {
return this._tickCount;
},
/**
* {ObjectList} Returns the list of currently connected clients.
*/
getClients: function() {
return this._clients;
},
/**
* {Float} Returns a synced* random number between `0` and `1`.
*
* Note: This is only synced if the game code actual is the same on both sides.
*
* E.g: Doing 2 calls to this method on the client, but doing 3 on the server
* will have the potential to unsync the RNG until the next tick.
*/
getRandom: function() {
this._randomState = (1103515245 * (this._randomState + this._randomSeed) + 12345) % 0x100000000;
return this._randomState / 0x100000000;
},
// Conversions ------------------------------------------------------------
/**
* {Integer} Converts the message @type {String|Integer} into a integer ID representing for network transmission.
*/
messageTypeToId: function(type) {
if (typeof type === 'string') {
var id = this._messageTypes.indexOf(type);
if (id === -1) {
this.logError('Undefined type id for "' + type + '"');
}
return id;
} else if (typeof type === 'number') {
return type;
} else {
this.logError('Invalid type value: "' + type + '"');
}
},
/**
* {String} Converts the message @id {Integer} into the corresponding message type string.
*/
messageTypeFromId: function(id) {
if (id >= 0 && id < this._messageTypes.length) {
return this._messageTypes[id];
} else {
this.logError('Undefined type string for "' + id + '"');
}
},
// Helpers ----------------------------------------------------------------
/**
* {var[]} A wrapper for `console.log()` which also gives information about the current state of the server.
*/
log: function() {
console.log.apply(console, this._log(arguments));
},
/**
* {var[]} A wrapper for `console.error()` which also gives information about the current state of the server.
*/
logError: function() {
console.error.apply(console, this._log(arguments));
},
/**
* {var[]} A wrapper for `console.warn()` which also gives information about the current state of the server.
*/
logWarning: function() {
console.warn.apply(console, this._log(arguments));
},
_log: function(args) {
var parts = Array.prototype.slice.call(args),
info = '[Cc: ' + this.getClients().length
+ ' T: ' + this.getTime()
+ ' I: ' + this.getTick()
+ ' R: ' + this.getRandom() + ']\n';
parts.unshift(info);
parts.push('\n');
return parts;
}
});
/**
* {Maple.Server.Client} Simple client abstraction for the Maple Server.
*
* @conn {WebSocketConnection}
*/
Maple.ServerClient = Maple.Class(function(server, conn, isBinary) {
this.id = conn.id;
this.clientId = -1;
this._conn = conn;
this._ping = 0;
this.server = server;
this.isBinary = isBinary || false;
this._messageArray = [0, 0];
}, {
/**
* Handler for messages received by the client which were not handled
* by the server
*/
message: function(type, tick, data) {
},
/**
* Sends a @msg {Array} to the server, @type {Number}.
*/
send: function(type, data) {
// Add type and tick to the message
this._messageArray.length = 2;
this._messageArray[0] = this.server.messageTypeToId(type);
this._messageArray[1] = this.server.getTick();
if (data !== undefined) {
this._messageArray.push.apply(this._messageArray, data);
}
// Make the message as small as possible and send it
return this._conn.send(BISON.encode(this._messageArray), this.isBinary);
},
/**
* {Integer} Sends down the raw @data {String} to the client
*/
sendRaw: function(data) {
return this._conn.send(data, this.isBinary);
},
/**
* {Integer} Returns the ping to the server.
*/
getPing: function() {
return this._ping;
}
});