refactor networking
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import dgram, { RemoteInfo } from 'dgram';
|
||||
import { UdpSocketCommunicator } from "../socket_communicator/udp_socket_communicator";
|
||||
import { OperationHandler } from "../operations_base/operation_handler";
|
||||
import { GeneralOperations} from "../operations_custom/general_operations";
|
||||
import { GeneralOperations } from "../operations_custom/general_operations";
|
||||
|
||||
export class UdpServer {
|
||||
private readonly udpServer: dgram.Socket;
|
||||
@@ -19,10 +19,20 @@ export class UdpServer {
|
||||
this.operationHandler.loadPlugin(new GeneralOperations());
|
||||
}
|
||||
|
||||
// Unified logging function
|
||||
private log(message: string, level: 'log' | 'error' = 'log'): void {
|
||||
const prefix = '[UdpServer]';
|
||||
if (level === 'error') {
|
||||
console.error(`${prefix} ${message}`);
|
||||
} else {
|
||||
console.log(`${prefix} ${message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Start the UDP server
|
||||
public start(): void {
|
||||
this.udpServer.on('message', this.handleUdpMessages.bind(this));
|
||||
this.udpServer.on('error', this.handleError);
|
||||
this.udpServer.on('error', this.handleError.bind(this));
|
||||
this.udpServer.on('listening', this.handleListening.bind(this));
|
||||
|
||||
// Bind the server to the UDP port and host
|
||||
@@ -34,7 +44,7 @@ export class UdpServer {
|
||||
const ip = rinfo.address;
|
||||
const port = rinfo.port;
|
||||
|
||||
//console.log(`Received message from ${ip}:${port}`);
|
||||
this.log(`Received message from ${ip}:${port}`);
|
||||
|
||||
// Create a temporary communicator for the incoming message
|
||||
const communicator = new UdpSocketCommunicator(this.udpServer, ip, port, this.operationHandler);
|
||||
@@ -45,18 +55,23 @@ export class UdpServer {
|
||||
const communicatorResult = communicator.getHandlerResult();
|
||||
if (communicatorResult) {
|
||||
// Send response back to the client using the temporary communicator
|
||||
await communicator.sendMessage(communicatorResult.operationCode, communicatorResult.metaInfo);
|
||||
try {
|
||||
await communicator.sendMessage(communicatorResult.operationCode, communicatorResult.metaInfo);
|
||||
this.log(`Sent response to ${ip}:${port}`);
|
||||
} catch (error: any) {
|
||||
this.log(`Error sending response to ${ip}:${port}: ${error.message}`, 'error');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Handle UDP server errors
|
||||
private handleError(err: Error): void {
|
||||
console.error(`UDP server error:\n${err.stack}`);
|
||||
this.log(`UDP server error:\n${err.stack}`, 'error');
|
||||
}
|
||||
|
||||
// Handle when the UDP server starts listening
|
||||
private handleListening(): void {
|
||||
const address = this.udpServer.address();
|
||||
console.log(`UDP server listening on ${address.address}:${address.port}`);
|
||||
this.log(`UDP server listening on ${address.address}:${address.port}`);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user