Skip to content
googer_ edited this page Mar 25, 2025 · 6 revisions

About GNet

GNet is a better abstraction over default gmod net library. It's pure lua and does not use any remote server - everything is done with default net. It offers support for automatic data compression and chunking - theoretically allowing for transfer of infinite amount of data.

Capability net

Info

  • ID: alt text net
  • Accessed with: thorium.gnet

Hooks

alt text GM:Thorium_NetworkMessageReceived(message: string, size: number)
Called when whole network message (not chunked) was received by GNet.

alt text GM:Thorium_NetworkMessageBegin(message: string, totalsize: number, transid: number)
Called when starting receiving chunked network message.

alt text GM:Thorium_NetworkMessageChunk(message: string, recvsize: number, transid: number)
Called when network message chunk was received.

alt text GM:Thorium_NetworkMessageEnd(message: string, totalsize: number, transid: number)
Called when chunked network message was fully received.

Functions

alt text GNet.Start(message: string) -> NetHandle
Creates new NetHandle

alt text GNet.Receive(message: string, callback: fun(handle: NetHandle, size: integer, from: Player|nil))
Adds a net message handler. Only one receiver can be used to receive the message
If message was received from another alt text client, "p2p_" should be appended to the beginning

alt text GNet.Send(handle: NetHandle, target: Entity|Player|"SERVER"|"BROADCAST|nil, docompress: boolean?, dochunk: boolean?, callbacks: table?) -> number|nil
Sends a NetHandle over network
If on alt text client, and target is not set, set to Entity(0) or "SERVER" will send to server.
If on alt text server, and target is set to "BROADCAST", will broadcast to everyone.
If callbacks table is supplied, function with key "progress" gets called every time that new chunk is transmitted. (arguments are message, handle, and pointer)
If callbacks table is supplied, function with key "done" gets called once every fragment of message was transmitted. (arguments are message and handle)
Returns transfer id or nil, if transfer was completed immediately

alt text GNet.GetByTransferID(transid: number) -> NetHandle|nil
Gets NetHandle by transfer ID. If you are going to use this function, please note that

  • it's your responsibility to keep pointer in the right place after your code returns.
  • GNet network messages are not validated on other end, changing data in handle will result in corruption

alt text GNet.GetReceiver(message: string) -> fun(handle: NetHandle, size: integer, from: Player|nil) Gets GNet message handler. Only one receiver can be used to receive the message


Class NetHandle


Info

  • Inherits: ByteBuffer

Reading functions

alt text NetHandle.ReadEntity(self: NetHandle, ent: any) -> ent: Entity
Reads entity from buffer. Reads 2 bytes.


Writing functions

alt text NetHandle.WriteEntity(self: NetHandle, ent: Entity) -> self: NetHandle
Writes entity to buffer. Writes 2 bytes.

Clone this wiki locally