Types

  • 2 minutes to read

The default provided types include:

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());