UNPKG

@slippi/slippi-js

Version:
63 lines (60 loc) 2.16 kB
import { decode, encode } from '@shelacek/ubjson'; var CommunicationType; (function (CommunicationType) { CommunicationType[CommunicationType["HANDSHAKE"] = 1] = "HANDSHAKE"; CommunicationType[CommunicationType["REPLAY"] = 2] = "REPLAY"; CommunicationType[CommunicationType["KEEP_ALIVE"] = 3] = "KEEP_ALIVE"; })(CommunicationType || (CommunicationType = {})); // This class is responsible for handling the communication protocol between the Wii and the // desktop app class ConsoleCommunication { constructor() { this.receiveBuf = Buffer.from([]); this.messages = new Array(); } receive(data) { this.receiveBuf = Buffer.concat([this.receiveBuf, data]); while (this.receiveBuf.length >= 4) { // First get the size of the message we are expecting const msgSize = this.receiveBuf.readUInt32BE(0); if (this.receiveBuf.length < msgSize + 4) { // If we haven't received all the data yet, let's wait for more return; } // Here we have received all the data, so let's decode it const ubjsonData = this.receiveBuf.slice(4, msgSize + 4); this.messages.push(decode(ubjsonData.buffer)); // Remove the processed data from receiveBuf this.receiveBuf = this.receiveBuf.slice(msgSize + 4); } } getReceiveBuffer() { return this.receiveBuf; } getMessages() { const toReturn = this.messages; this.messages = []; return toReturn; } genHandshakeOut(cursor, clientToken, isRealtime = false) { const clientTokenBuf = Buffer.from([0, 0, 0, 0]); clientTokenBuf.writeUInt32BE(clientToken, 0); const message = { type: CommunicationType.HANDSHAKE, payload: { cursor: cursor, clientToken: Uint8Array.from(clientTokenBuf), // TODO: Use real instance token isRealtime: isRealtime } }; const buf = encode(message, { optimizeArrays: true }); const msg = Buffer.concat([Buffer.from([0, 0, 0, 0]), Buffer.from(buf)]); msg.writeUInt32BE(buf.byteLength, 0); return msg; } } export { CommunicationType, ConsoleCommunication }; //# sourceMappingURL=communication.esm.js.map