Skip to content

Commit

Permalink
Merge pull request #16 from RevenantX/master
Browse files Browse the repository at this point in the history
Update to latest litenetlib 1.0.1. Use interface instead of EventBased listener which speedups things alot.
  • Loading branch information
JohannesDeml authored Apr 2, 2023
2 parents 7c7740b + 456c5b6 commit 505005a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 59 deletions.
35 changes: 12 additions & 23 deletions NetworkBenchmarkDotNet/Libraries/LiteNetLib/EchoClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace NetworkBenchmark.LiteNetLib
{
internal class EchoClient : AClient, IClient
internal class EchoClient : AClient, IClient, INetEventListener
{
public override bool IsConnected => isConnected;
public override bool IsDisposed => isDisposed;
Expand All @@ -27,7 +27,6 @@ internal class EchoClient : AClient, IClient
private readonly Configuration config;
private readonly BenchmarkStatistics benchmarkStatistics;

private readonly EventBasedNetListener listener;
private readonly NetManager netManager;
private readonly DeliveryMethod deliveryMethod;
private NetPeer peer;
Expand All @@ -39,28 +38,19 @@ public EchoClient(int id, Configuration config, BenchmarkStatistics benchmarkSta
this.benchmarkStatistics = benchmarkStatistics;
deliveryMethod = LiteNetLibBenchmark.GetDeliveryMethod(config.Transmission);

listener = new EventBasedNetListener();
netManager = new NetManager(listener);
netManager = new NetManager(this);
if (!config.Address.Contains(':'))
{
// For LiteNetLib 1.0 and above
//netManager.IPv6Mode = IPv6Mode.Disabled;

// LiteNetLib up to 0.9.4
netManager.IPv6Enabled = IPv6Mode.Disabled;
netManager.IPv6Mode = IPv6Mode.Disabled;
}

//netManager.UseNativeSockets = true;
netManager.UpdateTime = Utilities.CalculateTimeout(config.ClientTickRate);
netManager.UnsyncedEvents = true;
netManager.DisconnectTimeout = 10000;

isConnected = false;
isDisposed = false;

listener.PeerConnectedEvent += OnPeerConnected;
listener.PeerDisconnectedEvent += OnPeerDisconnected;
listener.NetworkReceiveEvent += OnNetworkReceive;
listener.NetworkErrorEvent += OnNetworkError;
}

public override void StartClient()
Expand Down Expand Up @@ -101,11 +91,6 @@ public override void StopClient()

public override void Dispose()
{
listener.PeerConnectedEvent -= OnPeerConnected;
listener.PeerDisconnectedEvent -= OnPeerDisconnected;
listener.NetworkReceiveEvent -= OnNetworkReceive;
listener.NetworkErrorEvent -= OnNetworkError;

isDisposed = true;
}

Expand Down Expand Up @@ -136,12 +121,12 @@ private void Send(byte[] bytes, DeliveryMethod delivery)
Interlocked.Increment(ref benchmarkStatistics.MessagesClientSent);
}

private void OnPeerConnected(NetPeer peer)
void INetEventListener.OnPeerConnected(NetPeer peer)
{
isConnected = true;
}

private void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)
void INetEventListener.OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)
{
if (disconnectInfo.Reason == DisconnectReason.Timeout && (BenchmarkRunning || BenchmarkPreparing))
{
Expand All @@ -153,7 +138,7 @@ private void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)
isConnected = false;
}

