Table of Contents

Class StreamPeer

Namespace
Godot
Assembly
GodotSharp.dll

StreamPeer is an abstract base class mostly used for stream-based protocols (such as TCP). It provides an API for sending and receiving data through streams as raw data or strings.

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 StreamPeer : RefCounted, IDisposable
Inheritance
StreamPeer
Implements
Derived
Inherited Members

Properties

BigEndian

If true, this StreamPeer will using big-endian format for encoding and decoding.

public bool BigEndian { get; set; }

Property Value

bool

Methods

Get16()

Gets a signed 16-bit value from the stream.

public short Get16()

Returns

short

Get32()

Gets a signed 32-bit value from the stream.

public int Get32()

Returns

int

Get64()

Gets a signed 64-bit value from the stream.

public long Get64()

Returns

long

Get8()

Gets a signed byte from the stream.

public sbyte Get8()

Returns

sbyte

GetAvailableBytes()

Returns the number of bytes this StreamPeer has available.

public int GetAvailableBytes()

Returns

int

GetData(int)

Returns a chunk data with the received bytes. The number of bytes to be received can be requested in the bytes argument. If not enough bytes are available, the function will block until the desired amount is received. This function returns two values, an Error code and a data array.

public Array GetData(int bytes)

Parameters

bytes int

Returns

Array

GetDouble()

Gets a double-precision float from the stream.

public double GetDouble()

Returns

double

GetFloat()

Gets a single-precision float from the stream.

public float GetFloat()

Returns

float

GetPartialData(int)

Returns a chunk data with the received bytes. The number of bytes to be received can be requested in the "bytes" argument. If not enough bytes are available, the function will return how many were actually received. This function returns two values, an Error code, and a data array.

public Array GetPartialData(int bytes)

Parameters

bytes int

Returns

Array

GetString(int)

Gets an ASCII string with byte-length bytes from the stream. If bytes is negative (default) the length will be read from the stream using the reverse process of PutString(string).

public string GetString(int bytes = -1)

Parameters

bytes int

Returns

string

GetU16()

Gets an unsigned 16-bit value from the stream.

public ushort GetU16()

Returns

ushort

GetU32()

Gets an unsigned 32-bit value from the stream.

public uint GetU32()

Returns

uint

GetU64()

Gets an unsigned 64-bit value from the stream.

public ulong GetU64()

Returns

ulong

GetU8()

Gets an unsigned byte from the stream.

public byte GetU8()

Returns

byte

GetUtf8String(int)

Gets a UTF-8 string with byte-length bytes from the stream (this decodes the string sent as UTF-8). If bytes is negative (default) the length will be read from the stream using the reverse process of PutUtf8String(string).

public string GetUtf8String(int bytes = -1)

Parameters

bytes int

Returns

string

GetVar(bool)

Gets a Variant from the stream. 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

Variant

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_name

Name of the method to check for.

Returns

bool

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_name

Name of the signal to check for.

Returns

bool

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_name

Name of the method to invoke.

args NativeVariantPtrArgs

Arguments to use with the invoked method.

ret godot_variant

Value returned by the invoked method.

Returns

bool

Put16(short)

Puts a signed 16-bit value into the stream.

public void Put16(short value)

Parameters

value short

Put32(int)

Puts a signed 32-bit value into the stream.

public void Put32(int value)

Parameters

value int

Put64(long)

Puts a signed 64-bit value into the stream.

public void Put64(long value)

Parameters

value long

Put8(sbyte)

Puts a signed byte into the stream.

public void Put8(sbyte value)

Parameters

value sbyte

PutData(byte[])

Sends a chunk of data through the connection, blocking if necessary until the data is done sending. This function returns an Error code.

public Error PutData(byte[] data)

Parameters

data byte[]

Returns

Error

PutDouble(double)

Puts a double-precision float into the stream.

public void PutDouble(double value)

Parameters

value double

PutFloat(float)

Puts a single-precision float into the stream.

public void PutFloat(float value)

Parameters

value float

PutPartialData(byte[])

Sends a chunk of data through the connection. If all the data could not be sent at once, only part of it will. This function returns two values, an Error code and an integer, describing how much data was actually sent.

public Array PutPartialData(byte[] data)

Parameters

data byte[]

Returns

Array

PutString(string)

Puts a zero-terminated ASCII string into the stream prepended by a 32-bit unsigned integer representing its size.

Note: To put an ASCII string without prepending its size, you can use PutData(byte[]):

PutData("Hello World".ToAsciiBuffer());
public void PutString(string value)

Parameters

value string

PutU16(ushort)

Puts an unsigned 16-bit value into the stream.

public void PutU16(ushort value)

Parameters

value ushort

PutU32(uint)

Puts an unsigned 32-bit value into the stream.

public void PutU32(uint value)

Parameters

value uint

PutU64(ulong)

Puts an unsigned 64-bit value into the stream.

public void PutU64(ulong value)

Parameters

value ulong

PutU8(byte)

Puts an unsigned byte into the stream.

public void PutU8(byte value)

Parameters

value byte

PutUtf8String(string)

Puts a zero-terminated UTF-8 string into the stream prepended by a 32 bits unsigned integer representing its size.

Note: To put a UTF-8 string without prepending its size, you can use PutData(byte[]):

PutData("Hello World".ToUtf8Buffer());
public void PutUtf8String(string value)

Parameters

value string

PutVar(Variant, bool)

Puts a Variant into the stream. 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 void PutVar(Variant value, bool fullObjects = false)

Parameters

value Variant
fullObjects bool