From e22dfea5999916d65c86216f02eaf8e4c330e4f7 Mon Sep 17 00:00:00 2001 From: extremeheat Date: Wed, 24 Mar 2021 06:10:56 -0400 Subject: [PATCH] Fix server/client closing --- src/client.js | 11 ++++++++--- src/connection.js | 4 +++- src/server.js | 6 ++++-- src/serverPlayer.js | 8 +++++--- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/client.js b/src/client.js index b270c2c..bd841b3 100644 --- a/src/client.js +++ b/src/client.js @@ -108,7 +108,7 @@ class Client extends Connection { } onDisconnectRequest (packet) { - console.warn(`Server requested ${packet.hide_disconnect_reason ? 'silent disconnect' : 'disconnect'}: ${packet.message}`) + console.warn(`C Server requested ${packet.hide_disconnect_reason ? 'silent disconnect' : 'disconnect'}: ${packet.message}`) this.emit('kick', packet) } @@ -130,7 +130,7 @@ class Client extends Connection { this.q2 = [] this.connection?.close() this.removeAllListeners() - console.log('Closed!') + console.log('Client closed!') } tryRencode (name, params, actual) { @@ -154,7 +154,7 @@ class Client extends Connection { const des = this.deserializer.parsePacketBuffer(packet) const pakData = { name: des.data.name, params: des.data.params } this.inLog('-> C', pakData.name/*, serialize(pakData.params).slice(0, 100) */) - this.emit('packet', pakData) + this.emit('packet', des) if (debugging) { // Packet verifying (decode + re-encode + match test) @@ -177,6 +177,11 @@ class Client extends Connection { case 'play_status': this.onPlayStatus(pakData.params) break + default: + if (this.status !== ClientStatus.Initializing && this.status !== ClientStatus.Initialized) { + this.inLog(`Can't accept ${des.data.name}, client not yet authenticated : ${this.status}`) + return + } } // Emit packet diff --git a/src/connection.js b/src/connection.js index 220013c..a9061c0 100644 --- a/src/connection.js +++ b/src/connection.js @@ -16,6 +16,8 @@ const ClientStatus = { class Connection extends EventEmitter { status = ClientStatus.Disconnected + q = [] + q2 = [] versionLessThan (version) { if (typeof version === 'string') { @@ -121,7 +123,7 @@ class Connection extends EventEmitter { // TODO: Rename this to sendEncapsulated sendMCPE (buffer, immediate) { - if (this.connection.connected === false) return + if (this.connection.connected === false || this.status === ClientStatus.Disconnected) return this.connection.sendReliable(buffer, immediate) } diff --git a/src/server.js b/src/server.js index 2c5187c..bf440d3 100644 --- a/src/server.js +++ b/src/server.js @@ -15,8 +15,8 @@ class Server extends EventEmitter { /** @type {Object} */ this.clients = {} this.clientCount = 0 - this.inLog = (...args) => debug('C -> S', ...args) - this.outLog = (...args) => debug('S -> C', ...args) + this.inLog = (...args) => debug('S ->', ...args) + this.outLog = (...args) => debug('S <-', ...args) } validateOptions () { @@ -40,6 +40,8 @@ class Server extends EventEmitter { onCloseConnection = (inetAddr, reason) => { console.debug('close connection', inetAddr, reason) + delete this.clients[inetAddr]?.connection // Prevent close loop + this.clients[inetAddr]?.close() delete this.clients[inetAddr] this.clientCount-- } diff --git a/src/serverPlayer.js b/src/serverPlayer.js index 49f7917..6fb03bb 100644 --- a/src/serverPlayer.js +++ b/src/serverPlayer.js @@ -21,8 +21,8 @@ class Player extends Connection { this.startQueue() this.status = ClientStatus.Authenticating - this.inLog = (...args) => console.info('S -> C', ...args) - this.outLog = (...args) => console.info('C -> S', ...args) + this.inLog = (...args) => console.info('S ->', ...args) + this.outLog = (...args) => console.info('S <-', ...args) } getData () { @@ -82,7 +82,7 @@ class Player extends Connection { /** * Disconnects a client */ - disconnect (reason, hide = false) { + disconnect (reason = 'Server closed', hide = false) { if ([ClientStatus.Authenticating, ClientStatus.Initializing].includes(this.status)) { this.sendDisconnectStatus('failed_server_full') } else { @@ -110,6 +110,7 @@ class Player extends Connection { clearInterval(this.loop) this.connection?.close() this.removeAllListeners() + this.status = ClientStatus.Disconnected } readPacket (packet) { @@ -136,6 +137,7 @@ class Player extends Connection { break case 'set_local_player_as_initialized': this.status = ClientStatus.Initialized + this.inLog('Server client spawned') // Emit the 'spawn' event this.emit('spawn') break