@holesail/hyper-cmd-lib-net
Advanced tools
Comparing version 0.1.1 to 0.2.0
110
index.js
@@ -0,3 +1,4 @@ | ||
const dgram = require('bare-dgram') | ||
const EventEmitter = require('events') | ||
function connPiper (connection, _dst, opts = {}, stats = {}) { | ||
@@ -30,9 +31,9 @@ const loc = _dst() | ||
loc.on('data', d => { | ||
connection.write(d) | ||
}) | ||
loc.on('data', d => { | ||
connection.write(d) | ||
}) | ||
connection.on('data', d => { | ||
loc.write(d) | ||
}) | ||
connection.on('data', d => { | ||
loc.write(d) | ||
}) | ||
@@ -78,52 +79,93 @@ loc.on('error', destroy).on('close', destroy) | ||
function connRemoteCtrl (connection, opts = {}, stats = {}) { | ||
if (!stats.remCnt) { | ||
stats.remCnt = 0 | ||
class udpSocket { | ||
constructor (opts) { | ||
this.opts = opts | ||
this.server = dgram.createSocket('udp4') | ||
this.client = dgram.createSocket('udp4') | ||
this.event = new EventEmitter() | ||
this.rinfo = null | ||
this.connect() | ||
} | ||
stats.remCnt++ | ||
connect () { | ||
if (this.opts.bind) { | ||
this.server.bind(this.opts.port, this.opts.host) | ||
} | ||
let destroyed = false | ||
this.server.on('message', (msg, rinfo) => { | ||
this.event.emit('message', msg, rinfo) | ||
this.rinfo = rinfo | ||
}) | ||
connection.on('error', destroy) | ||
connection.on('close', destroy) | ||
this.client.on('message', (response, rinfo) => { | ||
this.event.emit('message', response) | ||
}) | ||
connection.on('error', err => { | ||
if (opts.debug) { | ||
console.error(err) | ||
} | ||
}) | ||
this.client.on('error', (err) => { | ||
console.error(`UDP error: \n${err.stack}`) | ||
this.client.close() | ||
}) | ||
} | ||
function destroy (err) { | ||
if (destroyed) { | ||
return | ||
write (msg) { | ||
if (this.rinfo) { | ||
this.server.send(msg, 0, msg.length, this.rinfo.port, this.rinfo.address) | ||
} else { | ||
this.client.send(msg, 0, msg.length, this.opts.port, this.opts.host) | ||
} | ||
} | ||
} | ||
stats.remCnt-- | ||
class udpConnPiper { | ||
constructor (connection, _dst, opts = {}, stats = {}) { | ||
this.loc = _dst() | ||
this.connection = connection | ||
destroyed = true | ||
this.stats = { | ||
rejectCnt: 0, | ||
locCnt: 0, | ||
remCnt: 0 | ||
} | ||
connection.destroy(err) | ||
this.destroyed = false | ||
if (opts.onDestroy) { | ||
opts.onDestroy(err) | ||
} | ||
// Connect | ||
this.connect() | ||
} | ||
function send (data) { | ||
if (destroyed) { | ||
connect () { | ||
if (this.loc === null) { | ||
this.connection.destroy() | ||
return | ||
} | ||
connection.write(data) | ||
this.loc.event.on('message', (msg, rinfo) => { | ||
this.connection.trySend(msg) | ||
}) | ||
this.connection.on('message', (msg) => { | ||
this.loc.write(msg) | ||
}) | ||
this.loc.server.on('error', this.destroy).on('close', this.destroy) | ||
this.connection.on('error', this.destroy).on('close', this.destroy) | ||
} | ||
return { | ||
send | ||
destroy (err) { | ||
// TODO: Add destroy functionality | ||
} | ||
} | ||
function udpConnect (opts) { | ||
return new udpSocket(opts) | ||
} | ||
function udpPiper (connection, _dst, opts = {}, stats = {}) { | ||
return new udpConnPiper(connection, _dst, opts = {}, stats = {}) | ||
} | ||
module.exports = { | ||
connPiper, | ||
connRemoteCtrl | ||
udpPiper, | ||
udpConnect | ||
} |
{ | ||
"name": "@holesail/hyper-cmd-lib-net", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "HyperCmd Net lib", | ||
"main": "index.js", | ||
"dependencies": {}, | ||
"devDependencies": {}, | ||
"repository": { | ||
@@ -17,3 +15,9 @@ "type": "git", | ||
}, | ||
"homepage": "https://github.com/holesail/hyper-cmd-lib-net/s" | ||
"homepage": "https://github.com/holesail/hyper-cmd-lib-net/s", | ||
"devDependencies": { | ||
"standard": "^17.1.2" | ||
}, | ||
"dependencies": { | ||
"bare-dgram": "^1.0.1" | ||
} | ||
} |
15914
131
1
1
5
+ Addedbare-dgram@^1.0.1
+ Addedb4a@1.6.7(transitive)
+ Addedbare-dgram@1.0.1(transitive)
+ Addedbare-events@2.5.4(transitive)
+ Addedfast-fifo@1.3.2(transitive)
+ Addednode-gyp-build@4.8.4(transitive)
+ Addedqueue-tick@1.0.1(transitive)
+ Addedstreamx@2.21.1(transitive)
+ Addedtext-decoder@1.2.3(transitive)
+ Addedudx-native@1.17.3(transitive)