Skip to content

Commit

Permalink
feat: initial DumbClient impl
Browse files Browse the repository at this point in the history
  • Loading branch information
thetayloredman committed Feb 21, 2023
1 parent 1ab2419 commit afcaf38
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/DumbClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* node-subdata-2 - SubData 2 wrapper for Node.js
* Copyright (C) 2022 LogN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import SafeEventEmitter from "./lib/SafeEventEmitter";
import Packet from "./Packet";
import type Shell from "./shell";
import { ShellEvents } from "./shell";

/** A list of possible events for a {@link DumbClient} */
export enum DumbClientEvents {
/** A new packet is received */
Packet = "packet",
/** The connection is closing */
Close = "close"
}

export type DumbClientEventArguments = {
[DumbClientEvents.Packet]: [Packet];
[DumbClientEvents.Close]: [];
};

/**
* Represents a SubData 2 client that does not perform any protocol
* handshake or states, and leaves it up to the user. You probably
* don't want to use this.
*/
export default class DumbClient extends SafeEventEmitter<DumbClientEventArguments> {
private _shell: Shell;

/**
* Create a new DumbClient.
* @param shell The {@link Shell} to use
*/
public constructor(shell: Shell) {
super();
this._shell = shell;
this._shell.on(ShellEvents.Close, () => {
this.emit(DumbClientEvents.Close);
});
this._shell.on(ShellEvents.Packet, (_size, data) => {
this.emit(DumbClientEvents.Packet, new Packet(data));
});
}

/**
* Terminate the TCP level of the connection. This does not send the disconnect packets.
*/
public close() {
this._shell.close();
}

/** Send a {@link Packet} over the stream. */
public send(data: Packet) {
this._shell.send(data.toRaw());
}
}
44 changes: 44 additions & 0 deletions src/Packet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* node-subdata-2 - SubData 2 wrapper for Node.js
* Copyright (C) 2022 LogN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

/** Describes a SubData 2 packet. */
export default class Packet {
/** This packet's identification number */
public id: number;
/** The rest of the packet's InputStream */
public data: Buffer;

/**
* Create a new Packet.
* @param raw The raw packet data including the ID as the first two bytes
*/
public constructor(raw: Buffer) {
this.id = parseInt(raw.subarray(0, 2).toString("hex"), 16);
this.data = raw.subarray(2);
}

/** Convert this Packet to raw network data */
public toRaw() {
return Buffer.concat([Buffer.from([this.id >> 8, this.id & 0xff]), this.data]);
}

/** Make a new Packet given an ID and data. */
public static fromID(id: number, data: Buffer) {
return new Packet(Buffer.concat([Buffer.from([id >> 8, id & 0xff]), data]));
}
}
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import DumbClient, { DumbClientEventArguments, DumbClientEvents } from "./DumbClient";
import SafeEventEmitter from "./lib/SafeEventEmitter.js";
import { bytes, kb, mb } from "./lib/sizeHelpers.js";
import Shell, { ShellEventArguments, ShellEvents } from "./shell/";
Expand All @@ -34,6 +35,9 @@ export {
DirectStream,
DirectStreamEventArguments,
DirectStreamEvents,
DumbClient,
DumbClientEventArguments,
DumbClientEvents,
IOProvider,
IOProviderEventArguments,
IOProviderEvents,
Expand Down
5 changes: 5 additions & 0 deletions src/shell/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,9 @@ export default class Shell extends SafeEventEmitter<ShellEventArguments> {
public send(data: Buffer): void {
this._stream.writePacket(this._algorithm.encode(data));
}

/** Close the underlying connection. */
public close(): void {
this._stream.close();
}
}

0 comments on commit afcaf38

Please sign in to comment.