|
@ -235,18 +235,23 @@ class Request { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
reply (value, opts = {}) { |
|
|
reply (value, opts = {}) { |
|
|
this.sendReply(0, value || null, opts.token !== false, this.target !== null && opts.closerNodes !== false) |
|
|
this.sendReply(0, value || null, opts.token !== false, this.target !== null && opts.closerNodes !== false, opts.to || this.from) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
error (code, opts = {}) { |
|
|
error (code, opts = {}) { |
|
|
this.sendReply(code, null, opts.token === true, this.target !== null && opts.closerNodes !== false) |
|
|
this.sendReply(code, null, opts.token === true, this.target !== null && opts.closerNodes !== false, opts.to || this.from) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
relay (value, to) { |
|
|
|
|
|
const buffer = this._encodeRequest(null, value) |
|
|
|
|
|
this.socket.send(buffer, 0, buffer.byteLength, to.port, to.host) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
send (force = false) { |
|
|
send (force = false) { |
|
|
if (this.destroyed) return |
|
|
if (this.destroyed) return |
|
|
|
|
|
|
|
|
if (this.socket === null) return |
|
|
if (this.socket === null) return |
|
|
if (this._buffer === null) this._buffer = this._encodeRequest() |
|
|
if (this._buffer === null) this._buffer = this._encodeRequest(this.token, this.value) |
|
|
|
|
|
|
|
|
if (!force && this._io.congestion.isFull()) { |
|
|
if (!force && this._io.congestion.isFull()) { |
|
|
this._io._pending.push(this) |
|
|
this._io._pending.push(this) |
|
@ -278,7 +283,7 @@ class Request { |
|
|
this.onerror(err || DESTROY, this) |
|
|
this.onerror(err || DESTROY, this) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
sendReply (error, value, token, hasCloserNodes) { |
|
|
sendReply (error, value, token, hasCloserNodes, from) { |
|
|
if (this.socket === null || this.destroyed) return |
|
|
if (this.socket === null || this.destroyed) return |
|
|
|
|
|
|
|
|
const id = this._io.ephemeral === false && this.socket === this._io.serverSocket |
|
|
const id = this._io.ephemeral === false && this.socket === this._io.serverSocket |
|
@ -296,7 +301,7 @@ class Request { |
|
|
state.buffer[state.start++] = (id ? 1 : 0) | (token ? 2 : 0) | (closerNodes.length > 0 ? 4 : 0) | (error > 0 ? 8 : 0) | (value ? 16 : 0) |
|
|
state.buffer[state.start++] = (id ? 1 : 0) | (token ? 2 : 0) | (closerNodes.length > 0 ? 4 : 0) | (error > 0 ? 8 : 0) | (value ? 16 : 0) |
|
|
|
|
|
|
|
|
c.uint16.encode(state, this.tid) |
|
|
c.uint16.encode(state, this.tid) |
|
|
peer.ipv4.encode(state, this.from) |
|
|
peer.ipv4.encode(state, from) |
|
|
|
|
|
|
|
|
if (id) c.fixed32.encode(state, this._io.table.id) |
|
|
if (id) c.fixed32.encode(state, this._io.table.id) |
|
|
if (token) c.fixed32.encode(state, this._io.token(this.to, 1)) |
|
|
if (token) c.fixed32.encode(state, this._io.token(this.to, 1)) |
|
@ -304,35 +309,35 @@ class Request { |
|
|
if (error > 0) c.uint.encode(state, error) |
|
|
if (error > 0) c.uint.encode(state, error) |
|
|
if (value) c.buffer.encode(state, value) |
|
|
if (value) c.buffer.encode(state, value) |
|
|
|
|
|
|
|
|
this.socket.send(state.buffer, 0, state.buffer.byteLength, this.from.port, this.from.host) |
|
|
this.socket.send(state.buffer, 0, state.buffer.byteLength, from.port, from.host) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
_encodeRequest () { |
|
|
_encodeRequest (token, value) { |
|
|
const id = this._io.ephemeral === false && this.socket === this._io.serverSocket |
|
|
const id = this._io.ephemeral === false && this.socket === this._io.serverSocket |
|
|
const state = { start: 0, end: 1 + 1 + 6 + 2, buffer: null } // (type | version) + flags + to + tid
|
|
|
const state = { start: 0, end: 1 + 1 + 6 + 2, buffer: null } // (type | version) + flags + to + tid
|
|
|
|
|
|
|
|
|
if (id) state.end += 32 |
|
|
if (id) state.end += 32 |
|
|
if (this.token) state.end += 32 |
|
|
if (token) state.end += 32 |
|
|
|
|
|
|
|
|
c.string.preencode(state, this.command) |
|
|
c.string.preencode(state, this.command) |
|
|
|
|
|
|
|
|
if (this.target) state.end += 32 |
|
|
if (this.target) state.end += 32 |
|
|
if (this.value) c.buffer.preencode(state, this.value) |
|
|
if (value) c.buffer.preencode(state, value) |
|
|
|
|
|
|
|
|
state.buffer = Buffer.allocUnsafe(state.end) |
|
|
state.buffer = Buffer.allocUnsafe(state.end) |
|
|
state.buffer[state.start++] = REQUEST_ID |
|
|
state.buffer[state.start++] = REQUEST_ID |
|
|
state.buffer[state.start++] = (id ? 1 : 0) | (this.token ? 2 : 0) | (this.target ? 4 : 0) | (this.value ? 8 : 0) |
|
|
state.buffer[state.start++] = (id ? 1 : 0) | (token ? 2 : 0) | (this.target ? 4 : 0) | (value ? 8 : 0) |
|
|
|
|
|
|
|
|
c.uint16.encode(state, this.tid) |
|
|
c.uint16.encode(state, this.tid) |
|
|
peer.ipv4.encode(state, this.to) |
|
|
peer.ipv4.encode(state, this.to) |
|
|
|
|
|
|
|
|
if (id) c.fixed32.encode(state, this._io.table.id) |
|
|
if (id) c.fixed32.encode(state, this._io.table.id) |
|
|
if (this.token) c.fixed32.encode(state, this.token) |
|
|
if (token) c.fixed32.encode(state, token) |
|
|
|
|
|
|
|
|
c.string.encode(state, this.command) |
|
|
c.string.encode(state, this.command) |
|
|
|
|
|
|
|
|
if (this.target) c.fixed32.encode(state, this.target) |
|
|
if (this.target) c.fixed32.encode(state, this.target) |
|
|
if (this.value) c.buffer.encode(state, this.value) |
|
|
if (value) c.buffer.encode(state, value) |
|
|
|
|
|
|
|
|
return state.buffer |
|
|
return state.buffer |
|
|
} |
|
|
} |
|
|