Split out connection handling from client logic

This commit is contained in:
emerson 2022-05-18 12:09:35 -04:00
parent d18f6a699a
commit c9e6d6cbe5
3 changed files with 31 additions and 10 deletions

View file

@ -1,6 +1,6 @@
import { readFileSync } from 'fs';
import { createServer } from 'tls';
import { Client } from './lib/Client.js';
import { IRCClient } from './lib/IRCClient.js';
import { Server } from './lib/Server.js';
const config = JSON.parse(readFileSync(process.argv[2]));
@ -12,7 +12,7 @@ const listener = createServer({
});
listener.on('secureConnection', (c) => {
new Client(c, ircd);
new IRCClient(c, ircd);
})
listener.listen(config["port"], () => {

View file

@ -7,7 +7,7 @@ import { MatrixUser } from './MatrixUser.js';
import { IRCMessage, parseIRCMessage } from './Message.js';
import { Server } from './Server.js';
export class Client {
export abstract class Client {
capVersion: string
enabledCaps: Map<string, string>
allCaps: Map<string, string>
@ -15,7 +15,7 @@ export class Client {
isRegistered: boolean
apiCall: Axios
batchesInProgress: Map<string, Batch>
constructor(private socket: Socket, public server: Server) {
constructor(public server: Server) {
this.capVersion = '301';
this.enabledCaps = new Map();
this.allCaps = new Map([
@ -36,9 +36,6 @@ export class Client {
this.isRegistered = false;
this.apiCall = this.server.apiCall;
this.batchesInProgress = new Map();
this.server.doLog("New client connected");
this.socket.on('data', (data) => this.receiveData(data));
//this.socket.on('close', (e) => {if (this.user) this.user.handleClientClose(this, e)});
}
checkIfRegistered() {
@ -652,12 +649,15 @@ export class Client {
})
const newMsg = new IRCMessage(ourTags, prefix, command, params);
const msgToSend = newMsg.toString();
//console.log(`SENT: ${msgToSend}`);
this.socket.write(`${msgToSend}\r\n`);
this.writeMessage(`${msgToSend}\r\n`);
}
abstract writeMessage(message: string): void;
closeConnectionWithError(message: string) {
this.sendMessage(this.server.name, 'ERROR', [message], new Map());
this.socket.destroy();
this.closeConnection();
}
abstract closeConnection(): void;
}

21
src/IRCClient.ts Normal file
View file

@ -0,0 +1,21 @@
import { Socket } from "net";
import { Client } from "./Client.js";
import { Server } from './Server.js';
export class IRCClient extends Client {
constructor(private socket: Socket , public server: Server) {
super(server);
this.socket.on('data', (data) => this.receiveData(data));
//this.socket.on('close', (e) => {if (this.user) this.user.handleClientClose(this, e)});
this.server.doLog("New IRCClient connected");
}
writeMessage(message: string) {
//console.log(`SENT: ${msgToSend}`);
this.socket.write(message);
}
closeConnection() {
this.socket.destroy();
}
}