using System; using Lidgren.Network; #nullable disable namespace Robust.Shared.Network { /// /// The group the message belongs to, used for statistics and packet channels. /// public enum MsgGroups : byte { /// /// Error state, the message needs to set a different one. /// Error = 0, /// /// A core message, like connect, disconnect, and tick. /// Core, /// /// Entity message, for keeping entities in sync. /// Entity, /// /// A string message, for chat. /// String, /// /// A command message from client -> server. /// Command, /// /// ECS Events between the server and the client. /// EntityEvent, } /// /// A packet message that the NetManager sends/receives. /// public abstract class NetMessage { /// /// String identifier of the message type. /// public virtual string MsgName { get; } /// /// The group this message type belongs to. /// public virtual MsgGroups MsgGroup { get; } /// /// The channel that this message came in on. /// public INetChannel MsgChannel { get; set; } = default!; /// /// The size of this packet in bytes. /// public int MsgSize { get; set; } protected NetMessage() { MsgName = GetType().Name; } /// /// Deserializes the NetIncomingMessage into this NetMessage class. /// /// The buffer of the raw incoming packet. public abstract void ReadFromBuffer(NetIncomingMessage buffer); /// /// Serializes this NetMessage into a new NetOutgoingMessage. /// /// The buffer of the new packet being serialized. public abstract void WriteToBuffer(NetOutgoingMessage buffer); public virtual NetDeliveryMethod DeliveryMethod { get { switch (MsgGroup) { case MsgGroups.Entity: return NetDeliveryMethod.Unreliable; case MsgGroups.Core: case MsgGroups.Command: return NetDeliveryMethod.ReliableUnordered; case MsgGroups.String: case MsgGroups.EntityEvent: return NetDeliveryMethod.ReliableOrdered; default: throw new ArgumentOutOfRangeException(); } } } } }