Class NetPacketProcessor
Inheritance
Namespace: LiteNetLib.Utils
Assembly: LiteNetLib.dll
Syntax
public class NetPacketProcessor
Constructors
NetPacketProcessor()
Declaration
public NetPacketProcessor()
NetPacketProcessor(int)
Declaration
public NetPacketProcessor(int maxStringLength)
Parameters
| Type | Name | Description |
|---|---|---|
| int | maxStringLength |
Methods
GetCallbackFromData(NetDataReader)
Declaration
protected virtual NetPacketProcessor.SubscribeDelegate GetCallbackFromData(NetDataReader reader)
Parameters
| Type | Name | Description |
|---|---|---|
| NetDataReader | reader |
Returns
| Type | Description |
|---|---|
| NetPacketProcessor.SubscribeDelegate |
GetHash<T>()
Declaration
protected virtual ulong GetHash<T>()
Returns
| Type | Description |
|---|---|
| ulong |
Type Parameters
| Name | Description |
|---|---|
| T |
ReadAllPackets(NetDataReader)
Reads all available data from NetDataReader and calls OnReceive delegates
Declaration
public void ReadAllPackets(NetDataReader reader)
Parameters
| Type | Name | Description |
|---|---|---|
| NetDataReader | reader | NetDataReader with packets data |
ReadAllPackets(NetDataReader, object)
Reads all available data from NetDataReader and calls OnReceive delegates
Declaration
public void ReadAllPackets(NetDataReader reader, object userData)
Parameters
| Type | Name | Description |
|---|---|---|
| NetDataReader | reader | NetDataReader with packets data |
| object | userData | Argument that passed to OnReceivedEvent |
Exceptions
| Type | Condition |
|---|---|
| ParseException | Malformed packet |
ReadPacket(NetDataReader)
Reads one packet from NetDataReader and calls OnReceive delegate
Declaration
public void ReadPacket(NetDataReader reader)
Parameters
| Type | Name | Description |
|---|---|---|
| NetDataReader | reader | NetDataReader with packet |
Exceptions
| Type | Condition |
|---|---|
| ParseException | Malformed packet |
ReadPacket(NetDataReader, object)
Reads one packet from NetDataReader and calls OnReceive delegate
Declaration
public void ReadPacket(NetDataReader reader, object userData)
Parameters
| Type | Name | Description |
|---|---|---|
| NetDataReader | reader | NetDataReader with packet |
| object | userData | Argument that passed to OnReceivedEvent |
Exceptions
| Type | Condition |
|---|---|
| ParseException | Malformed packet |
RegisterNestedType<T>()
Register nested property type
Declaration
public void RegisterNestedType<T>() where T : struct, INetSerializable
Type Parameters
| Name | Description |
|---|---|
| T | INetSerializable structure |
RegisterNestedType<T>(Action<NetDataWriter, T>, Func<NetDataReader, T>)
Register nested property type
Declaration
public void RegisterNestedType<T>(Action<NetDataWriter, T> writeDelegate, Func<NetDataReader, T> readDelegate)
Parameters
| Type | Name | Description |
|---|---|---|
| Action<NetDataWriter, T> | writeDelegate | |
| Func<NetDataReader, T> | readDelegate |
Type Parameters
| Name | Description |
|---|---|
| T |
RegisterNestedType<T>(Func<T>)
Register nested property type
Declaration
public void RegisterNestedType<T>(Func<T> constructor) where T : class, INetSerializable
Parameters
| Type | Name | Description |
|---|---|---|
| Func<T> | constructor |
Type Parameters
| Name | Description |
|---|---|
| T | INetSerializable class |
RemoveSubscription<T>()
Remove any subscriptions by type
Declaration
public bool RemoveSubscription<T>()
Returns
| Type | Description |
|---|---|
| bool | true if remove is success |
Type Parameters
| Name | Description |
|---|---|
| T | Packet type |
SubscribeNetSerializable<T>(Action<T>)
Registers a callback for a packet type that implements INetSerializable and has a parameterless constructor.
Declaration
public void SubscribeNetSerializable<T>(Action<T> onReceive) where T : INetSerializable, new()
Parameters
| Type | Name | Description |
|---|---|---|
| Action<T> | onReceive | The delegate to be executed when the packet is received. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the packet. Must implement INetSerializable and have a new() constraint. |
Remarks
To reduce allocations, this method uses a single internal reference to T for deserialization.
SubscribeNetSerializable<T>(Action<T>, Func<T>)
Registers a callback for a packet type that implements INetSerializable, using a custom constructor.
Declaration
public void SubscribeNetSerializable<T>(Action<T> onReceive, Func<T> packetConstructor) where T : INetSerializable
Parameters
| Type | Name | Description |
|---|---|---|
| Action<T> | onReceive | The delegate to be executed when the packet is received. |
| Func<T> | packetConstructor | A function that returns a new instance of |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the packet. Must implement INetSerializable. |
SubscribeNetSerializable<T, TUserData>(Action<T, TUserData>)
Registers a callback for a packet type that implements INetSerializable and has a parameterless constructor, supporting user data.
Declaration
public void SubscribeNetSerializable<T, TUserData>(Action<T, TUserData> onReceive) where T : INetSerializable, new()
Parameters
| Type | Name | Description |
|---|---|---|
| Action<T, TUserData> | onReceive | The delegate to be executed when the packet is received. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the packet. Must implement INetSerializable and have a new() constraint. |
| TUserData | The type of the user data (typically NetPeer). |
Remarks
To reduce allocations, this method uses a single internal reference to T for deserialization.
SubscribeNetSerializable<T, TUserData>(Action<T, TUserData>, Func<T>)
Registers a callback for a packet type that implements INetSerializable, using a custom constructor and supporting user data.
Declaration
public void SubscribeNetSerializable<T, TUserData>(Action<T, TUserData> onReceive, Func<T> packetConstructor) where T : INetSerializable
Parameters
| Type | Name | Description |
|---|---|---|
| Action<T, TUserData> | onReceive | The delegate to be executed when the packet is received. |
| Func<T> | packetConstructor | A function that returns a new instance of |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the packet. Must implement INetSerializable. |
| TUserData | The type of the user data (typically NetPeer). |
SubscribeReusable<T>(Action<T>)
Register and subscribe to packet receive event This method will overwrite last received packet class on receive (less garbage)
Declaration
public void SubscribeReusable<T>(Action<T> onReceive) where T : class, new()
Parameters
| Type | Name | Description |
|---|---|---|
| Action<T> | onReceive | event that will be called when packet deserialized with ReadPacket method |
Type Parameters
| Name | Description |
|---|---|
| T |
Exceptions
| Type | Condition |
|---|---|
| InvalidTypeException |
|
SubscribeReusable<T, TUserData>(Action<T, TUserData>)
Register and subscribe to packet receive event This method will overwrite last received packet class on receive (less garbage)
Declaration
public void SubscribeReusable<T, TUserData>(Action<T, TUserData> onReceive) where T : class, new()
Parameters
| Type | Name | Description |
|---|---|---|
| Action<T, TUserData> | onReceive | event that will be called when packet deserialized with ReadPacket method |
Type Parameters
| Name | Description |
|---|---|
| T | |
| TUserData |
Exceptions
| Type | Condition |
|---|---|
| InvalidTypeException |
|
Subscribe<T>(Action<T>, Func<T>)
Register and subscribe to packet receive event
Declaration
public void Subscribe<T>(Action<T> onReceive, Func<T> packetConstructor) where T : class, new()
Parameters
| Type | Name | Description |
|---|---|---|
| Action<T> | onReceive | event that will be called when packet deserialized with ReadPacket method |
| Func<T> | packetConstructor | Method that constructs packet instead of slow Activator.CreateInstance |
Type Parameters
| Name | Description |
|---|---|
| T |
Exceptions
| Type | Condition |
|---|---|
| InvalidTypeException |
|
Subscribe<T, TUserData>(Action<T, TUserData>, Func<T>)
Register and subscribe to packet receive event (with userData)
Declaration
public void Subscribe<T, TUserData>(Action<T, TUserData> onReceive, Func<T> packetConstructor) where T : class, new()
Parameters
| Type | Name | Description |
|---|---|---|
| Action<T, TUserData> | onReceive | event that will be called when packet deserialized with ReadPacket method |
| Func<T> | packetConstructor | Method that constructs packet instead of slow Activator.CreateInstance |
Type Parameters
| Name | Description |
|---|---|
| T | |
| TUserData |
Exceptions
| Type | Condition |
|---|---|
| InvalidTypeException |
|
WriteHash<T>(NetDataWriter)
Declaration
protected virtual void WriteHash<T>(NetDataWriter writer)
Parameters
| Type | Name | Description |
|---|---|---|
| NetDataWriter | writer |
Type Parameters
| Name | Description |
|---|---|
| T |
WriteNetSerializable<T>(NetDataWriter, ref T)
Declaration
public void WriteNetSerializable<T>(NetDataWriter writer, ref T packet) where T : INetSerializable
Parameters
| Type | Name | Description |
|---|---|---|
| NetDataWriter | writer | |
| T | packet |
Type Parameters
| Name | Description |
|---|---|
| T |
Write<T>(NetDataWriter, T)
Declaration
public void Write<T>(NetDataWriter writer, T packet) where T : class, new()
Parameters
| Type | Name | Description |
|---|---|---|
| NetDataWriter | writer | |
| T | packet |
Type Parameters
| Name | Description |
|---|---|
| T |