/t/ - Meta

Site Discussion


Name
X
Subject
Email
Message
Files
Password
[New Post]


Scuffed code

'use strict';
const net = require("net");
const path = require("path");
const uuid = require("uuid/v4");

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

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

        this.rooms = {};

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

        this.messageQueue = [];
        this.onconnect = null;
        this.sendDelay = 50;
        this.flushDelay = 50;
        this.flushInProgress = false;

        setInterval(()=>this.flushQueue(), this.flushDelay);
    }

    connected(cb){ this.onconnect = cb; }

    async flushQueue() {
        if(this.flushInProgress) return;
        this.flushInProgress = true;
        while(this.messageQueue.length != 0) {
            this.client.write(this.messageQueue.shift());
            await new Promise((res, rej)=>setTimeout(res, this.sendDelay));
        }
        this.flushInProgress = false;
    }

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

        return false;
    }

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

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

    send(room, data) {
        this.messageQueue.push(JSON.stringify({room: room, data: data}));
    }

    sendAndWait(room, data) {
        return new Promise((res, rej)=>{
            const target = uuid();

            this.join(target, data=>{
                res(data);
                this.leave(target);
            });

            this.send(room, {
                return_room: target,
                content: data
            });
        });
    }

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

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

    dataHandler(data) {
        try {
            data = JSON.parse(data);
            if(this.rooms[data.room]) {
                const result = this.rooms[data.room](data.data);

                if(result != null && data.data.return_room) this.send(data.data.return_room, result);
                //else if (result != null) this.send(data.room, result);
            }
        } catch (err) {
            this.errorHandler(err);
        }
    }
};
language: kotlin, relevance: 146

Connecting...
Show Post Actions

Actions:

Staff Actions:

Captcha:

- rules - faq - source code -