make network abstraction pass bare buffer
this bitrots tcp a bit. once the tcp protocol is replaced with a message based one it shouild be fine
This commit is contained in:
@@ -173,9 +173,7 @@ export default class CollabVMServer implements IProtocolHandlers {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: we should probably just make this a buffer arg lol..
|
||||
user.socket.on('msg', (msg: string) => {
|
||||
let buf = Buffer.from(msg);
|
||||
user.socket.on('msg', (buf: Buffer, binary: boolean) => {
|
||||
try {
|
||||
user.protocol.processMessage(buf);
|
||||
} catch (err) {
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
export default interface NetworkClient {
|
||||
import { EventEmitter } from "stream";
|
||||
|
||||
interface NetworkClientEvents extends EventEmitter {
|
||||
on(event: 'msg', listener: (buf: Buffer, binary: boolean) => void): this;
|
||||
on(event: 'disconnect', listener: () => void): this;
|
||||
}
|
||||
|
||||
export interface NetworkClient extends NetworkClientEvents {
|
||||
getIP(): string;
|
||||
send(msg: string): Promise<void>;
|
||||
sendBinary(msg: Uint8Array): Promise<void>;
|
||||
close(): void;
|
||||
on(event: string, listener: (...args: any[]) => void): void;
|
||||
off(event: string, listener: (...args: any[]) => void): void;
|
||||
isOpen(): boolean;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
export default interface NetworkServer {
|
||||
import { EventEmitter } from "stream";
|
||||
import { User } from "./User";
|
||||
|
||||
interface NetworkServerEvents extends EventEmitter {
|
||||
on(event: 'connect', listener: (user: User) => void): this;
|
||||
}
|
||||
|
||||
export interface NetworkServer extends NetworkServerEvents {
|
||||
start(): void;
|
||||
stop(): void;
|
||||
on(event: string, listener: (...args: any[]) => void): void;
|
||||
off(event: string, listener: (...args: any[]) => void): void;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import EventEmitter from 'events';
|
||||
import NetworkClient from '../NetworkClient.js';
|
||||
import { NetworkClient } from '../NetworkClient.js';
|
||||
import { Socket } from 'net';
|
||||
|
||||
const TextHeader = 0;
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// struct msg { beu32 len; char data[len] }
|
||||
// (along with a length cap obviously)
|
||||
import EventEmitter from 'events';
|
||||
import NetworkServer from '../NetworkServer.js';
|
||||
import { NetworkServer } from '../NetworkServer.js';
|
||||
import { Server, Socket } from 'net';
|
||||
import IConfig from '../IConfig.js';
|
||||
import TCPClient from './TCPClient.js';
|
||||
|
||||
@@ -1,23 +1,25 @@
|
||||
import { WebSocket } from 'ws';
|
||||
import NetworkClient from '../NetworkClient.js';
|
||||
import { NetworkClient } from '../NetworkClient.js';
|
||||
import EventEmitter from 'events';
|
||||
|
||||
export default class WSClient extends EventEmitter implements NetworkClient {
|
||||
socket: WebSocket;
|
||||
ip: string;
|
||||
enforceTextOnly = true
|
||||
|
||||
constructor(ws: WebSocket, ip: string) {
|
||||
super();
|
||||
this.socket = ws;
|
||||
this.ip = ip;
|
||||
this.socket.on('message', (buf: Buffer, isBinary: boolean) => {
|
||||
// Close the user's connection if they send a non-string message
|
||||
if (isBinary) {
|
||||
// Close the user's connection if they send a binary message
|
||||
// when we are not expecting them yet.
|
||||
if (isBinary && this.enforceTextOnly) {
|
||||
this.close();
|
||||
return;
|
||||
}
|
||||
|
||||
this.emit('msg', buf.toString('utf-8'));
|
||||
this.emit('msg', buf, isBinary);
|
||||
});
|
||||
|
||||
this.socket.on('close', () => {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import * as http from 'http';
|
||||
import NetworkServer from '../NetworkServer.js';
|
||||
import { NetworkServer } from '../NetworkServer.js';
|
||||
import EventEmitter from 'events';
|
||||
import { WebSocketServer, WebSocket } from 'ws';
|
||||
import internal from 'stream';
|
||||
|
||||
Reference in New Issue
Block a user