import { Int64BE , Int64LE , Uint64BE , Uint64LE } from 'int64-buffer' ; import { WritableStreamBuffer } from 'stream-buffers' ; import { PacketBaseShared } from 'packets/packetbaseshared' ; import { PacketId , PacketSignature } from 'packets/definitions' ; import { PacketLongString } from 'packets/packetlongstring' ; import { PacketString } from 'packets/packetstring' ; class export class OutPacketBase extends PacketBaseShared { protected outStream : WritableStreamBuffer ; private builtBuffer : Buffer ; constructor ( id ) { super ( ) ; this . sequence = null ; this . id = id ; } returns public getData ( ) { if ( this . builtBuffer == null ) { this . builtBuffer = this . outStream . getContents ( ) as Buffer ; const dataLen = this . builtBuffer . byteLength - OutPacketBase . headerLength ; this . builtBuffer . writeUInt16LE ( dataLen , 2 ) ; } return this . builtBuffer ; } param val public writeInt8 ( val ) { const buf = Buffer . alloc ( 1 ) ; buf . writeInt8 ( val , 0 ) ; this . outStream . write ( buf ) ; } param val param littleEndian public writeInt16 ( val , littleEndian = true ) { const buf = Buffer . alloc ( 2 ) ; if ( littleEndian ) { buf . writeInt16LE ( val , 0 ) ; } else { buf . writeInt16BE ( val , 0 ) ; } this . outStream . write ( buf ) ; } param val param littleEndian public writeInt32 ( val , littleEndian = true ) { const buf = Buffer . alloc ( 4 ) ; if ( littleEndian ) { buf . writeInt32LE ( val , 0 ) ; } else { buf . writeInt32BE ( val , 0 ) ; } this . outStream . write ( buf ) ; } param val param littleEndian public writeInt64 ( val , littleEndian = true ) { let buf = null ; if ( littleEndian ) { buf = ( val as Int64LE ) . toBuffer ( ) ; } else { buf = ( val as Int64BE ) . toBuffer ( ) ; } this . outStream . write ( buf ) ; } param val public writeUInt8 ( val ) { const buf = Buffer . alloc ( 1 ) ; buf . writeUInt8 ( val , 0 ) ; this . outStream . write ( buf ) ; } param val param littleEndian public writeUInt16 ( val , littleEndian = true ) { const buf = Buffer . alloc ( 2 ) ; if ( littleEndian ) { buf . writeUInt16LE ( val , 0 ) ; } else { buf . writeUInt16BE ( val , 0 ) ; } this . outStream . write ( buf ) ; } param val param littleEndian public writeUInt32 ( val , littleEndian = true ) { const buf = Buffer . alloc ( 4 ) ; if ( littleEndian ) { buf . writeUInt32LE ( val , 0 ) ; } else { buf . writeUInt32BE ( val , 0 ) ; } this . outStream . write ( buf ) ; } param val param littleEndian public writeUInt64 ( val , littleEndian = true ) { let buf = null ; if ( littleEndian ) { buf = ( val as Uint64LE ) . toBuffer ( ) ; } else { buf = ( val as Uint64BE ) . toBuffer ( ) ; } this . outStream . write ( buf ) ; } public writeLongString ( str ) { const serializedStr = new PacketLongString ( str ) ; this . outStream . write ( serializedStr . toBuffer ( ) ) ; } public writeString ( str ) { const serializedStr = new PacketString ( str ) ; this . outStream . write ( serializedStr . toBuffer ( ) ) ; } /**
     * build the packet's header
     */ protected buildHeader ( ) { this . writeUInt8 ( PacketSignature ) ; this . writeUInt8 ( this . sequence ) ; this . writeUInt16 ( 0 ) ; this . writeUInt8 ( this . id ) ; } }