Class PacketPeer
- Namespace
- Godot
- Assembly
- GodotSharp.dll
PacketPeer is an abstraction and base class for packet-based protocols (such as UDP). It provides an API for sending and receiving packets both as raw data or variables. This makes it easy to transfer data over a protocol, without having to encode data as low-level bytes or having to worry about network ordering.
Note: When exporting to Android, make sure to enable the INTERNET
permission in the Android export preset before exporting the project or using one-click deploy. Otherwise, network communication of any kind will be blocked by Android.
public class PacketPeer : RefCounted, IDisposable
- Inheritance
-
PacketPeer
- Implements
- Derived
- Inherited Members
Properties
EncodeBufferMaxSize
Maximum buffer size allowed when encoding Variants. Raise this value to support heavier memory allocations.
The PutVar(Variant, bool) method allocates memory on the stack, and the buffer used will grow automatically to the closest power of two to match the size of the Variant. If the Variant is bigger than EncodeBufferMaxSize, the method will error out with OutOfMemory.
public int EncodeBufferMaxSize { get; set; }
Property Value
Methods
GetAvailablePacketCount()
Returns the number of packets currently available in the ring-buffer.
public int GetAvailablePacketCount()
Returns
GetPacket()
Gets a raw packet.
public byte[] GetPacket()
Returns
- byte[]
GetPacketError()
Returns the error state of the last packet received (via GetPacket() and GetVar(bool)).
public Error GetPacketError()
Returns
GetVar(bool)
Gets a Variant. If allowObjects
is true
, decoding objects is allowed.
Internally, this uses the same decoding mechanism as the @GlobalScope.bytes_to_var
method.
Warning: Deserialized objects can contain code which gets executed. Do not use this option if the serialized object comes from untrusted sources to avoid potential security threats such as remote code execution.
public Variant GetVar(bool allowObjects = false)
Parameters
allowObjects
bool
Returns
HasGodotClassMethod(in godot_string_name)
Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.
protected override bool HasGodotClassMethod(in godot_string_name method)
Parameters
method
godot_string_nameName of the method to check for.
Returns
HasGodotClassSignal(in godot_string_name)
Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.
protected override bool HasGodotClassSignal(in godot_string_name signal)
Parameters
signal
godot_string_nameName of the signal to check for.
Returns
InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)
Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.
protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)
Parameters
method
godot_string_nameName of the method to invoke.
args
NativeVariantPtrArgsArguments to use with the invoked method.
ret
godot_variantValue returned by the invoked method.
Returns
PutPacket(byte[])
Sends a raw packet.
public Error PutPacket(byte[] buffer)
Parameters
buffer
byte[]
Returns
PutVar(Variant, bool)
Sends a Variant as a packet. If fullObjects
is true
, encoding objects is allowed (and can potentially include code).
Internally, this uses the same encoding mechanism as the @GlobalScope.var_to_bytes
method.
public Error PutVar(Variant var, bool fullObjects = false)