-
Notifications
You must be signed in to change notification settings - Fork 10
KRPC Sockets
Low level implementation of the KRPC network layer that the BitTorrent DHT uses. Mostly extracted from the bittorrent-dht module on NPM into its own repo.
npm install @dwdht/krpc-socket
var rpc = require('@dwdht/krpc-socket')
var socket = rpc()
socket.on('query', function (query, peer) {
socket.response(peer, query, {echo: query.a})
})
socket.bind(10000, function () {
var anotherSocket = rpc()
anotherSocket.query({host: '127.0.0.1', port: 10000}, {q: 'echo', a: {greetings: 'martian'}}, function (err, response) {
console.log(response.r) // prints {echo: {hello: Buffer('world')}}
})
})
Create a new @dwdht/krpc-socket
. Options include:
{
timeout: queryTimeout, // defaults to 2s
socket: optionalUdpSocket,
isIP: optionalBooleanFunction
}
Send a raw message. The callback is called when the message has been flushed from the socket.
Send a query message. The callback is called with (err, response, peer, request)
.
You should set the method name you are trying to call as {q: 'method_name'}
and query data as {a: someQueryData}
.
The query method returns a query id. You can use this id to cancel the query using the .cancel
method.
Cancel a query. Will call the corresponding query's callback with an error indicating that it was cancelled.
Send a response to a query. The callback is called when the message has been flushed from the socket.
Send an error reploy to a query. The callback is called when the message has been flushed from the socket.
Integer representing the number of concurrent queries that are currently pending.
Destroys and unbinds the socket
Call this to bind to a specific port. If port is not specified or is 0, the operating system will attempt to bind to a random port. If address is not specified, the operating system will attempt to listen on all addresses.
If you don't call this a random free port will be chosen.
When a query is received a query
event is emitted with the query data and a peer object representing the querying peer.
Emitted when a non fatal error has occurred. It is safe to ignore this.
Emitted when a fatal error has occurred.