Skip to content

API reference / @jsprismarine/native-network / NativeNetwork

Class: NativeNetwork ​

Defined in: packages/native-network/src/NativeNetwork.ts:62

The native library, loaded once per thread, and that thread's event queue.

The library is mapped once per process, and each thread that loads it opens a queue of its own, so a worker's peer connections report to the worker. libdatachannel's threads queue events in native memory and send one datagram to a loopback socket here when the queue stops being empty. The socket's handler drains the queue and dispatches each event to the handler registered for its id, so every event reaches JavaScript on the event loop, in order, with no call from a foreign thread.

Properties ​

queue ​

readonly queue: number

Defined in: packages/native-network/src/NativeNetwork.ts:80

The handle of this thread's event queue, which peer connections are created on.


wakes ​

wakes: number = 0

Defined in: packages/native-network/src/NativeNetwork.ts:66

Wake datagrams received; a burst of events costs one.

Accessors ​

symbols ​

Get Signature ​

get symbols(): NativeSymbols

Defined in: packages/native-network/src/NativeNetwork.ts:142

The bound C interface.

Returns ​

NativeSymbols

Methods ​

close() ​

close(): void

Defined in: packages/native-network/src/NativeNetwork.ts:238

Closes this thread's peer connections and its event queue. The library stays mapped, and another thread's connections and the shared ports are not touched.

Returns ​

void


drain() ​

drain(): void

Defined in: packages/native-network/src/NativeNetwork.ts:191

Reads every queued event and dispatches it; a read that finds the queue empty rearms the wake.

Returns ​

void


flushLog() ​

flushLog(): void

Defined in: packages/native-network/src/NativeNetwork.ts:201

Hands every queued log line to the log handler now, and keeps the other events, in order, for the next turn of the event loop. A call that failed reads the lines it logged this way without running another handler in the middle of its own failure.

Returns ​

void


hold() ​

hold(): void

Defined in: packages/native-network/src/NativeNetwork.ts:181

Keeps the process alive until the matching NativeNetwork.release.

Returns ​

void


onLog() ​

onLog(handler): void

Defined in: packages/native-network/src/NativeNetwork.ts:176

Receives libdatachannel's log lines, when the library was loaded with a log level.

Parameters ​

handler ​

(level, line) => void

Receives each line and its level.

Returns ​

void


pointerOf() ​

pointerOf(memory): number | bigint

Defined in: packages/native-network/src/NativeNetwork.ts:151

The address of shared memory, for an interface call that takes a pointer.

Parameters ​

memory ​

SharedArrayBuffer

The memory; it must outlive every native use of the pointer.

Returns ​

number | bigint

The pointer.


release() ​

release(): void

Defined in: packages/native-network/src/NativeNetwork.ts:186

Lets the process exit once nothing holds it.

Returns ​

void


unwatch() ​

unwatch(id): void

Defined in: packages/native-network/src/NativeNetwork.ts:168

Stops sending an id's events anywhere.

Parameters ​

id ​

number

The peer connection or data channel.

Returns ​

void


watch() ​

watch(id, handler): void

Defined in: packages/native-network/src/NativeNetwork.ts:160

Sends the events of one id to a handler, until NativeNetwork.unwatch.

Parameters ​

id ​

number

The peer connection or data channel.

handler ​

NativeEventHandler

Receives its events in order.

Returns ​

void


load() ​

static load(options?): Promise<NativeNetwork>

Defined in: packages/native-network/src/NativeNetwork.ts:96

Loads the library and opens this thread's event queue, the first time it is called on a thread.

Parameters ​

options? ​

The library file and log level; the defaults find the installed build and log warnings and worse. libdatachannel takes the level once per process, from the first load.

logLevel? ​

number

path? ​

string

Returns ​

Promise<NativeNetwork>

The loaded library.

Throws ​

When the library cannot be found, loaded, or speaks another interface.


loaded() ​

static loaded(): NativeNetwork | undefined

Defined in: packages/native-network/src/NativeNetwork.ts:137

The loaded library, or undefined before NativeNetwork.load.

Returns ​

NativeNetwork | undefined