Skip to content

Commit

Permalink
will circle back on some of the pooling, slowly defining writers for …
Browse files Browse the repository at this point in the history
…each person
  • Loading branch information
dooly123 committed Jan 9, 2025
1 parent 55f31f8 commit 9f69990
Show file tree
Hide file tree
Showing 11 changed files with 278 additions and 246 deletions.
3 changes: 1 addition & 2 deletions Basis Server/BasisNetworkClient/BasisNetworkClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@ public static NetPeer StartClient(string IP, int port, ReadyMessage ReadyMessage
UnsyncedEvents = true,
};
client.Start();
NetDataWriter Writer = NetDataWriterPool.GetWriter();
NetDataWriter Writer = new NetDataWriter(true,12);
//this is the only time we dont put key!
Writer.Put(BasisNetworkVersion.ServerVersion);
AuthenticationMessage.Serialize(Writer);
ReadyMessage.Serialize(Writer);
peer = client.Connect(IP, port, Writer);
NetDataWriterPool.ReturnWriter(Writer);
return peer;
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,9 @@ private static void NetworkReceiveEvent(NetPeer peer, NetPacketReader Reader, by
ServerSideSyncPlayerMessage SSM = new ServerSideSyncPlayerMessage();
SSM.Deserialize(Reader);
Reader.Recycle();
NetDataWriter Writer = NetDataWriterPool.GetWriter();
NetDataWriter Writer = new NetDataWriter(true, 202);
SSM.avatarSerialization.Serialize(Writer);
LocalPLayer.Send(Writer, BasisNetworkCommons.MovementChannel, deliveryMethod);

NetDataWriterPool.ReturnWriter(Writer);
}
else
{
Expand Down
11 changes: 9 additions & 2 deletions Basis Server/BasisNetworkCore/BasisPlayerArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ public class BasisPlayerArray

// Reusable snapshot buffer
private static NetPeer[] SnapshotBuffer = new NetPeer[1024];

/// <summary>
/// slow operation but allows us to get a copy of the players quickly later.
/// slow operation can be out of order.
/// </summary>
/// <param name="player"></param>
public static void AddPlayer(NetPeer player)
{
lock (PlayerArrayLock)
Expand All @@ -22,7 +26,10 @@ public static void AddPlayer(NetPeer player)
}
}
}

