RiptideNetworking / Riptide

Lightweight C# networking solution for multiplayer games.
https://riptide.tomweiland.net
MIT License
1.14k stars 144 forks source link

Better packet model handling #137

Closed bberka closed 7 months ago

bberka commented 7 months ago

Before i start im very new to these thing, i somewhat understand the logic behind multiplayer etc.

However i see in RiptideNetworking provides more low level management which is good for some cases

What i wanted to ask is if you wish to implement something like this for packet models

namespace NetTCP.Example.Shared.Network.Message.Common;

[Packet(OpCodes.VersionInformation,PacketType.ClientAndServer)]
public class VersionInformation : IPacket
{
  public string Version { get; set; } = "1.0.0";
  public bool Debug { get; set; } = true;
  public bool Beta { get; set; } = false;

  public void Write(TcpPacketWriter writer) {
    writer.Write(Version);
    writer.Write(Debug);
    writer.Write(Beta);
  }

  public void Read(TcpPacketReader reader) {
    Version = reader.ReadString();
    Debug = reader.ReadBool();
    Beta = reader.ReadBool();
  }
}

Instead of writing raw data inside handlers it would be (i think) better t omanage packet models like this because once things gets complicated it will be very hard to manage readers

This way all packet reader and writes are in same place

Serialization as you can see its raw binary and only important thing is you have to keep the same order (you probably know that)

I have similiar thing implemented on this https://github.com/bberka/NetTCP repository. You probably should check the dev/rework repo which is what i have been working on. You can check NetTcpPacketManager.cs to see how it is handled etc.

Since im not very much experienced its somewhat good but mine (probably) is not for game servers

tom-weiland commented 7 months ago

This already exists in the form of IMessageSerializable:

public struct MyData : IMessageSerializable
{
    public int ExampleInt;

    public void Serialize(Message message)
    {
        message.AddInt(ExampleInt);
    }

    public void Deserialize(Message message)
    {
        ExampleInt = message.GetInt();
    }
}

Then you can message.AddSerializable(new MyData() { ExampleInt = someValue }); and use message.GetSerializable() on the other end.