RakNet
The RakNet library provides low-level access to outgoing game packets.
Interacting with RakNet is unsafe and can lead to account terminations, broken game behavior, disconnects, or other unintended side effects. Do not use this library unless you understand the risks and know exactly what you are doing.
Overview
RakNet allows you to:
- Inspect outgoing packets before they are sent
- Modify or block packets in a send hook
- Send custom packet payloads manually
- Work with packet data as a buffer, string, or byte array
Functions
| Function | Description |
|---|---|
raknet.add_send_hook | Register a callback that runs before a packet is sent |
raknet.remove_send_hook | Remove a callback registered with raknet.add_send_hook |
raknet.send | Send a packet with a payload, priority, reliability, and ordering channel |
Packet API
Methods
| Method | Description |
|---|---|
RakNetPacket:SetData | Replace the packet payload |
RakNetPacket:Block | Prevent the packet from being sent |
Properties
| Property | Type | Description |
|---|---|---|
RakNetPacket.AsBuffer | buffer | Packet data as a buffer |
RakNetPacket.AsString | string | Packet data as a string |
RakNetPacket.AsArray | {number} | Packet data as an array of bytes |
RakNetPacket.Priority | number | Packet priority |
RakNetPacket.Reliability | number | Packet reliability |
RakNetPacket.OrderingChannel | number | Packet ordering channel |
RakNetPacket.Size | number | Packet payload size in bytes |
Hook Syntax
raknet.add_send_hook(hook: (packet: RakNetPacket) -> ()) -> ()
raknet.remove_send_hook(hook: (packet: RakNetPacket) -> ()) -> ()Send Syntax
raknet.send(
data: buffer | string | {number},
priority: number,
reliability: number,
ordering_channel: number
) -> ()Packet Method Syntax
RakNetPacket:SetData(data: buffer | string | {number}) -> ()
RakNetPacket:Block() -> ()Parameters
raknet.add_send_hook
| Parameter | Type | Description |
|---|---|---|
hook | (packet: RakNetPacket) -> () | Callback fired before a packet is sent |
raknet.remove_send_hook
| Parameter | Type | Description |
|---|---|---|
hook | (packet: RakNetPacket) -> () | Previously registered send hook |
raknet.send
| Parameter | Type | Description |
|---|---|---|
data | `buffer | string |
priority | number | RakNet send priority |
reliability | number | RakNet reliability mode |
ordering_channel | number | Ordering channel used for ordered traffic |
RakNetPacket:SetData
| Parameter | Type | Description |
|---|---|---|
data | `buffer | string |
Returns
All raknet functions and RakNetPacket methods documented here do not return a value.
Description
raknet.add_send_hook lets you intercept outgoing packets before they are sent. Inside a hook, you can inspect packet metadata, read its payload in multiple formats, replace the payload with RakNetPacket:SetData, or stop transmission entirely with RakNetPacket:Block.
raknet.send sends a custom packet payload using the provided priority, reliability, and ordering channel. Payload data may be supplied as a buffer, a string, or an array of byte values.
Because this library operates at the packet level, mistakes can be difficult to debug and may have immediate consequences. Keep hooks minimal, validate payloads carefully, and avoid modifying traffic unless absolutely necessary.
Example: Logging Outgoing Packets
local function packetLogger(packet)
print("Outgoing packet:")
print(" Size:", packet.Size)
print(" Priority:", packet.Priority)
print(" Reliability:", packet.Reliability)
print(" Ordering channel:", packet.OrderingChannel)
end
raknet.add_send_hook(packetLogger)Example: Blocking a Packet
local function blockLargePackets(packet)
if packet.Size > 512 then
warn("Blocked packet larger than 512 bytes")
packet:Block()
end
end
raknet.add_send_hook(blockLargePackets)Example: Replacing Packet Data
local function rewritePacket(packet)
local bytes = packet.AsArray
if #bytes > 0 then
bytes[1] = 0x01
packet:SetData(bytes)
end
end
raknet.add_send_hook(rewritePacket)Example: Sending a Packet
local payload = { 0x01, 0x02, 0x03, 0x04 }
raknet.send(payload, 0, 0, 0)Notes
- Hooks run before the packet is sent
RakNetPacket:Block()prevents the current packet from being transmittedRakNetPacket:SetData()replaces the packet payload in-place- Packet data can be read and written as a
buffer,string, or byte array - Invalid payloads or incorrect metadata can disconnect clients or break protocol behavior