diff options
| author | TheMas3212 <mas3212@gmail.com> | 2023-09-23 12:21:43 +1000 |
|---|---|---|
| committer | TheMas3212 <mas3212@gmail.com> | 2023-09-23 12:21:43 +1000 |
| commit | f2aab487c22f4919d48896d33cd0f09ba0928395 (patch) | |
| tree | c0930fd74c887c59a009cc8bd36833c381ebed8a /src/packets/utils.ts | |
| parent | bdf1756baa223c89587abaa239001222263b665f (diff) | |
| download | tasd-js-f2aab487c22f4919d48896d33cd0f09ba0928395.tar.gz tasd-js-f2aab487c22f4919d48896d33cd0f09ba0928395.zip | |
Add additional Packet Types
Diffstat (limited to 'src/packets/utils.ts')
| -rw-r--r-- | src/packets/utils.ts | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/packets/utils.ts b/src/packets/utils.ts new file mode 100644 index 0000000..3a5befc --- /dev/null +++ b/src/packets/utils.ts @@ -0,0 +1,37 @@ +import { packetName } from "../constants"; +import { minUInt, writeUintN, writeUint8 } from "../utils"; + +export interface TASDPacket { + key: number; + size: number; + toBuffer(g_keylen: number): Uint8Array; + toString(): string; +} + +export function buildBuffer(g_keylen: number, key: number, payload: Uint8Array) { + const plen = payload.length; + const pexp = minUInt(plen); + const buffer = new Uint8Array(g_keylen + 1 + pexp + plen); + let index = 0; + writeUintN(key, buffer, index, g_keylen); + index += g_keylen; + writeUint8(pexp, buffer, index); + index += 1; + writeUintN(plen, buffer, index, pexp); + index += pexp; + buffer.set(payload, index); + return buffer; +} + +export class UnknownPacket implements TASDPacket { + constructor(public key: number, public payload: Uint8Array) {} + get size() { + return this.payload.length; + } + toBuffer(g_keylen: number) { + return buildBuffer(g_keylen, this.key, this.payload); + } + toString() { + return `UnknownPacket ${packetName(this.key)}, ${this.size}`; + } +}
\ No newline at end of file |
