Class TypedP2PMessage<TPayload>
- Namespace
- SteamNetworkLib.Models
- Assembly
- SteamNetworkLib.dll
Represents a custom P2P message that carries a strongly typed JSON payload.
public abstract class TypedP2PMessage<TPayload> : P2PMessage
Type Parameters
TPayloadThe payload DTO type to serialize. Use primitive values, strings, arrays, lists, dictionaries, and small nested DTOs with public get/set properties.
- Inheritance
-
TypedP2PMessage<TPayload>
- Inherited Members
Remarks
Derive from this class when a mod needs a custom message type but does not need to hand-write JSON parsing. The base SenderId and Timestamp metadata is serialized separately from Payload, so payload contracts can stay focused on gameplay state.
This is the preferred shape for transaction and state messages such as restock
requests, label edits, host-authored configuration deltas, and other consumer-mod
messages that need nested DTOs. Do not put CSteamID, Unity objects, item
instances, scene objects, or live storage slot references directly in the payload.
Send stable IDs and primitive values, then resolve runtime objects locally when
handling the message.
If you need a custom binary format, compression, encryption, or a serializer not supported by SteamNetworkLib's sync serializer, inherit from P2PMessage directly instead.
Constructors
TypedP2PMessage()
Initializes a new instance of the TypedP2PMessage<TPayload> class.
protected TypedP2PMessage()
Remarks
A public or protected parameterless constructor is required so MessageSerializer can create message instances when packets are received.
TypedP2PMessage(TPayload)
Initializes a new instance of the TypedP2PMessage<TPayload> class with a payload.
protected TypedP2PMessage(TPayload payload)
Parameters
payloadTPayloadThe payload to send with the message.
Properties
Payload
Gets or sets the typed payload carried by this message.
public TPayload Payload { get; set; }
Property Value
- TPayload
Remarks
The payload is serialized under the Payload JSON property. It should be
non-null before sending unless receivers explicitly handle a default payload.
Methods
Deserialize(byte[])
Deserializes message metadata and the typed payload from UTF-8 JSON.
public override void Deserialize(byte[] data)
Parameters
databyte[]The byte array containing the serialized message data.
Exceptions
- InvalidOperationException
Thrown when the payload cannot be deserialized into
TPayload.
Serialize()
Serializes the message metadata and typed payload to UTF-8 JSON.
public override byte[] Serialize()
Returns
- byte[]
A byte array containing the serialized message data.
Exceptions
- InvalidOperationException
Thrown when the payload cannot be serialized by SteamNetworkLib's sync serializer.