mirror of
https://github.com/smogon/pokemon-showdown.git
synced 2026-05-19 21:48:23 -05:00
If a child process dies in a way that it can't recover from, the main process now exits, to prevent loops of 'Error: channel closed' thousands of times a second, filling up the error log and making it hard to track down the real error.
38 lines
1.0 KiB
JavaScript
38 lines
1.0 KiB
JavaScript
'use strict';
|
|
|
|
let EventEmitter = require('events').EventEmitter;
|
|
|
|
function createWorker() {
|
|
let fakeWorker = new EventEmitter();
|
|
fakeWorker.send = function () {};
|
|
fakeWorker.process = {connected: true};
|
|
Sockets.workers[fakeWorker.id] = fakeWorker;
|
|
return fakeWorker;
|
|
}
|
|
|
|
function createConnection(ip, workerid, socketid) {
|
|
if (!workerid || !socketid) {
|
|
workerid = Object.keys(Sockets.workers)[0];
|
|
if (!workerid) workerid = createWorker().id;
|
|
socketid = 1;
|
|
while (Users.connections.has(workerid + '-' + socketid)) {
|
|
socketid++;
|
|
}
|
|
}
|
|
let connectionid = workerid + '-' + socketid;
|
|
let connection = new Users.Connection(connectionid, Sockets.workers[workerid], socketid, null, ip || '127.0.0.1');
|
|
Users.connections.set(connectionid, connection);
|
|
return connection;
|
|
}
|
|
|
|
function createUser(connection) {
|
|
if (!connection) connection = createConnection();
|
|
let user = new Users.User(connection);
|
|
connection.user = user;
|
|
user.joinRoom('global', connection);
|
|
return user;
|
|
}
|
|
|
|
exports.Connection = createConnection;
|
|
exports.User = createUser;
|