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', (buf: Buffer, binary: boolean) => {
|
||||||
user.socket.on('msg', (msg: string) => {
|
|
||||||
let buf = Buffer.from(msg);
|
|
||||||
try {
|
try {
|
||||||
user.protocol.processMessage(buf);
|
user.protocol.processMessage(buf);
|
||||||
} catch (err) {
|
} 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;
|
getIP(): string;
|
||||||
send(msg: string): Promise<void>;
|
send(msg: string): Promise<void>;
|
||||||
sendBinary(msg: Uint8Array): Promise<void>;
|
sendBinary(msg: Uint8Array): Promise<void>;
|
||||||
close(): void;
|
close(): void;
|
||||||
on(event: string, listener: (...args: any[]) => void): void;
|
|
||||||
off(event: string, listener: (...args: any[]) => void): void;
|
|
||||||
isOpen(): boolean;
|
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;
|
start(): void;
|
||||||
stop(): 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 EventEmitter from 'events';
|
||||||
import NetworkClient from '../NetworkClient.js';
|
import { NetworkClient } from '../NetworkClient.js';
|
||||||
import { Socket } from 'net';
|
import { Socket } from 'net';
|
||||||
|
|
||||||
const TextHeader = 0;
|
const TextHeader = 0;
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
// struct msg { beu32 len; char data[len] }
|
// struct msg { beu32 len; char data[len] }
|
||||||
// (along with a length cap obviously)
|
// (along with a length cap obviously)
|
||||||
import EventEmitter from 'events';
|
import EventEmitter from 'events';
|
||||||
import NetworkServer from '../NetworkServer.js';
|
import { NetworkServer } from '../NetworkServer.js';
|
||||||
import { Server, Socket } from 'net';
|
import { Server, Socket } from 'net';
|
||||||
import IConfig from '../IConfig.js';
|
import IConfig from '../IConfig.js';
|
||||||
import TCPClient from './TCPClient.js';
|
import TCPClient from './TCPClient.js';
|
||||||
|
|||||||
@@ -1,23 +1,25 @@
|
|||||||
import { WebSocket } from 'ws';
|
import { WebSocket } from 'ws';
|
||||||
import NetworkClient from '../NetworkClient.js';
|
import { NetworkClient } from '../NetworkClient.js';
|
||||||
import EventEmitter from 'events';
|
import EventEmitter from 'events';
|
||||||
|
|
||||||
export default class WSClient extends EventEmitter implements NetworkClient {
|
export default class WSClient extends EventEmitter implements NetworkClient {
|
||||||
socket: WebSocket;
|
socket: WebSocket;
|
||||||
ip: string;
|
ip: string;
|
||||||
|
enforceTextOnly = true
|
||||||
|
|
||||||
constructor(ws: WebSocket, ip: string) {
|
constructor(ws: WebSocket, ip: string) {
|
||||||
super();
|
super();
|
||||||
this.socket = ws;
|
this.socket = ws;
|
||||||
this.ip = ip;
|
this.ip = ip;
|
||||||
this.socket.on('message', (buf: Buffer, isBinary: boolean) => {
|
this.socket.on('message', (buf: Buffer, isBinary: boolean) => {
|
||||||
// Close the user's connection if they send a non-string message
|
// Close the user's connection if they send a binary message
|
||||||
if (isBinary) {
|
// when we are not expecting them yet.
|
||||||
|
if (isBinary && this.enforceTextOnly) {
|
||||||
this.close();
|
this.close();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.emit('msg', buf.toString('utf-8'));
|
this.emit('msg', buf, isBinary);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.socket.on('close', () => {
|
this.socket.on('close', () => {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import * as http from 'http';
|
import * as http from 'http';
|
||||||
import NetworkServer from '../NetworkServer.js';
|
import { NetworkServer } from '../NetworkServer.js';
|
||||||
import EventEmitter from 'events';
|
import EventEmitter from 'events';
|
||||||
import { WebSocketServer, WebSocket } from 'ws';
|
import { WebSocketServer, WebSocket } from 'ws';
|
||||||
import internal from 'stream';
|
import internal from 'stream';
|
||||||
|
|||||||
Reference in New Issue
Block a user