Pendigard's picture
Upload folder using huggingface_hub (part 23)
bd98362 verified
Raw History Blame Contribute Delete
2.86 kB
'use strict';
function flush() {
this.commands.forEach(function(command) {
this.client.write(Packet.encode(0x02, Protocol.REQ_ID+1, command));
}, this);
this.commands = [];
}
function dequeue(response) {
var callback = this.responseCallbacks.shift();
if (typeof callback === 'function') {
callback(response);
}
}
function connect() {
this.client = net.connect({
host: this.host,
port: this.port
}, function() {
this.emit('connected');
this.client.write(Packet.encode(
Protocol.SERVERDATA_AUTH,
Protocol.AUTH_ID,
this.password
));
}.bind(this));
this.client.setTimeout(this.timeout);
}
function listen() {
this.client.on('data', function(data) {
var res = Packet.decode(data);
switch(res.type) {
case Protocol.SERVERDATA_RESPONSE_VALUE:
if (this.authenticated) {
dequeue.call(this, res);
}
break;
case Protocol.SERVERDATA_AUTH_RESPONSE:
if (res.body !== '') {
return this.emit('error', 'Error: Invalid RCON password');
}
this.authenticated = true;
this.emit('authenticated');
if (this.execOnAuthenticated) flush.call(this);
break;
default:
this.emit('error', 'Error: Unknown server response');
break;
}
}.bind(this));
this.client.on('timeout', this.client.end.bind(this.client));
this.client.on('end', this.emit.bind(this, 'disconnected'));
this.client.on('error', function(e) {
this.emit('error', e);
}.bind(this));
}
var net = require('net'),
EventEmitter = require('events').EventEmitter,
inherits = require('util').inherits,
Packet = require('./packet'),
Protocol = require('./protocol');
var Rcon = function(options) {
EventEmitter.call(this);
options = options || {};
this.host = options.host || '127.0.0.1';
this.port = options.port || '27015';
this.password = options.password;
this.timeout = options.timeout || 5000;
//
// need a more succinct name for this option
this.execOnAuthenticated = options.execOnAuthenticated || true;
this.authenticated = false;
this.responseCallbacks = [];
this.commands = [];
if (options.connect) {
connect.call(this);
listen.call(this);
}
};
inherits(Rcon, EventEmitter);
Rcon.prototype.exec = function(command, callback) {
if (this.authenticated) {
this.client.write(Packet.encode(0x02, Protocol.REQ_ID, command));
} else {
this.commands.push(command);
}
this.responseCallbacks.push(callback);
return this;
};
Rcon.prototype.connect = function() {
this.emit('connecting');
if (!this.client) {
connect.call(this);
listen.call(this);
}
return this;
};
Rcon.prototype.close = function() {
this.emit('disconnecting');
if (this.client) this.client.end();
return this;
};
module.exports = Rcon;