From 16ddf23ff4a52cbc6f6843cb0967ff2cb20fe461 Mon Sep 17 00:00:00 2001 From: modeco80 Date: Tue, 2 Apr 2024 10:16:34 -0400 Subject: [PATCH] remove vestigeal crusttest stuff --- backend/src/SocketComputerServer.ts | 26 +++--- shared/src/Protocol.ts | 134 +--------------------------- shared/src/UsernameValidator.ts | 9 -- shared/src/index.ts | 1 - shared/test/Protocol.test.ts | 4 +- 5 files changed, 15 insertions(+), 159 deletions(-) delete mode 100644 shared/src/UsernameValidator.ts diff --git a/backend/src/SocketComputerServer.ts b/backend/src/SocketComputerServer.ts index 9a737cf..2c0f281 100644 --- a/backend/src/SocketComputerServer.ts +++ b/backend/src/SocketComputerServer.ts @@ -13,6 +13,9 @@ import { WebSocket } from 'ws'; import Queue from 'mnemonist/queue.js'; +// for the maximum socket.io experience +const kCanvasJpegQuality = 0.35; + class VMUser { public connection: WebSocket; public username: string; @@ -215,6 +218,8 @@ class VirtualMachine extends EventEmitter { async RemUser(user: VMUser) { // TODO: erase from turn queue (once we have it) wired up + console.log(user.username, 'left.'); + this.users.splice(this.users.indexOf(user), 1); // bye-bye! @@ -237,10 +242,6 @@ class VirtualMachine extends EventEmitter { private async OnDecodedMessage(user: VMUser, message: Shared.DeserializedMessage) { switch (message.type) { - case Shared.MessageType.Chat: - console.log(`${user.username} > ${(message as Shared.ChatMessage).message}`); - break; - case Shared.MessageType.Turn: this.queue.TryEnqueue(user); break; @@ -296,13 +297,10 @@ class VirtualMachine extends EventEmitter { let canvas = self.display.GetCanvas(); - if(canvas == null) - return; + let buffer = canvas.toBuffer('image/jpeg', { quality: kCanvasJpegQuality }); - let buffer = canvas.toBuffer('image/jpeg', { quality: 0.75 }); - - await this.BroadcastMessage((encoder: Shared.MessageEncoder) => { - encoder.Init(buffer.length + 256); + await self.BroadcastMessage((encoder: Shared.MessageEncoder) => { + encoder.Init(buffer.length + 4); encoder.SetDisplayRectMessage(0, 0, buffer); return encoder.Finish(); }); @@ -312,10 +310,10 @@ class VirtualMachine extends EventEmitter { let canvas = new Canvas(rect.width, rect.height); canvas.getContext('2d').putImageData(rect, 0, 0); - let buffer = canvas.toBuffer('image/jpeg', { quality: 0.75 }); + let buffer = canvas.toBuffer('image/jpeg', { quality: kCanvasJpegQuality }); await this.BroadcastMessage((encoder: Shared.MessageEncoder) => { - encoder.Init(buffer.length + 256); + encoder.Init(buffer.length + 4); encoder.SetDisplayRectMessage(x, y, buffer); return encoder.Finish(); }); @@ -327,7 +325,7 @@ class VirtualMachine extends EventEmitter { private async sendFullScreen(user: VMUser) { if (this.display == null) return; - let buffer = this.display.GetCanvas().toBuffer('image/jpeg', { quality: 0.75 }); + let buffer = this.display.GetCanvas().toBuffer('image/jpeg', { quality: kCanvasJpegQuality }); await user.SendMessage((encoder: Shared.MessageEncoder) => { encoder.Init(8); @@ -336,7 +334,7 @@ class VirtualMachine extends EventEmitter { }); await user.SendMessage((encoder: Shared.MessageEncoder) => { - encoder.Init(buffer.length + 256); + encoder.Init(buffer.length + 4); encoder.SetDisplayRectMessage(0, 0, buffer); return encoder.Finish(); }); diff --git a/shared/src/Protocol.ts b/shared/src/Protocol.ts index f929b11..43605c1 100644 --- a/shared/src/Protocol.ts +++ b/shared/src/Protocol.ts @@ -10,17 +10,9 @@ export enum MessageType { DisplayRect, DisplaySize, // display changed size - // chat - Chat, - ChatHistory, - // user AddUser, RemUser, - - // currently aren't used - RenUser, - Rename, } export enum MouseButtons { @@ -31,16 +23,8 @@ export enum MouseButtons { WheelDn = 1 << 4 } -export enum RenameResult { - Ok, - InvalidUsername, /// This username is too long or otherwise invalid - UsernameTaken /// This username is taken on this slot. -} - - export const kMaxUserNameLength = 24; -export const kMaxChatMessageLength = 150; /// This is a 16-bit value, sort of a structure if you will. /// 0x55 is the actual magic value. @@ -95,15 +79,6 @@ export type DisplaySizeMessage = DeserializedMessageRoot & { height: number; }; -export type ChatMessage = DeserializedMessageRoot & { - message: string; -}; - -export type ChatServerMessage = DeserializedMessageRoot & ChatMessageObject; - -export type ChatHistoryMessage = DeserializedMessageRoot & { - history: ChatMessageObject[]; -}; export type AddUserMessage = DeserializedMessageRoot & { user: string; @@ -113,19 +88,6 @@ export type RemUserMessage = DeserializedMessageRoot & { user: string; }; -export type RenUserMessage = DeserializedMessageRoot & { - prevUsername: string; - newUsername: string; -}; - -export type RenameMessage = DeserializedMessageRoot & { - newUsername: string; -}; - -export type RenameServerMessage = DeserializedMessageRoot & { - result: RenameResult; -}; - export type AnyMessage = | KeyMessage @@ -134,14 +96,7 @@ export type AnyMessage = | TurnServerMessage | DisplayRectMessage | DisplaySizeMessage - | ChatMessage - | ChatServerMessage - | ChatHistoryMessage - | AddUserMessage - | RemUserMessage - | RenUserMessage - | RenameMessage - | RenameServerMessage; + | RemUserMessage; export type DeserializedMessage = AnyMessage; @@ -193,21 +148,6 @@ export class MessageEncoder { this.struct.WriteU16(h); } - SetChatMessage(msg) { - this.SetTypeCode(MessageType.Chat); - this.struct.WriteStringLen(msg, kMaxChatMessageLength); - } - - SetChatSrvMessage(user: string, msg: string) { - this.SetTypeCode(MessageType.Chat); - this.struct.WriteStringLen(user, kMaxUserNameLength); - this.struct.WriteStringLen(msg, kMaxChatMessageLength); - } - - SetChatHistoryMessage(history: ChatMessageObject[]) { - this.SetTypeCode(MessageType.ChatHistory); - this.struct.WriteArray(history, this.AddChatSrvMessage); - } SetAddUserMessage(user: string) { this.SetTypeCode(MessageType.AddUser); @@ -219,21 +159,6 @@ export class MessageEncoder { this.struct.WriteString(user); } - SetRenUserMessage(prevUsername: string, newUsername: string) { - this.SetTypeCode(MessageType.RenUser); - this.struct.WriteStringLen(prevUsername, kMaxUserNameLength); - this.struct.WriteStringLen(newUsername, kMaxUserNameLength); - } - - SetRenameMessage(username: string) { - this.SetTypeCode(MessageType.Rename); - this.struct.WriteStringLen(username, kMaxUserNameLength); - } - - SetRenameServerMessage(result: RenameResult) { - this.SetTypeCode(MessageType.Rename); - this.struct.WriteU8(result); - } // Setup some stuff and then return the final message Finish() { @@ -257,24 +182,8 @@ export class MessageEncoder { this.struct.WriteU16(type); this.struct.Seek(oldOff); } - - // TODO rename to AddChatMessageObject. lazy - private AddChatSrvMessage(message: ChatMessageObject) { - this.struct.WriteStringLen(message.username, kMaxUserNameLength); - this.struct.WriteStringLen(message.message, kMaxChatMessageLength); - } } -// goofy, but.. reentrancy! -class MessageDecoderExtContext { - public struct: Struct; - - ReadChatSrvMessage() { - let msg: ChatMessageObject; - msg.username = this.struct.ReadStringLen(kMaxUserNameLength); - msg.message = this.struct.ReadStringLen(kMaxChatMessageLength); - } -} export class MessageDecoder { @@ -289,8 +198,6 @@ export class MessageDecoder { private static ReadMessageSync(buffer: ArrayBuffer, asClient: boolean, callback: (err: Error, message:DeserializedMessage) => void) { let struct = new Struct(buffer); - let context = new MessageDecoderExtContext(); - context.struct = struct; // Read and verify the header let header: ProtocolHeader = { @@ -357,26 +264,6 @@ export class MessageDecoder { return callback(null, message); break; - case MessageType.Chat: - if (asClient) { - (message as unknown as ChatMessageObject).username = struct.ReadStringLen(kMaxUserNameLength); - (message as unknown as ChatMessageObject).message = struct.ReadStringLen(kMaxChatMessageLength); - } else { - // the client->server version of this message only has the content - (message as ChatMessage).message = struct.ReadStringLen(kMaxChatMessageLength); - } - return callback(null, message); - break; - - case MessageType.ChatHistory: - if (asClient) { - (message as ChatHistoryMessage).history = struct.ReadArray(context.ReadChatSrvMessage); - return callback(null, message); - } else { - return callback(new Error('unexpected client->server message'), null); - } - break; - case MessageType.AddUser: if (asClient) { (message as AddUserMessage).user = struct.ReadString(); @@ -394,25 +281,6 @@ export class MessageDecoder { } break; - case MessageType.RenUser: - if (asClient) { - (message as RenUserMessage).prevUsername = struct.ReadStringLen(kMaxUserNameLength); - (message as RenUserMessage).newUsername = struct.ReadStringLen(kMaxUserNameLength); - return callback(null, message); - } else { - return callback(new Error('unexpected client->server message'), null); - } - break; - - case MessageType.Rename: - if (asClient) { - (message as RenameServerMessage).result = struct.ReadU8() as RenameResult; - } else { - (message as RenameMessage).newUsername = struct.ReadStringLen(kMaxUserNameLength); - } - return callback(null, message); - break; - default: return callback(new Error(`unknown type code ${header.type}`), null); } diff --git a/shared/src/UsernameValidator.ts b/shared/src/UsernameValidator.ts deleted file mode 100644 index 137cd57..0000000 --- a/shared/src/UsernameValidator.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { kMaxUserNameLength, RenameResult } from './Protocol.js'; - -// This function simply validates that the username is ok to set. if it's not -// then we don't even bother checking if the user already exists, in the backend -// code we'll just give up. -export function ValidateUsername(username: string): RenameResult { - if (!/^[a-zA-Z0-9\ \-\_\.]+$/.test(username) || username.length > kMaxUserNameLength || username.trim().length < 3) return RenameResult.InvalidUsername; - return RenameResult.Ok; -} diff --git a/shared/src/index.ts b/shared/src/index.ts index a6820d2..5e51cc5 100644 --- a/shared/src/index.ts +++ b/shared/src/index.ts @@ -1,2 +1 @@ export * from './Protocol.js'; -export * from './UsernameValidator.js'; diff --git a/shared/test/Protocol.test.ts b/shared/test/Protocol.test.ts index 60e4c8c..76d4f7b 100644 --- a/shared/test/Protocol.test.ts +++ b/shared/test/Protocol.test.ts @@ -25,7 +25,7 @@ function encodeMessage(f: (encoder: MessageEncoder) => void) { console.log('[CLIENT TESTS]'); await decodeMessage( encodeMessage((encoder) => { - encoder.SetChatMessage('Hi, CrustTest World!'); + encoder.SetKeyMessage(100, true); }), false ); @@ -44,7 +44,7 @@ function encodeMessage(f: (encoder: MessageEncoder) => void) { // server->client tests await decodeMessage( encodeMessage((encoder) => { - encoder.SetChatSrvMessage('tester19200', 'Hi, CrustTest World!'); + encoder.SetDisplaySizeMessage(1024, 1024); }), true );