Skip to content

Commit

Permalink
add List<T> support for NetSerializer/PacketProcessor for custom type…
Browse files Browse the repository at this point in the history
…s. Improved PacketProcessorExapmle
  • Loading branch information
RevenantX committed Aug 2, 2020
1 parent 87bc3c7 commit 6c2fa5a
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 32 deletions.
59 changes: 50 additions & 9 deletions LibSample/PacketProcessorExample.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
using System;
using System.Collections.Generic;
using System.Threading;
using LiteNetLib;
using LiteNetLib.Utils;

namespace LibSample
{
struct CustomStruct : INetSerializable
{
public int X;
public int Y;

public void Serialize(NetDataWriter writer)
{
writer.Put(X);
writer.Put(Y);
}

public void Deserialize(NetDataReader reader)
{
X = reader.GetInt();
Y = reader.GetInt();
}
}

class ArgumentsForLogin
{
public string UserId { get; set; }
public string Password { get; set; }
public int SomeInt { get; set; }
public List<CustomStruct> SomeList { get; set; }
}

class PacketProcessorExample
Expand All @@ -21,31 +41,47 @@ class PacketProcessorExample
public void Run()
{
//setup netpacketprocessor
_netPacketProcessor.RegisterNestedType<CustomStruct>();
_netPacketProcessor.SubscribeReusable<ArgumentsForLogin, NetPeer>(Method1);

//setup events
EventBasedNetListener clientListener = new EventBasedNetListener();
EventBasedNetListener serverListener = new EventBasedNetListener();
serverListener.ConnectionRequestEvent += request => request.AcceptIfKey("key");
serverListener.ConnectionRequestEvent += request =>
{
request.AcceptIfKey("key");
};
serverListener.NetworkReceiveEvent +=
(peer, reader, method) => _netPacketProcessor.ReadAllPackets(reader, peer);
(peer, reader, method) =>
{
_netPacketProcessor.ReadAllPackets(reader, peer);
};
clientListener.PeerConnectedEvent += peer =>
{
//send after connect
var testList = new List<CustomStruct>
{
new CustomStruct {X = 1, Y = -1},
new CustomStruct {X = 5, Y = -28},
new CustomStruct {X = -114, Y = 65535}
};
_netPacketProcessor.Send(
peer,
new ArgumentsForLogin {Password = "pass", SomeInt = 5, UserId = "someUser", SomeList = testList},
DeliveryMethod.ReliableOrdered);
};

//start client/server
_client = new NetManager(clientListener);
_server = new NetManager(serverListener);
_client.Start();
_server.Start(9050);
var clientPeer = _client.Connect("localhost", 9050, "key");

//send
_netPacketProcessor.Send(
clientPeer,
new ArgumentsForLogin { Password = "pass", SomeInt = 5, UserId = "someUser"},
DeliveryMethod.ReliableOrdered);
_client.Connect("localhost", 9050, "key");

while (!Console.KeyAvailable)
{
_server.PollEvents();
_client.PollEvents();
Thread.Sleep(10);
}
_client.Stop();
Expand All @@ -55,6 +91,11 @@ public void Run()
void Method1(ArgumentsForLogin args, NetPeer peer)
{
Console.WriteLine("Received: \n {0}\n {1}\n {2}", args.UserId, args.Password, args.SomeInt);
Console.WriteLine("List count: " + args.SomeList.Count);
for (int i = 0; i < args.SomeList.Count; i++)
{
Console.WriteLine($" X: {args.SomeList[i].X}, Y: {args.SomeList[i].Y}");
}
}
}
}
35 changes: 32 additions & 3 deletions LiteNetLib.Tests/NetSerializerTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using LiteNetLib.Utils;
using System.Collections.Generic;
using LiteNetLib.Utils;

using NUnit.Framework;

Expand All @@ -22,7 +23,8 @@ public void Init()
SomeByteArray = new byte[] { 255, 1, 0 },
TestObj = new SampleNetSerializable {Value = 5},
TestArray = new [] { new SampleNetSerializable { Value = 6 }, new SampleNetSerializable { Value = 15 } },
SampleClassArray = new[] { new SampleClass { Value = 6 }, new SampleClass { Value = 15 } }
SampleClassArray = new[] { new SampleClass { Value = 6 }, new SampleClass { Value = 15 } },
SampleClassList = new List<SampleClass> { new SampleClass { Value = 1 }, new SampleClass { Value = 5 }}
};

_packetProcessor = new NetPacketProcessor();
Expand Down Expand Up @@ -120,6 +122,7 @@ private class SamplePacket
public SampleNetSerializable TestObj { get; set; }
public SampleNetSerializable[] TestArray { get; set; }
public SampleClass[] SampleClassArray { get; set; }
public List<SampleClass> SampleClassList { get; set; }
}

private static bool AreSame(string s1, string s2)
Expand All @@ -137,7 +140,7 @@ public void CustomPackageTest()
var writer = new NetDataWriter();
_packetProcessor.Write(writer, _samplePacket);

var reader = new NetDataReader(writer.CopyData());
var reader = new NetDataReader(writer);
SamplePacket readPackage = null;

_packetProcessor.SubscribeReusable<SamplePacket>(
Expand All @@ -160,6 +163,32 @@ public void CustomPackageTest()
Assert.AreEqual(_samplePacket.TestArray, readPackage.TestArray);
Assert.AreEqual(_samplePacket.SomeByteArray, readPackage.SomeByteArray);
Assert.AreEqual(_samplePacket.SampleClassArray, readPackage.SampleClassArray);
CollectionAssert.AreEqual(_samplePacket.SampleClassList, readPackage.SampleClassList);

//remove test
_samplePacket.SampleClassList.RemoveAt(0);
_samplePacket.SampleClassArray = new []{new SampleClass {Value = 1}};

writer.Reset();
_packetProcessor.Write(writer, _samplePacket);
reader.SetSource(writer);
_packetProcessor.ReadAllPackets(reader);

Assert.AreEqual(_samplePacket.SampleClassArray, readPackage.SampleClassArray);
CollectionAssert.AreEqual(_samplePacket.SampleClassList, readPackage.SampleClassList);

//add test
_samplePacket.SampleClassList.Add(new SampleClass { Value = 152 });
_samplePacket.SampleClassList.Add(new SampleClass { Value = 154 });
_samplePacket.SampleClassArray = new[] { new SampleClass { Value = 1 }, new SampleClass { Value = 2 }, new SampleClass { Value = 3 } };

writer.Reset();
_packetProcessor.Write(writer, _samplePacket);
reader.SetSource(writer);
_packetProcessor.ReadAllPackets(reader);

Assert.AreEqual(_samplePacket.SampleClassArray, readPackage.SampleClassArray);
CollectionAssert.AreEqual(_samplePacket.SampleClassList, readPackage.SampleClassList);
}
}
}
5 changes: 5 additions & 0 deletions LiteNetLib/Utils/NetDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ public NetDataReader()

}

public NetDataReader(NetDataWriter writer)
{
SetSource(writer);
}

public NetDataReader(byte[] source)
{
SetSource(source);
Expand Down
Loading

0 comments on commit 6c2fa5a

Please sign in to comment.