-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
196 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,49 @@ | ||
import { isBigEndian } from '../util'; | ||
|
||
export type BufferIOOptions = { | ||
/** | ||
* @default 0 | ||
*/ | ||
byteOffset: number; | ||
/** | ||
* @default 'system' | ||
*/ | ||
endianness: 'big' | 'little' | 'system'; | ||
/** | ||
* @default 0 | ||
*/ | ||
byteOffset: number; | ||
/** | ||
* @default 'system' | ||
*/ | ||
endianness: 'big' | 'little' | 'system'; | ||
}; | ||
|
||
export class BufferIOBase { | ||
protected readonly uint8View: Uint8Array; | ||
protected readonly helperIntView: Int32Array; | ||
protected readonly helperFloatView: Float32Array; | ||
protected readonly helperByteView: Uint8Array; | ||
protected readonly switchEndianness: boolean; | ||
|
||
protected byteOffset = 0; | ||
|
||
constructor(buffer: ArrayBufferLike, options?: BufferIOOptions) { | ||
if (typeof Buffer !== 'undefined' && buffer instanceof Buffer) { | ||
// Getting rid of the outer shell, which causes the Uint8Array line to create a copy, instead of a view. | ||
buffer = buffer.buffer; | ||
} | ||
|
||
this.uint8View = new Uint8Array(buffer, 0); | ||
this.byteOffset = options?.byteOffset ?? 0; | ||
|
||
const helperBuffer = new ArrayBuffer(4); | ||
this.helperIntView = new Int32Array(helperBuffer); | ||
this.helperFloatView = new Float32Array(helperBuffer); | ||
this.helperByteView = new Uint8Array(helperBuffer); | ||
|
||
const isSystemBigEndian = isBigEndian(); | ||
const endianness = options?.endianness ?? 'system'; | ||
this.switchEndianness = | ||
(endianness === 'big' && !isSystemBigEndian) || | ||
(endianness === 'little' && isSystemBigEndian); | ||
} | ||
protected readonly uint8View: Uint8Array; | ||
protected readonly helperInt32View: Int32Array; | ||
protected readonly helperUint32View: Uint32Array; | ||
protected readonly helperFloatView: Float32Array; | ||
protected readonly helperByteView: Uint8Array; | ||
protected readonly switchEndianness: boolean; | ||
|
||
protected byteOffset = 0; | ||
|
||
get currentByteOffset() { | ||
return this.byteOffset; | ||
constructor(buffer: ArrayBufferLike, options?: BufferIOOptions) { | ||
if (typeof Buffer !== 'undefined' && buffer instanceof Buffer) { | ||
// Getting rid of the outer shell, which causes the Uint8Array line to create a copy, instead of a view. | ||
buffer = buffer.buffer; | ||
} | ||
|
||
this.uint8View = new Uint8Array(buffer, 0); | ||
this.byteOffset = options?.byteOffset ?? 0; | ||
|
||
const helperBuffer = new ArrayBuffer(4); | ||
this.helperInt32View = new Int32Array(helperBuffer); | ||
this.helperUint32View = new Uint32Array(helperBuffer); | ||
this.helperFloatView = new Float32Array(helperBuffer); | ||
this.helperByteView = new Uint8Array(helperBuffer); | ||
|
||
const isSystemBigEndian = isBigEndian(); | ||
const endianness = options?.endianness ?? 'system'; | ||
this.switchEndianness = | ||
(endianness === 'big' && !isSystemBigEndian) || | ||
(endianness === 'little' && isSystemBigEndian); | ||
} | ||
|
||
get currentByteOffset() { | ||
return this.byteOffset; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
export interface ISerialInput { | ||
readBool(): boolean; | ||
readByte(): number; | ||
readInt(): number; | ||
readFloat(): number; | ||
readInt32(): number; | ||
readUint32(): number; | ||
readFloat32(): number; | ||
readString(): string; | ||
readonly currentByteOffset: number; | ||
} | ||
|
||
export interface ISerialOutput { | ||
writeBool(value: boolean): void; | ||
writeByte(value: number): void; | ||
writeInt(value: number): void; | ||
writeFloat(value: number): void; | ||
writeInt32(value: number): void; | ||
writeUint32(value: number): void; | ||
writeFloat32(value: number): void; | ||
writeString(value: string): void; | ||
readonly currentByteOffset: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.