summaryrefslogtreecommitdiff
path: root/src/packets/input.ts
blob: e2e712d68873697282bcdae93c9e9ecb9555bbd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { PACKET_TYPES } from "../constants";
import { readUint32, readUint64, readUint8, writeUint32, writeUint64, writeUint8 } from "../utils";
import { TASDPacket, buildBuffer } from "./utils";

export class InputChunkPacket implements TASDPacket {
  constructor(public port: number, public inputs: Uint8Array) {}
  get key() {
    return PACKET_TYPES.INPUT_CHUNK;
  }
  get size() {
    return this.inputs.length + 1;
  }
  static fromBuffer(buffer: Uint8Array) {
    return new this(readUint8(buffer, 0), buffer.subarray(1));
  }
  toBuffer(g_keylen: number): Uint8Array {
    const payload = new Uint8Array(this.inputs.length + 1);
    writeUint8(this.port, payload, 0);
    payload.set(this.inputs, 1);
    return buildBuffer(g_keylen, this.key, payload);
  }
  toString() {
    return `InputChunk ${this.port}, ${this.size}`;
  }
}

export class InputMomentPacket implements TASDPacket {
  constructor(
    public port: number,
    public indexType: number,
    public index: bigint,
    public inputs: Uint8Array
  ) {}
  get key() {
    return PACKET_TYPES.INPUT_MOMENT;
  }
  get size() {
    return 10 + this.inputs.length;
  }
  static fromBuffer(buffer: Uint8Array) {
    const port = readUint8(buffer, 0);
    const indexType = readUint8(buffer, 1);
    const index = readUint64(buffer, 2);
    const inputs = buffer.subarray(10);
    return new this(port, indexType, index, inputs);
  }
  toBuffer(g_keylen: number): Uint8Array {
    const payload = new Uint8Array(this.size);
    writeUint8(this.port, payload, 0);
    writeUint8(this.indexType, payload, 1);
    writeUint64(this.index, payload, 2);
    payload.set(this.inputs, 10);
    return buildBuffer(g_keylen, this.key, payload);
  }
  toString(): string {
    return `InputMoment ${this.port}, ${this.indexType}, ${this.index}, ${this.inputs.length}`;
  }
}

export class TransitionPacket implements TASDPacket {
  constructor(
    public indexType: number,
    public port: number,
    public index: bigint,
    public type: number,
    public data: Uint8Array
  ) {}
  get key() {
    return PACKET_TYPES.TRANSITION;
  }
  get size() {
    return 11 + this.data.length;
  }
  static fromBuffer(buffer: Uint8Array) {
    const indexType = readUint8(buffer, 0);
    const port = readUint8(buffer, 1);
    const index = readUint64(buffer, 2);
    const type = readUint8(buffer, 10);
    const data = buffer.subarray(11);
    return new this(indexType, port, index, type, data);
  }
  toBuffer(g_keylen: number): Uint8Array {
    const payload = new Uint8Array(this.size);
    writeUint8(this.indexType, payload, 0);
    writeUint8(this.port, payload, 1);
    writeUint64(this.index, payload, 2);
    writeUint8(this.type, payload, 10);
    payload.set(this.data, 11);
    return buildBuffer(g_keylen, this.key, payload);
  }
  toString(): string {
    return `Transition ${this.indexType}, ${this.index}, ${this.type}, ${this.data.length}`;
  }
}

export class LagFrameChunkPacket implements TASDPacket {
  constructor(public frame: number, public count: number) {}
  get key() {
    return PACKET_TYPES.LAG_FRAME_CHUNK;
  }
  get size() {
    return 8;
  }
  static fromBuffer(buffer: Uint8Array) {
    const frame = readUint32(buffer, 0);
    const count = readUint32(buffer, 4);
    return new this(frame, count);
  }
  toBuffer(g_keylen: number): Uint8Array {
    const payload = new Uint8Array(this.size);
    writeUint32(this.frame, payload, 0);
    writeUint32(this.count, payload, 4);
    return buildBuffer(g_keylen, this.key, payload);
  }
  toString(): string {
    return `LagFrameChunk ${this.frame}, ${this.count}`;
  }
}

export class MovieTransitionPacket implements TASDPacket {
  constructor(public frame: number, public type: number, public data: Uint8Array) {}
  get key() {
    return PACKET_TYPES.MOVIE_TRANSITION;
  }
  get size() {
    return 5 + this.data.length;
  }
  static fromBuffer(buffer: Uint8Array) {
    const frame = readUint32(buffer, 0);
    const type = readUint8(buffer, 4);
    const data = buffer.subarray(5);
    return new this(frame, type, data);
  }
  toBuffer(g_keylen: number): Uint8Array {
    const payload = new Uint8Array(this.size);
    writeUint32(this.frame, payload, 0);
    writeUint8(this.type, payload, 4);
    payload.set(this.data, 5);
    return buildBuffer(g_keylen, this.key, payload);
  }
  toString(): string {
    return `MovieTransition ${this.frame}, ${this.type}, ${this.data.length}`;
  }
}