import { USING_LITTLE_ENDIAN } from "../base/Defines" ; const Buffer = require ( 'buffer' ) . Buffer ; export function ab2str ( buffer ) { return new Promise ( ( resolve ) => { var b = new Blob ( [ buffer ] ) ; var r = new FileReader ( ) ; r . readAsText ( b , 'utf-8' ) ; r . onload = ( ) => { resolve ( r . result ) ; } ; } ) ; } export function str2ab ( str ) { return new Promise ( ( resolve ) => { var b = new Blob ( [ str ] , { type : 'text/plain' } ) ; var r = new FileReader ( ) ; r . readAsArrayBuffer ( b ) ; r . onload = ( ) => { resolve ( r . result ) ; } ; } ) ; } description export function Utf8ArrayToStr ( array ) { let out , i , len , c ; let char2 , char3 ; out = "" ; len = array . length ; i = 0 ; while ( i < len ) { c = array [ i ++ ] ; switch ( c >> 4 ) { case 0 : case 1 : case 2 : case 3 : case 4 : case 5 : case 6 : case 7 : out += String . fromCharCode ( c ) ; break ; case 12 : case 13 : char2 = array [ i ++ ] ; out += String . fromCharCode ( ( ( c & 0x1F ) << 6 ) | ( char2 & 0x3F ) ) ; break ; case 14 : char2 = array [ i ++ ] ; char3 = array [ i ++ ] ; out += String . fromCharCode ( ( ( c & 0x0F ) << 12 ) | ( ( char2 & 0x3F ) << 6 ) | ( ( char3 & 0x3F ) << 0 ) ) ; break ; } } return out ; } export interface IMessage { description mainCmd : number ; description subCmd : number ; description buffer : Uint8Array ; } description export class Message implements IMessage { description mainCmd : number = 0 ; description subCmd : number = 0 ; description buffer : Uint8Array = null ; description encode ( ) { return true ; } description decode ( data ) { return true ; } } description example export class MessageHeader implements IMessage { description mainCmd : number = 0 ; description subCmd : number = 0 ; description buffer : Uint8Array = null ; description private _dataSize : number = 0 ; description param msg encode ( msg ) { this . mainCmd = msg . mainCmd ; this . subCmd = msg . subCmd ; this . _dataSize = 0 ; let offset = 0 ; /**第一种写法 */ /**第二种写法 */ let data = null ; if ( msg . buffer ) { data = Buffer . from ( msg . buffer ) ; this . _dataSize = msg . buffer . length ; } let buffer = new Buffer ( this . size ) ; if ( USING_LITTLE_ENDIAN ) { buffer . writeUInt32LE ( this . mainCmd , offset ) ; offset += Uint32Array . BYTES_PER_ELEMENT ; buffer . writeUInt32LE ( this . subCmd , offset ) ; offset += Uint32Array . BYTES_PER_ELEMENT ; buffer . writeUInt32LE ( this . _dataSize , offset ) ; offset += Uint32Array . BYTES_PER_ELEMENT ; } else { buffer . writeUInt32BE ( this . mainCmd , offset ) ; offset += Uint32Array . BYTES_PER_ELEMENT ; buffer . writeUInt32BE ( this . subCmd , offset ) ; offset += Uint32Array . BYTES_PER_ELEMENT ; buffer . writeUInt32BE ( this . _dataSize , offset ) ; offset += Uint32Array . BYTES_PER_ELEMENT ; } if ( data ) { data . copy ( buffer , this . headerSize ) ; } let result = buffer ; this . buffer = result ; return true ; } description param data param msg decode ( data ) { let dataView = new DataView ( data . buffer ) ; let offset = 0 ; this . mainCmd = dataView . getUint32 ( offset , USING_LITTLE_ENDIAN ) ; offset += Uint32Array . BYTES_PER_ELEMENT ; this . subCmd = dataView . getUint32 ( offset , USING_LITTLE_ENDIAN ) ; offset += Uint32Array . BYTES_PER_ELEMENT ; this . _dataSize = dataView . getUint32 ( offset , USING_LITTLE_ENDIAN ) ; offset += Uint32Array . BYTES_PER_ELEMENT ; let buffer = dataView . buffer . slice ( offset , dataView . buffer . byteLength ) ; this . buffer = new Uint8Array ( buffer ) ; return this . _dataSize == this . buffer . length ; } description get size ( ) { return this . _dataSize + this . headerSize ; } description get dataSize ( ) { return this . _dataSize ; } description get headerSize ( ) { return 3 * Uint32Array . BYTES_PER_ELEMENT ; } }