remove vestigeal crusttest stuff
This commit is contained in:
parent
071b531679
commit
16ddf23ff4
5 changed files with 15 additions and 159 deletions
|
@ -13,6 +13,9 @@ import { WebSocket } from 'ws';
|
||||||
|
|
||||||
import Queue from 'mnemonist/queue.js';
|
import Queue from 'mnemonist/queue.js';
|
||||||
|
|
||||||
|
// for the maximum socket.io experience
|
||||||
|
const kCanvasJpegQuality = 0.35;
|
||||||
|
|
||||||
class VMUser {
|
class VMUser {
|
||||||
public connection: WebSocket;
|
public connection: WebSocket;
|
||||||
public username: string;
|
public username: string;
|
||||||
|
@ -215,6 +218,8 @@ class VirtualMachine extends EventEmitter {
|
||||||
async RemUser(user: VMUser) {
|
async RemUser(user: VMUser) {
|
||||||
// TODO: erase from turn queue (once we have it) wired up
|
// TODO: erase from turn queue (once we have it) wired up
|
||||||
|
|
||||||
|
console.log(user.username, 'left.');
|
||||||
|
|
||||||
this.users.splice(this.users.indexOf(user), 1);
|
this.users.splice(this.users.indexOf(user), 1);
|
||||||
|
|
||||||
// bye-bye!
|
// bye-bye!
|
||||||
|
@ -237,10 +242,6 @@ class VirtualMachine extends EventEmitter {
|
||||||
|
|
||||||
private async OnDecodedMessage(user: VMUser, message: Shared.DeserializedMessage) {
|
private async OnDecodedMessage(user: VMUser, message: Shared.DeserializedMessage) {
|
||||||
switch (message.type) {
|
switch (message.type) {
|
||||||
case Shared.MessageType.Chat:
|
|
||||||
console.log(`${user.username} > ${(message as Shared.ChatMessage).message}`);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case Shared.MessageType.Turn:
|
case Shared.MessageType.Turn:
|
||||||
this.queue.TryEnqueue(user);
|
this.queue.TryEnqueue(user);
|
||||||
break;
|
break;
|
||||||
|
@ -296,13 +297,10 @@ class VirtualMachine extends EventEmitter {
|
||||||
|
|
||||||
let canvas = self.display.GetCanvas();
|
let canvas = self.display.GetCanvas();
|
||||||
|
|
||||||
if(canvas == null)
|
let buffer = canvas.toBuffer('image/jpeg', { quality: kCanvasJpegQuality });
|
||||||
return;
|
|
||||||
|
|
||||||
let buffer = canvas.toBuffer('image/jpeg', { quality: 0.75 });
|
await self.BroadcastMessage((encoder: Shared.MessageEncoder) => {
|
||||||
|
encoder.Init(buffer.length + 4);
|
||||||
await this.BroadcastMessage((encoder: Shared.MessageEncoder) => {
|
|
||||||
encoder.Init(buffer.length + 256);
|
|
||||||
encoder.SetDisplayRectMessage(0, 0, buffer);
|
encoder.SetDisplayRectMessage(0, 0, buffer);
|
||||||
return encoder.Finish();
|
return encoder.Finish();
|
||||||
});
|
});
|
||||||
|
@ -312,10 +310,10 @@ class VirtualMachine extends EventEmitter {
|
||||||
let canvas = new Canvas(rect.width, rect.height);
|
let canvas = new Canvas(rect.width, rect.height);
|
||||||
canvas.getContext('2d').putImageData(rect, 0, 0);
|
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) => {
|
await this.BroadcastMessage((encoder: Shared.MessageEncoder) => {
|
||||||
encoder.Init(buffer.length + 256);
|
encoder.Init(buffer.length + 4);
|
||||||
encoder.SetDisplayRectMessage(x, y, buffer);
|
encoder.SetDisplayRectMessage(x, y, buffer);
|
||||||
return encoder.Finish();
|
return encoder.Finish();
|
||||||
});
|
});
|
||||||
|
@ -327,7 +325,7 @@ class VirtualMachine extends EventEmitter {
|
||||||
private async sendFullScreen(user: VMUser) {
|
private async sendFullScreen(user: VMUser) {
|
||||||
if (this.display == null) return;
|
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) => {
|
await user.SendMessage((encoder: Shared.MessageEncoder) => {
|
||||||
encoder.Init(8);
|
encoder.Init(8);
|
||||||
|
@ -336,7 +334,7 @@ class VirtualMachine extends EventEmitter {
|
||||||
});
|
});
|
||||||
|
|
||||||
await user.SendMessage((encoder: Shared.MessageEncoder) => {
|
await user.SendMessage((encoder: Shared.MessageEncoder) => {
|
||||||
encoder.Init(buffer.length + 256);
|
encoder.Init(buffer.length + 4);
|
||||||
encoder.SetDisplayRectMessage(0, 0, buffer);
|
encoder.SetDisplayRectMessage(0, 0, buffer);
|
||||||
return encoder.Finish();
|
return encoder.Finish();
|
||||||
});
|
});
|
||||||
|
|
|
@ -10,17 +10,9 @@ export enum MessageType {
|
||||||
DisplayRect,
|
DisplayRect,
|
||||||
DisplaySize, // display changed size
|
DisplaySize, // display changed size
|
||||||
|
|
||||||
// chat
|
|
||||||
Chat,
|
|
||||||
ChatHistory,
|
|
||||||
|
|
||||||
// user
|
// user
|
||||||
AddUser,
|
AddUser,
|
||||||
RemUser,
|
RemUser,
|
||||||
|
|
||||||
// currently aren't used
|
|
||||||
RenUser,
|
|
||||||
Rename,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum MouseButtons {
|
export enum MouseButtons {
|
||||||
|
@ -31,16 +23,8 @@ export enum MouseButtons {
|
||||||
WheelDn = 1 << 4
|
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 kMaxUserNameLength = 24;
|
||||||
export const kMaxChatMessageLength = 150;
|
|
||||||
|
|
||||||
/// This is a 16-bit value, sort of a structure if you will.
|
/// This is a 16-bit value, sort of a structure if you will.
|
||||||
/// 0x55 is the actual magic value.
|
/// 0x55 is the actual magic value.
|
||||||
|
@ -95,15 +79,6 @@ export type DisplaySizeMessage = DeserializedMessageRoot & {
|
||||||
height: number;
|
height: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ChatMessage = DeserializedMessageRoot & {
|
|
||||||
message: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type ChatServerMessage = DeserializedMessageRoot & ChatMessageObject;
|
|
||||||
|
|
||||||
export type ChatHistoryMessage = DeserializedMessageRoot & {
|
|
||||||
history: ChatMessageObject[];
|
|
||||||
};
|
|
||||||
|
|
||||||
export type AddUserMessage = DeserializedMessageRoot & {
|
export type AddUserMessage = DeserializedMessageRoot & {
|
||||||
user: string;
|
user: string;
|
||||||
|
@ -113,19 +88,6 @@ export type RemUserMessage = DeserializedMessageRoot & {
|
||||||
user: string;
|
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 =
|
export type AnyMessage =
|
||||||
| KeyMessage
|
| KeyMessage
|
||||||
|
@ -134,14 +96,7 @@ export type AnyMessage =
|
||||||
| TurnServerMessage
|
| TurnServerMessage
|
||||||
| DisplayRectMessage
|
| DisplayRectMessage
|
||||||
| DisplaySizeMessage
|
| DisplaySizeMessage
|
||||||
| ChatMessage
|
| RemUserMessage;
|
||||||
| ChatServerMessage
|
|
||||||
| ChatHistoryMessage
|
|
||||||
| AddUserMessage
|
|
||||||
| RemUserMessage
|
|
||||||
| RenUserMessage
|
|
||||||
| RenameMessage
|
|
||||||
| RenameServerMessage;
|
|
||||||
|
|
||||||
export type DeserializedMessage = AnyMessage;
|
export type DeserializedMessage = AnyMessage;
|
||||||
|
|
||||||
|
@ -193,21 +148,6 @@ export class MessageEncoder {
|
||||||
this.struct.WriteU16(h);
|
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) {
|
SetAddUserMessage(user: string) {
|
||||||
this.SetTypeCode(MessageType.AddUser);
|
this.SetTypeCode(MessageType.AddUser);
|
||||||
|
@ -219,21 +159,6 @@ export class MessageEncoder {
|
||||||
this.struct.WriteString(user);
|
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
|
// Setup some stuff and then return the final message
|
||||||
Finish() {
|
Finish() {
|
||||||
|
@ -257,24 +182,8 @@ export class MessageEncoder {
|
||||||
this.struct.WriteU16(type);
|
this.struct.WriteU16(type);
|
||||||
this.struct.Seek(oldOff);
|
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 {
|
export class MessageDecoder {
|
||||||
|
|
||||||
|
@ -289,8 +198,6 @@ export class MessageDecoder {
|
||||||
|
|
||||||
private static ReadMessageSync(buffer: ArrayBuffer, asClient: boolean, callback: (err: Error, message:DeserializedMessage) => void) {
|
private static ReadMessageSync(buffer: ArrayBuffer, asClient: boolean, callback: (err: Error, message:DeserializedMessage) => void) {
|
||||||
let struct = new Struct(buffer);
|
let struct = new Struct(buffer);
|
||||||
let context = new MessageDecoderExtContext();
|
|
||||||
context.struct = struct;
|
|
||||||
|
|
||||||
// Read and verify the header
|
// Read and verify the header
|
||||||
let header: ProtocolHeader = {
|
let header: ProtocolHeader = {
|
||||||
|
@ -357,26 +264,6 @@ export class MessageDecoder {
|
||||||
return callback(null, message);
|
return callback(null, message);
|
||||||
break;
|
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:
|
case MessageType.AddUser:
|
||||||
if (asClient) {
|
if (asClient) {
|
||||||
(message as AddUserMessage).user = struct.ReadString();
|
(message as AddUserMessage).user = struct.ReadString();
|
||||||
|
@ -394,25 +281,6 @@ export class MessageDecoder {
|
||||||
}
|
}
|
||||||
break;
|
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:
|
default:
|
||||||
return callback(new Error(`unknown type code ${header.type}`), null);
|
return callback(new Error(`unknown type code ${header.type}`), 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;
|
|
||||||
}
|
|
|
@ -1,2 +1 @@
|
||||||
export * from './Protocol.js';
|
export * from './Protocol.js';
|
||||||
export * from './UsernameValidator.js';
|
|
||||||
|
|
|
@ -25,7 +25,7 @@ function encodeMessage(f: (encoder: MessageEncoder) => void) {
|
||||||
console.log('[CLIENT TESTS]');
|
console.log('[CLIENT TESTS]');
|
||||||
await decodeMessage(
|
await decodeMessage(
|
||||||
encodeMessage((encoder) => {
|
encodeMessage((encoder) => {
|
||||||
encoder.SetChatMessage('Hi, CrustTest World!');
|
encoder.SetKeyMessage(100, true);
|
||||||
}),
|
}),
|
||||||
false
|
false
|
||||||
);
|
);
|
||||||
|
@ -44,7 +44,7 @@ function encodeMessage(f: (encoder: MessageEncoder) => void) {
|
||||||
// server->client tests
|
// server->client tests
|
||||||
await decodeMessage(
|
await decodeMessage(
|
||||||
encodeMessage((encoder) => {
|
encodeMessage((encoder) => {
|
||||||
encoder.SetChatSrvMessage('tester19200', 'Hi, CrustTest World!');
|
encoder.SetDisplaySizeMessage(1024, 1024);
|
||||||
}),
|
}),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in a new issue