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,SAAU,EAAI,EAAS,GAC3C,GAAI,EAAG,UAAY,eAEf,KAAK,WAAW,KAAK,EAAI,EAAS,OAEjC,CACD,MAAM,EAAS,IAAI,iBAAiB,CAChC,KAAM,EAAG,KACT,KAAM,EAAG,UACV,KACC,IAAI,EAAa,IAAI,OAAO,GAC5B,EAAW,cAAc,KAAK,MAAO,GACrC,EAAO,IAAI,OAAO,OAAO,CAAC,EAAS,GAAa,EAAQ,OAAS,EAAW,QAAQ,IAExF,EAAO,GAAG,OAAO,KACb,GAAU,IAEd,EAAO,GAAG,SAAU,IAChB,EAAS,EAAI,GAErB,CACJ,EAKA,OAAO,UAAU,KAAO,WACpB,KAAK,WAAW,QAChB,KAAK,WAAW,MACpB,EAKA,OAAO,UAAU,MAAQ,WACrB,KAAK,WAAW,QAChB,KAAK,WAAW,OACpB,EAMA,OAAO,UAAU,oBAAsB,WACnC,OAAO,KAAK,WAAW,qBAC3B,EAEA,IAAI,iBAAmB,SAAU,EAAU,GACvC,OAAO,EAAS,UAAY,EAAS,OACzC,EAMA,OAAO,UAAU,WAAa,WAC1B,OAAO,KAAK,WAAW,YAC3B,EASA,OAAO,UAAU,iBAAmB,iBAQpC,QAAQ,aAAe,aAQvB,QAAQ,iBAAmB","file":"/npm/rainfall-tcp@0.1.1/driver.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/rainfall-tcp@0.1.1/driver.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","/*jshint esversion: 6 */\n\nvar net = require('net');\nvar udp = require('rainfall-udp');\n\nconst BROADCAST_ADDR = \"255.255.255.255\";\n\n\n/** @module tcp */\n\n/**\n    @typedef initParams\n    @property {Boolean} [udplisten] - (Optional) True if you want to listen for broadcast\n    messages and false (or undefined) otherwise\n    @property {Number} [rport] - (Optional) The port listened by the TCP (and UDP if you are\n    listening for broadcasts). If undefined, server will listen on arbitrary port.\n    @property {Number} [broadcast_port] - (Optional) The port used when creating a broadcast address. If undefined, a default value (2357) will be used\n*/\n\n/**\n * Callback used by Driver.\n * @callback onInitialized\n * @param {Error} error - If there is a problem initializing this will be an Error object, otherwise will be null\n * @param {module:tcp~Driver} driver - The driver object\n */\n\n/**\n * Callback used by listen.\n * @callback onMessage\n * @param {Buffer} message - Buffer containing the buffer received from the network\n * @param {module:tcp~address} from - Address of the transmitter\n */\n\n/**\n * Callback used by listen.\n * @callback onListening\n * @param {Error} error - If there is a problem listening this will be an Error object, otherwise will be null\n */\n\n/**\n * Callback used by send.\n * @callback onSent\n * @param {Error} error - If there is a problem sending this will be an Error object, otherwise will be null\n */\n\n/**\n    @typedef {Object} address\n    @property {String} address - The IP (v4 or v6) address\n    @property {Number} port - The TCP port\n    @property {String} family - (Optional) The IP version (can be IPv4 or IPv6)\n */\n\n\n /**\n    @class Driver\n **/\nfunction Driver (params, callback) {\n    this._port = params.rport;\n    this._msgCallback = null;\n    this._udpListen = params.udplisten;\n\n    //TCP server to listen\n    this._tcpServer = net.createServer();\n    this._tcpServer.on('error', (err) => {\n        if (this._lastCallback) {\n            this._lastCallback(err);\n            this._lastCallback = null;\n        }\n    });\n    this._tcpServer.on('connection', (socket) => {\n        var buf = null;\n        var error = null;\n        var address = {\n            address: socket.remoteAddress,\n            family: socket.remoteFamily\n        };\n        socket.on('data', (data) => {\n            if (buf === null)\n                buf = data;\n            else\n                buf = Buffer.concat([buf, data], buf.length + data.length);\n        });\n        socket.on('end', () => {\n            if (this._msgCallback && buf !== null) {\n                if (!error) {\n                    address.port = buf.readUInt32BE(buf.length - 4);\n                    this._msgCallback(buf.slice(0, buf.length - 4), address);\n                }\n            }\n        });\n        socket.on('error', (err) => {\n            error = err;\n        });\n    });\n    //Creates UDP for broadcast\n    udp.createDriver(params, (err, udpInstance) => {\n        this._udpDriver = udpInstance;\n        this._port = udpInstance.getAddress().port;\n        if (err)\n            callback(err);\n        else if (callback)\n            callback(null, this);\n    });\n}\n\nfunction createDriver(params, callback) {\n    new Driver(params, callback);\n}\n\n/**\n    Opens an TCP socket listening the port and address specified in the rport parameter.\n    Also does that for UDP if you want to listen for broadcast messages.\n    @param {module:tcp~onMessage} msgCallback - Function to be called when a message arrives\n    @param {module:tcp~onListening} [listenCallback] - Function to be called when the driver is\n    listening, if an error occurred\n*/\nDriver.prototype.listen = function (msgCallback, listenCallback) {\n    this._lastCallback = listenCallback;\n    this._msgCallback = msgCallback;\n    this._tcpServer.listen({\n        port: this._port\n    }, () => {\n        this._lastCallback  = null;\n        if (this._udpListen) {\n            this._udpDriver.listen(msgCallback, listenCallback);\n        }\n        else if (listenCallback) listenCallback();\n    });\n};\n\n/**\n    Sends an package using TCP. If the address is the broadcast address, sends the\n    package using UDP\n    @param {module:tcp~address} to - Object containing the address object of the recipient\n    @param {Buffer} message - Buffer containing the message to be sent\n    @param {module:tcp~onSent} [callback] - Function to be called when the message was sent\n\n*/\nDriver.prototype.send = function (to, message, callback) {\n    if (to.address === BROADCAST_ADDR) {\n        //Send using udp\n        this._udpDriver.send(to, message, callback);\n    }\n    else {\n        const client = net.createConnection({\n            port: to.port,\n            host: to.address,\n        }, () => {\n            var portBuffer = new Buffer(4);\n            portBuffer.writeUInt32BE(this._port, 0);\n            client.end(Buffer.concat([message, portBuffer], message.length + portBuffer.length));\n        });\n        client.on('end', () => {\n            callback();\n        });\n        client.on('error', (err) => {\n            callback(err);\n        });\n    }\n};\n\n/**\n    Stops listening for messages. But, if you start listening again, this instance must work\n*/\nDriver.prototype.stop = function () {\n    this._tcpServer.close();\n    this._udpDriver.stop();\n};\n\n/**\n    Stops the UDP server, if listen() was called. You can still use the driver to listen and send\n*/\nDriver.prototype.close = function () {\n    this._tcpServer.close();\n    this._udpDriver.close();\n};\n\n/**\n    Gets the broadcast network address.\n    @returns {module:tcp~address}  Broadcast network address\n*/\nDriver.prototype.getBroadcastAddress = function () {\n    return this._udpDriver.getBroadcastAddress();\n};\n\nvar compareAddresses = function (address1, address2) {\n    return address1.address === address2.address;\n};\n\n/**\n    Gets the driver network address. Only works when \"listening\" was called beforehands\n    @returns {module:tcp~address} Network address\n*/\nDriver.prototype.getAddress = function() {\n    return this._udpDriver.getAddress();\n};\n\n\n/**\n    Compares two addresses (it is the same compareAddresses exported function)\n    @param {module:tcp~address} a1 - First address to compare\n    @param {module:tcp~address} a2 - Second address to compare\n    @returns {boolean} true if address1 and adress2 are the same and false otherwise\n*/\nDriver.prototype.compareAddresses = compareAddresses;\n\n/**\n    Creates a driver for TCP socket. It doesn't bind the port. But binds this port to the UDP.\n    @param {module:tcp~initParams} params - Parameters to initialize the tcp driver\n    @param {module:tcp~onInitialized} [callback] - Function to be called when the driver is initialized\n\n*/\nexports.createDriver = createDriver;\n\n/**\n    Compares two addresses\n    @param {module:tcp~address} a1 - First address to compare\n    @param {module:tcp~address} a2 - Second address to compare\n    @returns {boolean} true if address1 and adress2 are the same and false otherwise\n*/\nexports.compareAddresses = compareAddresses;\n"]}