dreamworkAPI
Ctrl K
GitHub
classDocs / Network

Network

classsharedsource
Object → Network

A named two-way channel between the server and the clients built on top of the engine net library, without its 64 KiB message limit.

Data up to 8 KiB is usually sent as a single reliable message, larger data is split into segments and streamed over the unreliable channel with acknowledgements, retransmission, congestion control and a CRC32 check of the whole data.

Transfers to the same peer are queued, one runs at a time per network.

The network must be created on the server first, the client can only open networks the server has registered. Creating a network with the name of an existing one returns the existing object.

On the server, every client is limited in strikes, concurrent transfers and bytes, protocol violations accumulate strikes and kick the client.

Constructor

Network(name: string, can_receive: boolean?): Network

Fields

NameTypeDescription
idintegerThe engine ID of the network, read-only.
namestringThe name of the network without the internal prefix, read-only.
receiverbooleantrue if the network accepts incoming messages, false by default.
timeoutnumberThe number of seconds without any progress after which a transfer is aborted, 15 by default, at least 1.
windowintegerThe maximum number of segments in flight, 64 by default, clamped to [1, 256].
segmentintegerThe size of outgoing segments in bytes, by default fits into a 1 KiB message, at least 256 and at most what fits into one message.
limitintegerThe maximum size of an incoming message in bytes, 64 MiB by default, 0 rejects everything.

Methods

abortCancels the outgoing transmission to the target and all queued ones.
attachAttaches a handler that is called when a message is fully received.
awaitWaits for the next fully received message.
clearRemoves every receive handler.
detachDetaches a previously attached receive handler.
isReceivingChecks if the network is receiving data from the sender.
isSendingChecks if the network is sending data to the target.
onceAttaches a handler that automatically detaches itself right before being invoked, so it only ever runs once.
sendSends the data to the target.
streamAttaches a handler that receives incoming data in order, chunk by chunk, while it arrives.
transmitBuilds the data with a binary writer and sends it to the target.
unstreamDetaches a previously attached stream handler.
unwatchDetaches a previously attached progress handler.
watchAttaches a handler that is called on every transfer progress, both incoming and outgoing.

#Network:abort

sharedsource
function Network:abort(target?: garrysmod.Player): boolean

Cancels the outgoing transmission to the target and all queued ones.

Parameters
NameTypeDescription
targetoptionalgarrysmod.PlayerThe target player, required on the server, ignored on the client.
Returns
TypeNameDescription
booleancancelledtrue if anything was cancelled.

#Network:attach

sharedsource
function Network:attach(handler: Network.ReceiveFn | Hook | Event)

Attaches a handler that is called when a message is fully received.

If the handler is already attached, it will not be attached twice.

Parameters
NameTypeDescription
handlerNetwork.ReceiveFn | Hook | EventThe callback or callable object.

#Network:await

sharedasyncsource
async function Network:await(): Network, BinaryReader, garrysmod.Player | nil

Waits for the next fully received message.

Returns
TypeNameDescription
NetworknetworkThe network.
BinaryReaderreaderThe reader of the message.
garrysmod.Player | nilsenderThe sender, nil on the client.

#Network:clear

sharedsource
function Network:clear()

Removes every receive handler.

#Network:detach

sharedsource
function Network:detach(handler: Network.ReceiveFn | Hook | Event): boolean

Detaches a previously attached receive handler.

Parameters
NameTypeDescription
handlerNetwork.ReceiveFn | Hook | EventThe exact handler reference.
Returns
TypeNameDescription
booleanremovedtrue if the handler was attached.

#Network:isReceiving

sharednodiscardsource
function Network:isReceiving(sender?: garrysmod.Player): boolean

Checks if the network is receiving data from the sender.

Parameters
NameTypeDescription
senderoptionalgarrysmod.PlayerThe sender player, required on the server, ignored on the client.
Returns
TypeNameDescription
booleanis_receivingtrue if a transmission from the sender is running.

#Network:isSending

sharednodiscardsource
function Network:isSending(target?: garrysmod.Player): boolean

Checks if the network is sending data to the target.

Parameters
NameTypeDescription
targetoptionalgarrysmod.PlayerThe target player, required on the server, ignored on the client.
Returns
TypeNameDescription
booleanis_sendingtrue if a transmission to the target is running.

#Network:once

sharedsource
function Network:once(handler: Network.ReceiveFn | Hook | Event)

Attaches a handler that automatically detaches itself right before being invoked, so it only ever runs once.

Parameters
NameTypeDescription
handlerNetwork.ReceiveFn | Hook | EventThe callback or callable object.

#Network:send

sharedsource
function Network:send(data: string, target?: ( garrysmod.Player | garrysmod.Player[] )?, callback?: Network.FinishFn)

Sends the data to the target.

Small data is sent as a single reliable message, larger data is split into segments and streamed with acknowledgements.

If the target is already receiving a transmission from this network, the data is queued behind it.

Parameters
NameTypeDescription
datastringThe data to send.
targetoptional( garrysmod.Player | garrysmod.Player[] )?The target player(s), required on the server, ignored on the client.
callbackoptionalNetwork.FinishFnThe callback that is called when the transmission is finished or failed.

#Network:stream

sharedsource
function Network:stream(handler: Network.StreamFn | Hook | Event)

Attaches a handler that receives incoming data in order, chunk by chunk, while it arrives.

chunk is the data starting at the zero-based byte offset, after the last chunk the handler is called once more with chunk = nil and success, success is false if the transfer failed ( timed out, cancelled or checksum mismatch ), the chunks received before it should be discarded then.

If a network has only stream handlers, the received data is not kept in memory.

Parameters
NameTypeDescription
handlerNetwork.StreamFn | Hook | EventThe callback or callable object.

#Network:transmit

sharedsource
function Network:transmit(fn: fun( network: Network, writer: BinaryWriter ), target?: ( garrysmod.Player | garrysmod.Player[] )?, callback?: Network.FinishFn)

Builds the data with a binary writer and sends it to the target.

Parameters
NameTypeDescription
fnfun( network: Network, writer: BinaryWriter )The function that writes the data.
targetoptional( garrysmod.Player | garrysmod.Player[] )?The target player(s), required on the server, ignored on the client.
callbackoptionalNetwork.FinishFnThe callback that is called when the transmission is finished or failed.

#Network:unstream

sharedsource
function Network:unstream(handler: Network.StreamFn | Hook | Event): boolean

Detaches a previously attached stream handler.

Parameters
NameTypeDescription
handlerNetwork.StreamFn | Hook | EventThe exact handler reference.
Returns
TypeNameDescription
booleanremovedtrue if the handler was attached.

#Network:unwatch

sharedsource
function Network:unwatch(handler: Network.ProgressFn | Hook | Event): boolean

Detaches a previously attached progress handler.

Parameters
NameTypeDescription
handlerNetwork.ProgressFn | Hook | EventThe exact handler reference.
Returns
TypeNameDescription
booleanremovedtrue if the handler was attached.

#Network:watch

sharedsource
function Network:watch(handler: Network.ProgressFn | Hook | Event)

Attaches a handler that is called on every transfer progress, both incoming and outgoing.

Parameters
NameTypeDescription
handlerNetwork.ProgressFn | Hook | EventThe callback or callable object.
Generated from the dreamwork meta branch.