FirstGearGames / FishNet

FishNet: Unity Networking Evolved.
Other
1.39k stars 150 forks source link

Substream feature #679

Closed viliwonka closed 5 months ago

viliwonka commented 6 months ago

Adds SubStream struct that enables simplified serializing inside RPC functions or Broadcasts. It is GC free, albeit for writes there is one byte-buffer copy, read has zero copy.

It is an alternative to using byte[] or ArraySegment as a means for sending dynamically sized data.

Example:

// this method calls RPC
public void SendData()
{
    var stream = SubStream.StartWriting(NetworkManager, out PooledWriter paramWriter);

    // write anything to the stream
    paramWriter.Write("Hello FishNet!");
    paramWriter.WriteInt32(128);

    TestObserverRPC(1, stream, Vector3.zero);

    stream.Dispose();
}

[ObserversRpc]
void TestObserverRPC(int randomParam, SubStream stream, Vector3 randomVec) {

    if(stream.StartReading(out Reader reader)) {
        // read the data from the stream in same order
        var text = reader.ReadString();
        var number = reader.ReadInt32();

        stream.Dispose();
    }
}

It also can be used inside structs, like this:

struct PathfindingData
{
    public Vector3 StartPosition;
    public SubStream PathDeltaStream;
}
FirstGearGames commented 5 months ago

Merging in. I updated the code and file names slightly to match current internals. If you have the time after 4.3.6 release can you please add XML to the methods/members missing them.