-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1ab2419
commit afcaf38
Showing
4 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters