Transport layer implementation for Detox project.
This library provides transport layer implementation with simple API that:
- ensures constant data rate on connection
- supports sending numeric commands with payload
- compresses payloads of some commands
npm install @detox/transport
Node.js:
var detox_transport = require('@detox/transport')
detox_transport.ready(function () {
// Do stuff
});
Browser:
requirejs(['@detox/transport'], function (detox_transport) {
detox_transport.ready(function () {
// Do stuff
});
})
detox_transport.P2P_transport(id : Uint8Array, peer_id : Uint8Array, initiator : boolean, ice_servers : Object[], packets_per_second : number, uncompressed_commands_offset : number) : detox_transport.P2P_transport
Constructor for peer-to-peer transport between 2 nodes in Detox network.
id
- own IDpeer_id
- ID of a peerinitiator
- whether current node initiates connectionice_servers
- array of objects as in RTCPeerConnection constructorpackets_per_second
- packets are sent at constant rate (which together with fixed packet size of 512 bytes can be used to identify bandwidth requirements for specific connection),1
is minimal supported rate, actual rate is negotiated between 2 nodes on connectionuncompressed_commands_offset
- commands with number less than this will be compressed/decompressed with zlib
signal
- as generated bysignal
event
Allows to change old peer ID to new one, is useful when connection needs to be initialized, but peer's ID is not yet known, so that random ID can be used and then updated to correct one.
Send command with some payload to the remote node.
command
- command from range[0, 255]
data
- command payload, up todetox_transport.MAX_COMPRESSED_DATA_SIZE
bytes for commands belowuncompressed_commands_offset
and up todetox_transport.MAX_DATA_SIZE
bytes for other commands
Destroy instance, disconnect from the remote node.
Register event handler.
detox_transport.P2P_transport.once(event: string, callback: Function) : detox_transport.P2P_transport
Register one-time event handler (just on()
+ off()
under the hood).
detox_transport.P2P_transport.off(event: string[, callback: Function]) : detox_transport.P2P_transport
Unregister event handler.
Payload consists of single Uint8Array
argument signal
.
Event is fired when signaling data is available and should be sent to remote node.
Event is fired when new remote node is connected.
Event is fired when new remote node is disconnected.
Payload consists of two arguments: command
(number
) and data
(Uint8Array
).
Event is fired when new remote node have sent data using send()
method.
detox_transport.Transport(id : Uint8Array, ice_servers : Object[], packets_per_second : number, uncompressed_commands_offset : number, connect_timeout : number) : detox_transport.Transport
Constructor for transport object that encapsulates multiple P2P connections and uses P2P_transport
under the hood.
id
- own IDice_servers
- array of objects as in RTCPeerConnection constructorpackets_per_second
- packets are sent at constant rate (which together with fixed packet size of 512 bytes can be used to identify bandwidth requirements for specific connection),1
is minimal supported rate, actual rate is negotiated between 2 nodes on connectionuncompressed_commands_offset
- commands with number less than this will be compressed/decompressed with zlibconnect_timeout
- how many seconds sincesignal
generation to wait for connection before failing
detox_transport.Transport.create_connection(initiator : boolean, peer_id : Uint8Array) : detox_transport.P2P_transport
Create new connection for peer_id
as initiator or responder.
Returns instance of P2P_transport
on success.
Get connection for peer_id
that was already created.
Returns instance of P2P_transport
if it was already created or null
otherwise.
detox_transport.Transport.update_peer_id(old_peer_id : Uint8Array, new_peer_id : Uint8Array) : boolean
Allows to change old peer ID to new one, is useful when connection needs to be initialized, but peer's ID is not yet known, so that random ID can be used and then updated to correct one.
Returns true
if update succeeded (will fail when connection to peer with new_peer_id
already exists).
Destroy connection for peer_id
.
peer_id
- as generated bysignal
eventsignal
- as generated bysignal
event
Send command with some payload to the remote node.
peer_id
- peer to which sent commandcommand
- command from range[0, 255]
data
- command payload, up todetox_transport.MAX_COMPRESSED_DATA_SIZE
bytes for commands belowuncompressed_commands_offset
and up todetox_transport.MAX_DATA_SIZE
bytes for other commands
Destroy instance, disconnect from all of the peers.
Register event handler.
Register one-time event handler (just on()
+ off()
under the hood).
Unregister event handler.
Payload consists of two Uint8Array
arguments: peer_id
and signal
.
Event is fired when signaling data is available and should be sent to remote node.
Payload consists of single Uint8Array
argument peer_id
.
Event is fired when new remote node is connected.
Payload consists of single Uint8Array
argument peer_id
.
Event is fired when new remote node is disconnected.
Payload consists of three arguments: peer_id
(Uint8Array
), command
(number
) and data
(Uint8Array
).
Event is fired when new remote node have sent data using send()
method.
Constant that defines max data size supported for sending for non-compressed commands (controlled by uncompressed_commands_offset
option).
Constant that defines max data size supported for sending for compressed commands (controlled by uncompressed_commands_offset
option).
Feel free to create issues and send pull requests (for big changes create an issue first and link it from the PR), they are highly appreciated!
When reading LiveScript code make sure to configure 1 tab to be 4 spaces (GitHub uses 8 by default), otherwise code might be hard to read.
Free Public License 1.0.0 / Zero Clause BSD License