Browse Source

allow changing socket when replying

session-estimator
Mathias Buus 3 years ago
parent
commit
fc9bfca4b6
  1. 10
      index.js
  2. 20
      lib/io.js

10
index.js

@ -307,7 +307,7 @@ class DHT extends EventEmitter {
// standard keep alive call
if (req.command === 'ping') {
req.sendReply(0, null, false, false, req.from)
req.sendReply(0, null, false, false)
return
}
@ -317,14 +317,14 @@ class DHT extends EventEmitter {
const port = c.uint16.decode({ start: 0, end: 2, buffer: req.value })
if (port === 0) return
req.from.port = port
req.sendReply(0, null, false, false, req.from)
req.sendReply(0, null, false, false)
return
}
// empty dht reply back
if (req.command === 'find_node') {
if (!req.target) return
req.sendReply(0, null, false, true, req.from)
req.sendReply(0, null, false, true)
return
}
@ -338,13 +338,13 @@ class DHT extends EventEmitter {
this._check(node)
}
}
req.sendReply(0, null, false, false, req.from)
req.sendReply(0, null, false, false)
return
}
// ask the user to handle it or reply back with a bad command
if (this.onrequest(req) === false) {
req.sendReply(UNKNOWN_COMMAND, null, false, true, req.from)
req.sendReply(UNKNOWN_COMMAND, null, false, req.target !== null)
}
}

20
lib/io.js

@ -235,11 +235,15 @@ class Request {
}
reply (value, opts = {}) {
this.sendReply(0, value || null, opts.token !== false, this.target !== null && opts.closerNodes !== false, opts.to || this.from)
const socket = opts.socket || this.socket
const to = opts.to || this.from
this._sendReply(0, value || null, opts.token !== false, this.target !== null && opts.closerNodes !== false, to, socket)
}
error (code, opts = {}) {
this.sendReply(code, null, opts.token === true, this.target !== null && opts.closerNodes !== false, opts.to || this.from)
const socket = opts.socket || this.socket
const to = opts.to || this.from
this._sendReply(code, null, opts.token === true, this.target !== null && opts.closerNodes !== false, to, socket)
}
relay (value, to) {
@ -261,6 +265,10 @@ class Request {
this._sendNow()
}
sendReply (error, value, token, hasCloserNodes) {
this._sendReply(error, value, token, hasCloserNodes, this.from, this.socket)
}
_sendNow () {
if (this.destroyed) return
this.sent++
@ -283,10 +291,10 @@ class Request {
this.onerror(err || DESTROY, this)
}
sendReply (error, value, token, hasCloserNodes, from) {
if (this.socket === null || this.destroyed) return
_sendReply (error, value, token, hasCloserNodes, from, socket) {
if (socket === null || this.destroyed) return
const id = this._io.ephemeral === false && this.socket === this._io.serverSocket
const id = this._io.ephemeral === false && socket === this._io.serverSocket
const closerNodes = hasCloserNodes ? this._io.table.closest(this.target) : EMPTY_ARRAY
const state = { start: 0, end: 1 + 1 + 6 + 2, buffer: null } // (type | version) + flags + to + tid
@ -309,7 +317,7 @@ class Request {
if (error > 0) c.uint.encode(state, error)
if (value) c.buffer.encode(state, value)
this.socket.send(state.buffer, 0, state.buffer.byteLength, from.port, from.host)
socket.send(state.buffer, 0, state.buffer.byteLength, from.port, from.host)
}
_encodeRequest (token, value) {

Loading…
Cancel
Save