/// <summary>
/// slow operation can be out of order.
/// </summary>
/// <param name="player"></param>
public static void RemovePlayer(NetPeer player)
{
lock (PlayerArrayLock)
Expand Down
141 changes: 0 additions & 141 deletions Basis Server/BasisNetworkCore/NetDataWriterPool.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public void Serialize(NetDataWriter Writer)
public struct VoiceReceiversMessage
{
public ushort[] users;

public void Deserialize(NetDataReader Writer)
{
// Calculate the number of ushorts based on the remaining bytes
Expand Down
55 changes: 55 additions & 0 deletions Basis Server/BasisNetworkServer/BasisNetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,63 @@ public static class BasisNetworkServer
public static EventBasedNetListener listener;
public static NetManager server;
public static ConcurrentDictionary<ushort, NetPeer> Peers = new ConcurrentDictionary<ushort, NetPeer>();
public static ChunkedNetPeerArray chunkedNetPeerArray = new ChunkedNetPeerArray();
public static Configuration Configuration;
public static IAuth auth;
public class ChunkedNetPeerArray
{
private readonly object[] _chunkLocks; // Locks for each chunk
private readonly NetPeer[][] _chunks; // Array divided into chunks
private readonly ushort _chunkSize; // Number of elements in each chunk
public const ushort totalSize = 1024; // Total size

public ChunkedNetPeerArray(ushort chunkSize = 256)
{
if (totalSize <= 0)
throw new ArgumentOutOfRangeException(nameof(totalSize), "Total size must be greater than zero.");
if (chunkSize <= 0)
throw new ArgumentOutOfRangeException(nameof(chunkSize), "Chunk size must be greater than zero.");

_chunkSize = chunkSize;
ushort numChunks = (ushort)Math.Ceiling((double)totalSize / chunkSize); // Now using ushort
_chunks = new NetPeer[numChunks][];
_chunkLocks = new object[numChunks];

for (ushort i = 0; i < numChunks; i++) // Changed to ushort
{
_chunks[i] = new NetPeer[chunkSize];
_chunkLocks[i] = new object();
}
}

public void SetPeer(ushort index, NetPeer value)
{
if (index < 0 || index >= _chunkSize * _chunks.Length)
throw new ArgumentOutOfRangeException(nameof(index), "Index is out of range.");

ushort chunkIndex = (ushort)(index / _chunkSize); // Changed to ushort
ushort localIndex = (ushort)(index % _chunkSize); // Changed to ushort

lock (_chunkLocks[chunkIndex])
{
_chunks[chunkIndex][localIndex] = value;
}
}

public NetPeer GetPeer(ushort index)
{
if (index < 0 || index >= _chunkSize * _chunks.Length)
throw new ArgumentOutOfRangeException(nameof(index), "Index is out of range.");

ushort chunkIndex = (ushort)(index / _chunkSize); // Changed to ushort
ushort localIndex = (ushort)(index % _chunkSize); // Changed to ushort

lock (_chunkLocks[chunkIndex])
{
return _chunks[chunkIndex][localIndex];
}
}
}
public static void StartServer(Configuration configuration)
{
Configuration = configuration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ public static void OwnershipResponse(NetPacketReader Reader, NetPeer Peer)
//the goal here is to make it so ownership understanding has to be requested.
//once a ownership has been requested there good for life or when a ownership switch happens.
NetworkRequestNewOrExisting(ownershipTransferMessage, out ushort currentOwner);
NetDataWriter Writer = NetDataWriterPool.GetWriter();
NetDataWriter Writer = new NetDataWriter(true, 2);
ownershipTransferMessage.playerIdMessage.playerID = currentOwner;
ownershipTransferMessage.Serialize(Writer);
BNL.Log("OwnershipResponse " + currentOwner + " for " + ownershipTransferMessage.playerIdMessage);
Peer.Send(Writer, BasisNetworkCommons.OwnershipResponse, DeliveryMethod.ReliableSequenced);
NetDataWriterPool.ReturnWriter(Writer);
}
catch (Exception ex)
{
Expand All @@ -50,7 +49,7 @@ public static void OwnershipTransfer(NetPacketReader Reader, NetPeer Peer)
Reader.Recycle();

ushort ClientId = (ushort)Peer.Id;
NetDataWriter Writer = NetDataWriterPool.GetWriter();
NetDataWriter Writer = new NetDataWriter(true, 2);
//all clients need to know about a ownership switch
if (SwitchOwnership(ownershipTransferMessage.ownershipID, ClientId))
{
Expand All @@ -69,7 +68,6 @@ public static void OwnershipTransfer(NetPacketReader Reader, NetPeer Peer)
ownershipTransferMessage.Serialize(Writer);
Peer.Send(Writer, BasisNetworkCommons.OwnershipTransfer, DeliveryMethod.ReliableSequenced);
}
NetDataWriterPool.ReturnWriter(Writer);
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using BasisNetworkCore;
using LiteNetLib;
using LiteNetLib.Utils;
using System.Collections.Concurrent;
using System.Collections.Generic;
using static SerializableBasis;

Expand All @@ -28,7 +27,7 @@ public static void HandleScene(NetPacketReader Reader, DeliveryMethod DeliveryMe
}
};
byte Channel = BasisNetworkCommons.SceneChannel;
NetDataWriter Writer = NetDataWriterPool.GetWriter();
NetDataWriter Writer = new NetDataWriter(true, 2);
if (DeliveryMethod == DeliveryMethod.Unreliable)
{
Writer.Put(Channel);
Expand All @@ -43,9 +42,10 @@ public static void HandleScene(NetPacketReader Reader, DeliveryMethod DeliveryMe
// BNL.Log("Query Recipients " + recipientsLength);
for (int index = 0; index < recipientsLength; index++)
{
if (BasisNetworkServer.Peers.TryGetValue(SceneDataMessage.recipients[index], out NetPeer client))
NetPeer client = BasisNetworkServer.chunkedNetPeerArray.GetPeer(SceneDataMessage.recipients[index]);
if (client != null)
{
BNL.Log("Found Peer! " + SceneDataMessage.recipients[index]);
// BNL.Log("Found Peer! " + SceneDataMessage.recipients[index]);
targetedClients.Add(client);
}
else
Expand All @@ -64,7 +64,6 @@ public static void HandleScene(NetPacketReader Reader, DeliveryMethod DeliveryMe
{
BasisNetworkServer.BroadcastMessageToClients(Writer, Channel, sender,BasisPlayerArray.GetSnapshot(), DeliveryMethod);
}
NetDataWriterPool.ReturnWriter(Writer);
}
public static void HandleAvatar(NetPacketReader Reader, DeliveryMethod DeliveryMethod, NetPeer sender)
{
Expand All @@ -85,7 +84,7 @@ public static void HandleAvatar(NetPacketReader Reader, DeliveryMethod DeliveryM
}
};
byte Channel = BasisNetworkCommons.AvatarChannel;
NetDataWriter Writer = NetDataWriterPool.GetWriter();
NetDataWriter Writer = new NetDataWriter(true, 2);
if (DeliveryMethod == DeliveryMethod.Unreliable)
{
Writer.Put(Channel);
Expand All @@ -100,14 +99,15 @@ public static void HandleAvatar(NetPacketReader Reader, DeliveryMethod DeliveryM
// BNL.Log("Query Recipients " + recipientsLength);
for (int index = 0; index < recipientsLength; index++)
{
if (BasisNetworkServer.Peers.TryGetValue(avatarDataMessage.recipients[index], out NetPeer client))
NetPeer client = BasisNetworkServer.chunkedNetPeerArray.GetPeer(avatarDataMessage.recipients[index]);
if (client != null)
{
// BNL.Log("Found Peer! " + avatarDataMessage.recipients[index]);
// BNL.Log("Found Peer! " + SceneDataMessage.recipients[index]);
targetedClients.Add(client);
}
else
{
BNL.LogError("Missing Peer! " + avatarDataMessage.recipients[index]);
BNL.Log("Missing Peer! " + avatarDataMessage.recipients[index]);
}
}

Expand All @@ -121,7 +121,6 @@ public static void HandleAvatar(NetPacketReader Reader, DeliveryMethod DeliveryM
{
BasisNetworkServer.BroadcastMessageToClients(Writer, Channel, sender, BasisPlayerArray.GetSnapshot(), DeliveryMethod);
}
NetDataWriterPool.ReturnWriter(Writer);
}
}
}
Loading

0 comments on commit 9f69990

Please sign in to comment.