implement TCP server, reimplement maxConnections except it now kicks the oldest connection
This commit is contained in:
55
cvmts/src/TCP/TCPClient.ts
Normal file
55
cvmts/src/TCP/TCPClient.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import EventEmitter from "events";
|
||||
import NetworkClient from "../NetworkClient.js";
|
||||
import { Socket } from "net";
|
||||
|
||||
export default class TCPClient extends EventEmitter implements NetworkClient {
|
||||
private socket: Socket;
|
||||
private cache: string;
|
||||
|
||||
constructor(socket: Socket) {
|
||||
super();
|
||||
this.socket = socket;
|
||||
this.cache = '';
|
||||
this.socket.on('end', () => {
|
||||
this.emit('disconnect');
|
||||
})
|
||||
this.socket.on('data', (data) => {
|
||||
var msg = data.toString('utf-8');
|
||||
if (msg[msg.length - 1] === '\n') msg = msg.slice(0, -1);
|
||||
this.cache += msg;
|
||||
this.readCache();
|
||||
});
|
||||
}
|
||||
|
||||
private readCache() {
|
||||
for (var index = this.cache.indexOf(';'); index !== -1; index = this.cache.indexOf(';')) {
|
||||
this.emit('msg', this.cache.slice(0, index + 1));
|
||||
this.cache = this.cache.slice(index + 1);
|
||||
}
|
||||
}
|
||||
|
||||
getIP(): string {
|
||||
return this.socket.remoteAddress!;
|
||||
}
|
||||
|
||||
send(msg: string): Promise<void> {
|
||||
return new Promise((res, rej) => {
|
||||
this.socket.write(msg, (err) => {
|
||||
if (err) {
|
||||
rej(err);
|
||||
return;
|
||||
}
|
||||
res();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
close(): void {
|
||||
this.emit('disconnect');
|
||||
this.socket.end();
|
||||
}
|
||||
|
||||
isOpen(): boolean {
|
||||
return this.socket.writable;
|
||||
}
|
||||
}
|
||||
39
cvmts/src/TCP/TCPServer.ts
Normal file
39
cvmts/src/TCP/TCPServer.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import EventEmitter from "events";
|
||||
import NetworkServer from "../NetworkServer.js";
|
||||
import { Server, Socket } from "net";
|
||||
import IConfig from "../IConfig.js";
|
||||
import { Logger } from "@cvmts/shared";
|
||||
import TCPClient from "./TCPClient.js";
|
||||
import { IPDataManager } from "../IPData.js";
|
||||
import { User } from "../User.js";
|
||||
|
||||
export default class TCPServer extends EventEmitter implements NetworkServer {
|
||||
listener: Server;
|
||||
Config: IConfig;
|
||||
logger: Logger;
|
||||
clients: TCPClient[];
|
||||
|
||||
constructor(config: IConfig) {
|
||||
super();
|
||||
this.logger = new Logger("CVMTS.TCPServer");
|
||||
this.Config = config;
|
||||
this.listener = new Server();
|
||||
this.clients = [];
|
||||
this.listener.on('connection', socket => this.onConnection(socket));
|
||||
}
|
||||
|
||||
private onConnection(socket: Socket) {
|
||||
var client = new TCPClient(socket);
|
||||
this.clients.push(client);
|
||||
this.emit('connect', new User(client, IPDataManager.GetIPData(client.getIP()), this.Config));
|
||||
}
|
||||
|
||||
start(): void {
|
||||
this.listener.listen(this.Config.tcp.port, this.Config.tcp.host, () => {
|
||||
this.logger.Info(`TCP server listening on ${this.Config.tcp.host}:${this.Config.tcp.port}`);
|
||||
})
|
||||
}
|
||||
stop(): void {
|
||||
this.listener.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user