Fix server/client closing
This commit is contained in:
parent
cf6471f6eb
commit
e22dfea599
4 changed files with 20 additions and 9 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -15,8 +15,8 @@ class Server extends EventEmitter {
|
|||
/** @type {Object<string, Player>} */
|
||||
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--
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue