import net from 'net' ; import { PacketLogger } from 'packetlogger' ; import { OutPacketBase } from 'packets/out/packet' ; import { UserSession } from 'user/usersession' ; const MIN_SEQUENCE = 0 ; const MAX_SEQUENCE = 255 ; class export class ExtendedSocket extends net . Socket { public static from ( socket , packetDumper ) { const newSocket = new ExtendedSocket ( ) ; Object . assign ( newSocket , socket ) ; newSocket . session = null ; newSocket . realInSeq = MIN_SEQUENCE ; newSocket . realOutSeq = MIN_SEQUENCE ; newSocket . packetDumper = packetDumper ; return newSocket ; } public uuid : string ; public session : UserSession ; private seq : number ; private realInSeq : number ; private realOutSeq : number ; private packetDumper : PacketLogger ; /**
     * returns the current sequence and increments it
     */ public getNextSeq ( ) { if ( this . seq > MAX_SEQUENCE ) { this . resetReq ( ) ; } return this . seq ++ ; } /**
     * get the current incoming packet real sequence
     */ public getRealInSeq ( ) { return this . realInSeq ++ ; } /**
     * get the current outgoing packet real sequence
     */ public getRealOutSeq ( ) { return this . realOutSeq ++ ; } /**
     * sets the sequence to the start
     */ public resetReq ( ) { this . seq = MIN_SEQUENCE ; } param packet returns public send ( packet ) { if ( this . destroyed === true ) { console . warn ( 'ExtendedSocket::send: tried to send with a broken socket. This is most likely a bug' ) ; return false ; } const data = packet . getData ( ) ; data . writeUInt8 ( this . getNextSeq ( ) , 1 ) ; if ( this . packetDumper ) { this . packetDumper . dumpOut ( this . uuid , this . getRealOutSeq ( ) , packet . id , data ) ; } return super . write ( data ) ; } param buff public sendBuffer ( buff ) { if ( this . destroyed === true ) { console . warn ( 'ExtendedSocket::sendBuffer: tried to send with a broken socket. This is most likely a bug' ) ; return false ; } buff . writeUInt8 ( this . getNextSeq ( ) , 1 ) ; return super . write ( buff ) ; } }