private void OnNetworkReceive(NetPeer peer, NetPacketReader reader, DeliveryMethod deliverymethod)
void INetEventListener.OnNetworkReceive(NetPeer peer, NetPacketReader reader, byte channelNumber, DeliveryMethod deliverymethod)
{
if (BenchmarkRunning)
{
Expand All @@ -168,7 +153,11 @@ private void OnNetworkReceive(NetPeer peer, NetPacketReader reader, DeliveryMeth
reader.Recycle();
}

private void OnNetworkError(IPEndPoint endpoint, SocketError socketerror)
void INetEventListener.OnNetworkReceiveUnconnected(IPEndPoint remoteEndPoint, NetPacketReader reader, UnconnectedMessageType messageType) { }
void INetEventListener.OnNetworkLatencyUpdate(NetPeer peer, int latency) { }
void INetEventListener.OnConnectionRequest(ConnectionRequest request) { }

void INetEventListener.OnNetworkError(IPEndPoint endpoint, SocketError socketerror)
{
if (BenchmarkRunning)
{
Expand Down
57 changes: 22 additions & 35 deletions NetworkBenchmarkDotNet/Libraries/LiteNetLib/EchoServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@

namespace NetworkBenchmark.LiteNetLib
{
internal class EchoServer : AServer
internal class EchoServer : AServer, INetEventListener
{
public override bool IsStarted => netManager != null && netManager.IsRunning;

private readonly Configuration config;
private readonly BenchmarkStatistics benchmarkStatistics;
private readonly EventBasedNetListener listener;
private readonly NetManager netManager;
private readonly DeliveryMethod deliveryMethod;

Expand All @@ -32,24 +31,15 @@ public EchoServer(Configuration config, BenchmarkStatistics benchmarkStatistics)
this.benchmarkStatistics = benchmarkStatistics;
deliveryMethod = LiteNetLibBenchmark.GetDeliveryMethod(config.Transmission);

listener = new EventBasedNetListener();
netManager = new NetManager(listener);
netManager = new NetManager(this);
netManager.UpdateTime = Utilities.CalculateTimeout(config.ServerTickRate);
//netManager.UseNativeSockets = true;
if (!config.Address.Contains(':'))
{
// For LiteNetLib 1.0 and above
//netManager.IPv6Mode = IPv6Mode.Disabled;

// LiteNetLib up to 0.9.4
netManager.IPv6Enabled = IPv6Mode.Disabled;
netManager.IPv6Mode = IPv6Mode.Disabled;
}

netManager.UnsyncedEvents = true;

listener.ConnectionRequestEvent += OnConnectionRequest;
listener.NetworkReceiveEvent += OnNetworkReceive;
listener.NetworkErrorEvent += OnNetworkError;
listener.PeerDisconnectedEvent += OnPeerDisconnected;
}

public override void StartServer()
Expand All @@ -64,13 +54,7 @@ public override void StopServer()
netManager.Stop();
}

public override void Dispose()
{
listener.ConnectionRequestEvent -= OnConnectionRequest;
listener.NetworkReceiveEvent -= OnNetworkReceive;
listener.NetworkErrorEvent -= OnNetworkError;
listener.PeerDisconnectedEvent -= OnPeerDisconnected;
}
public override void Dispose() { }

#region ManualMode

Expand All @@ -87,7 +71,7 @@ public override void SendMessages(int messageCount, TransmissionType transmissio

#endregion

private void OnConnectionRequest(ConnectionRequest request)
void INetEventListener.OnConnectionRequest(ConnectionRequest request)
{
if (netManager.ConnectedPeerList.Count > config.Clients)
{
Expand All @@ -98,8 +82,19 @@ private void OnConnectionRequest(ConnectionRequest request)

request.Accept();
}
private void Broadcast(byte[] data, DeliveryMethod delivery)
{
netManager.SendToAll(data, delivery);
var messagesSent = netManager.ConnectedPeersCount;
Interlocked.Add(ref benchmarkStatistics.MessagesServerSent, messagesSent);
}

void INetEventListener.OnNetworkError(IPEndPoint endPoint, SocketError socketError)
{
Interlocked.Increment(ref benchmarkStatistics.Errors);
}

private void OnNetworkReceive(NetPeer peer, NetPacketReader reader, DeliveryMethod clientDeliveryMethod)
void INetEventListener.OnNetworkReceive(NetPeer peer, NetPacketReader reader, byte channelNumber, DeliveryMethod deliveryMethod)
{
if (benchmarkRunning)
{
Expand All @@ -116,19 +111,11 @@ private void OnNetworkReceive(NetPeer peer, NetPacketReader reader, DeliveryMeth
reader.Recycle();
}

private void Broadcast(byte[] data, DeliveryMethod delivery)
{
netManager.SendToAll(data, delivery);
var messagesSent = netManager.ConnectedPeersCount;
Interlocked.Add(ref benchmarkStatistics.MessagesServerSent, messagesSent);
}

private void OnNetworkError(IPEndPoint endpoint, SocketError socketerror)
{
Interlocked.Increment(ref benchmarkStatistics.Errors);
}
void INetEventListener.OnNetworkReceiveUnconnected(IPEndPoint remoteEndPoint, NetPacketReader reader, UnconnectedMessageType messageType) { }
void INetEventListener.OnNetworkLatencyUpdate(NetPeer peer, int latency) { }
void INetEventListener.OnPeerConnected(NetPeer peer) { }

private void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectinfo)
void INetEventListener.OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectinfo)
{
if (benchmarkPreparing || benchmarkRunning)
{
Expand Down
2 changes: 1 addition & 1 deletion NetworkBenchmarkDotNet/NetworkBenchmarkDotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
<PackageReference Include="ENet-CSharp" Version="2.4.7" />
<PackageReference Include="LiteNetLib" Version="0.9.5.2" />
<PackageReference Include="LiteNetLib" Version="1.0.1" />
<PackageReference Include="NetCoreServer" Version="5.1.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20574.7" />
</ItemGroup>
Expand Down

0 comments on commit 505005a

Please sign in to comment.