/tech/ - Tech

Technology


Name
X
Subject
Email
Message
Files
Password
[New Post]


I made a net socket server that runs on named IPC pipes, to expose a server only on a local server.
Server:
'use strict';
const net = require("net");
const path = require("path");

class IPCSocket {
    constructor(socket) {
        this.socket = socket;

        this.socket.on("end", ()=>this.endHandler());
        this.socket.on("error", (err)=>this.errorHandler(err));
        this.socket.on("data", (data)=>this.dataHandler(data));
        this.rooms = {};

        this.join("sys", this.systemHandler);
    }

    join(room, cb) { this.rooms[room] = cb; }

    leave(room) { delete this.rooms[room]; }

    send(room, data) { this.socket.write(JSON.stringify({room: room, data: data})); }

    systemHandler(data) {
        console.log("Received system message");
        console.log(data);
    }

    errorHandler(err) {
        console.log(err);
    }

    endHandler() {
        console.log("connection closed");
    }

    dataHandler(data) {
        try {
            data = JSON.parse(data);
            if(this.rooms[data.room]) this.rooms[data.room](data.data);
        } catch (err) {
            this.errorHandler(err);
        }
    }
}

module.exports = class IPCServer {
    constructor(pipeName) {
        this.server = net.createServer(this.socketHandler);

        this.server.on("error", (err)=>this.errorHandler(err));

        this.server.listen(path.join(
            "\\\\?\\pipe", process.cwd(), pipeName
        ), ()=>{
            console.log("[] IPC server is listening");
        });
    }

    errorHandler(err) {
        console.log(err);
    }

    socketHandler(socket) {
        new IPCSocket(socket);
    }
};
language: javascript, relevance: 68
Client:
'use strict';
const net = require("net");
const path = require("path");

module.exports = class IPCClient {
    constructor(pipeName) {
        this.client = net.createConnection(path.join(
            "\\\\?\\pipe", process.cwd(), pipeName
        ), ()=>{
            console.log("[] IPC socket is connected");
        });

        this.client.on("end", ()=>this.endHandler());
        this.client.on("error", (err)=>this.errorHandler(err));
        this.client.on("data", (data)=>this.dataHandler(data));

        this.rooms = {};

        this.join("sys", this.systemHandler);
    }

    systemHandler(data) {
        console.log("Received system message");
        console.log(data);
    }

    join(room, cb) { this.rooms[room] = cb; }

    leave(room) { delete this.rooms[room]; }

    send(room, data) { this.client.write(JSON.stringify({room: room, data: data})); }

    errorHandler(err) {
        console.log(err);
    }

    endHandler() {
        console.log("connection closed");
    }

    dataHandler(data) {
        try {
            data = JSON.parse(data);
            if(this.rooms[data.room]) this.rooms[data.room](data.data);
        } catch (err) {
            this.errorHandler(err);
        }
    }
};
language: javascript, relevance: 56
Replies: >>175
>>168 (OP) 
You can shorten a few lines and improve a few things. For example you dont need that anonymous function in the socket events. Pass the callback directly, so instead of:
this.socket.on("error", (err)=>this.errorHandler(err));
this.socket.on("data", (data)=>this.dataHandler(data));
language: csharp, relevance: 8you could have
this.socket.on("error", this.errorHandler);
this.socket.on("data", this.dataHandler);
language: csharp, relevance: 8They will perform the same.
The errorHandler seems to use console log, should change to console.error and the same principle as above would apply if you just want to use console.error as the callback for an error event.
Pardon a newfag, but what language is this? JavaScript, or C#?
Replies: >>182
>>181
Javascript. The language detection just has problems with short snippets.

Connecting...
Show Post Actions

Actions:

Staff Actions:

Captcha:

- rules - faq - source code -