import { SocketHandler } from './Structures/Base/SocketHandler' ; import { Socket as NetSocket } from 'net' ; import { Server } from './Server' ; import { makeError } from './Structures/MessageError' ; import { NetworkError , VCLOSE } from './Util/Shared' ; since export enum ServerSocketStatus { since Connected , since Identifiying , since Disconnected } export class ServerSocket extends SocketHandler { public readonly server : Server ; public status = ServerSocketStatus . Disconnected ; public constructor ( server , socket ) { super ( null , socket ) ; this . server = server ; } public async setup ( ) { this . status = ServerSocketStatus . Identifiying ; this . socket ! . on ( 'data' , this . _onData . bind ( this ) ) . on ( 'error' , this . _onError . bind ( this ) ) . on ( 'close' , this . _onClose . bind ( this ) ) ; try { const sName = await this . send ( this . server . name ) ; if ( typeof sName !== 'string' ) { return this . disconnect ( ) ; } this . status = ServerSocketStatus . Connected ; this . name = sName ; const existing = this . server . sockets . get ( sName ) ; if ( existing ) { existing . disconnect ( true ) ; } this . server . sockets . set ( sName , this ) ; this . server . emit ( 'connect' , this ) ; } catch { this . disconnect ( ) ; } } /**
     * Disconnect from the socket, this will also reject all messages
     */ public disconnect ( close ? ) { if ( this . status === ServerSocketStatus . Disconnected ) return false ; if ( close ) this . socket . end ( VCLOSE ) ; this . socket . destroy ( ) ; if ( this . queue . size ) { const rejectError = new Error ( 'Socket has been disconnected.' ) ; for ( const element of this . queue . values ( ) ) element . reject ( rejectError ) ; } this . status = ServerSocketStatus . Disconnected ; if ( this . name ) { this . server . sockets . delete ( this . name ) ; } this . socket . removeAllListeners ( ) ; this . server . emit ( 'disconnect' , this ) ; return true ; } protected _onData ( data ) { this . server . emit ( 'raw' , data , this ) ; for ( const processed of this . queue . process ( data ) ) { if ( processed . id === null ) { if ( this . status === ServerSocketStatus . Connected ) { this . server . emit ( 'error' , makeError ( 'Failed to parse message' , processed . data ) , this ) ; } else { this . server . emit ( 'error' , makeError ( 'Failed to process message during connection, calling disconnect' , processed . data ) , this ) ; this . disconnect ( ) ; } } else { const message = this . _handleMessage ( processed ) ; if ( message ) this . server . emit ( 'message' , message , this ) ; } } } private _onError ( error ) { this . server . emit ( 'error' , error , this ) ; } private _onClose ( ) { this . disconnect ( ) ; } }