The default provided types include:
Bool
Byte
Char
Decimal
Double
Float
Int
Long
SByte
Short
UInt
ULong
UShort
String
DateTime
TimeSpan
Uri
Vector2
Vector3
Vector4
Quaternion
Plane
Matrix3x2
Matrix4x4
VoltRpc will automatically handle arrays for any type.
Custom Types
Custom types can be implemented using a TypeReadWriter<T>.
For this example we will be using a custom CustomType
that looks like this:
public struct CustomType
{
public int UserId { get; set; }
public string Message { get; set; }
}
To create a TypeReadWriter<T> for CustomType
, you would do:
using VoltRpc.IO;
using VoltRpc.Types;
public class CustomTypeReadWriter : TypeReadWriter<CustomType>
{
public void Write(BufferedWriter writer, CustomType customType)
{
writer.WriteInt(customType.UserId);
writer.WriteString(customType.Message);
}
public CustomType Read(BufferedReader reader)
{
return new CustomType
{
UserId = reader.ReadInt(),
Message = reader.ReadString()
};
}
}
You can read and write any type supported by BufferedWriter and BufferedReader.
To let the client and host know about this type, you will need to call their respected TypeReaderWriterManager.AddType.
For example:
client.TypeReaderWriterManager.AddType<CustomType>(new CustomTypeReadWriter());
host.TypeReaderWriterManager.AddType<CustomType>(new CustomTypeReadWriter());