From 2d4f4a05e17ee5bc31123cf264b32932ea1f8915 Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Sun, 9 Jul 2017 13:21:54 +0200 Subject: [PATCH 1/3] trying to fix autoversion in online mode --- src/client/autoVersion.js | 1 + src/createClient.js | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/client/autoVersion.js b/src/client/autoVersion.js index 061e52b..23293cb 100644 --- a/src/client/autoVersion.js +++ b/src/client/autoVersion.js @@ -53,6 +53,7 @@ module.exports = function(client, options) { // Finished configuring client object, let connection proceed client.emit('connect_allowed'); + client.wait_connect = false; }); return client; }; diff --git a/src/createClient.js b/src/createClient.js index faaf635..934afc3 100644 --- a/src/createClient.js +++ b/src/createClient.js @@ -19,6 +19,8 @@ module.exports=createClient; function createClient(options) { assert.ok(options, "options is required"); assert.ok(options.username, "username is required"); + if(!options.version) + options.version=false; // TODO: avoid setting default version if autoVersion is enabled const optVersion = options.version || require("./version").defaultVersion; From eb5764e7abc22886f5a4dcfec00948c423773e3f Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Wed, 12 Jul 2017 13:07:25 +0200 Subject: [PATCH 2/3] fix autoversion in online mode. Explanation: in this specific case, we were changing the state of the client to hanshaking (in autoVersion.js) before a connection was made (using setSocket), the consequence is the serializer was already piped to the framer when setSocket was called, and setSocket doesn't unpipe before piping. The consequence of that was the framer was piped 2 times, the packet was sent 2 times and the server kicked us. My solution is piping the serializer to the framer in Client constructor. Then if the state is changed, it will anyway automatically unpipe before repiping. Other places correctly unpipe before piping. Autoversion is now the default in nmp too --- examples/client_auto/client_auto.js | 3 ++- src/client.js | 32 ++++++++++++++--------------- src/ping.js | 2 +- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/examples/client_auto/client_auto.js b/examples/client_auto/client_auto.js index e89e803..1733608 100644 --- a/examples/client_auto/client_auto.js +++ b/examples/client_auto/client_auto.js @@ -7,7 +7,8 @@ if(process.argv.length < 4 || process.argv.length > 6) { process.exit(1); } -var client = mc.createClient({version: false, +var client = mc.createClient({ + version: false, host: process.argv[2], port: parseInt(process.argv[3]), username: process.argv[4] ? process.argv[4] : "echo", diff --git a/src/client.js b/src/client.js index bec22c4..929b851 100644 --- a/src/client.js +++ b/src/client.js @@ -18,7 +18,6 @@ class Client extends EventEmitter this.version=version; this.isServer = !!isServer; this.splitter=framing.createSplitter(); - this.setSerializer(states.HANDSHAKING); this.packetsToParse={}; this.serializer; this.compressor=null; @@ -28,7 +27,6 @@ class Client extends EventEmitter this.decompressor=null; this.deserializer; this.isServer; - this.protocolState=states.HANDSHAKING; this.ended=true; this.latency=0; @@ -41,6 +39,8 @@ class Client extends EventEmitter const direction = this.isServer ? 'toServer' : 'toClient'; this.packetsToParse[event] -= 1; }); + + this.state=states.HANDSHAKING; } get state(){ @@ -109,19 +109,19 @@ class Client extends EventEmitter const oldProperty = this.protocolState; this.protocolState = newProperty; - if(!this.compressor) - { - this.serializer.unpipe(this.framer); - this.splitter.unpipe(this.deserializer); - } - else - { - this.serializer.unpipe(this.compressor); - this.decompressor.unpipe(this.deserializer); - } + if(this.serializer) { + if (!this.compressor) { + this.serializer.unpipe(); + this.splitter.unpipe(this.deserializer); + } + else { + this.serializer.unpipe(this.compressor); + this.decompressor.unpipe(this.deserializer); + } - this.serializer.removeAllListeners(); - this.deserializer.removeAllListeners(); + this.serializer.removeAllListeners(); + this.deserializer.removeAllListeners(); + } this.setSerializer(this.protocolState); if(!this.compressor) @@ -180,8 +180,8 @@ class Client extends EventEmitter this.framer.on('error', onError); this.splitter.on('error', onError); - this.socket.pipe(this.splitter).pipe(this.deserializer); - this.serializer.pipe(this.framer).pipe(this.socket); + this.socket.pipe(this.splitter); + this.framer.pipe(this.socket); } end(reason) { diff --git a/src/ping.js b/src/ping.js index 50232aa..1a3f7a6 100644 --- a/src/ping.js +++ b/src/ping.js @@ -40,7 +40,7 @@ function ping(options, cb) { // TODO: refactor with src/client/setProtocol.js client.on('connect', function() { client.write('set_protocol', { - protocolVersion: options.protocolVersion, + protocolVersion: -1, serverHost: options.host, serverPort: options.port, nextState: 1 From 936a12e6e019e6195a19fe3b618542f1e0bf78a0 Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Wed, 12 Jul 2017 13:17:00 +0200 Subject: [PATCH 3/3] fix ping --- src/ping.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ping.js b/src/ping.js index 1a3f7a6..50232aa 100644 --- a/src/ping.js +++ b/src/ping.js @@ -40,7 +40,7 @@ function ping(options, cb) { // TODO: refactor with src/client/setProtocol.js client.on('connect', function() { client.write('set_protocol', { - protocolVersion: -1, + protocolVersion: options.protocolVersion, serverHost: options.host, serverPort: options.port, nextState: 1