Skip to content

Commit

Permalink
ton of work to cache arrays of data for recycling
Browse files Browse the repository at this point in the history
  • Loading branch information
dooly123 committed Jan 28, 2025
1 parent 95f365a commit d9242d7
Show file tree
Hide file tree
Showing 38 changed files with 294 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ public void Deserialize(NetDataReader Writer)
messageIndex = Writer.GetByte();

byte PayloadSize = Writer.GetByte();
array = new byte[PayloadSize];
if (array == null || array.Length != PayloadSize)
{
array = new byte[PayloadSize];
}
Writer.GetBytes(array, PayloadSize);
//89 * 2 = 178 + 12 + 14 = 204
//now 178 for muscles, 3*4 for position 12, 4*4 for rotation 16-2 (W is half) = 204
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@ public void Deserialize(NetDataReader Writer)
}
else
{
buffer = Writer.GetRemainingBytes();
LengthUsed = buffer.Length;
if (buffer != null && buffer.Length == Writer.AvailableBytes)
{
Writer.GetBytes(buffer, Writer.AvailableBytes);
LengthUsed = buffer.Length;
}
else
{
buffer = Writer.GetRemainingBytes();
LengthUsed = buffer.Length;
}
// BNL.Log("Get Length was " + LengthUsed);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ public void Deserialize(NetDataReader Reader)
{
if (Reader.TryGetUShort(out ushort msgLength))
{
bytes = new byte[msgLength];
if (bytes == null || bytes.Length != msgLength)
{
bytes = new byte[msgLength];
}
Reader.GetBytes(bytes, msgLength);
}
else
Expand Down
14 changes: 12 additions & 2 deletions Basis Server/BasisNetworkCore/Serializable/AvatarDataMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ public void Deserialize(NetDataReader Writer)
{
throw new ArgumentException($"Invalid recipientsSize: {recipientsSize}");
}
recipients = new ushort[recipientsSize];
if (recipients == null || recipients.Length != recipientsSize)
{
recipients = new ushort[recipientsSize];
}
// BNL.Log("Recipients is " + recipientsSize);
for (int index = 0; index < recipientsSize; index++)
{
Expand All @@ -42,7 +45,14 @@ public void Deserialize(NetDataReader Writer)
// Read remaining bytes as payload
if (Writer.AvailableBytes > 0)
{
payload = Writer.GetRemainingBytes();
if (payload != null && payload.Length == Writer.AvailableBytes)
{
Writer.GetBytes(payload, Writer.AvailableBytes);
}
else
{
payload = Writer.GetRemainingBytes();
}
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ public void Deserialize(NetDataReader Writer)
{
throw new ArgumentException($"Invalid recipientsSize: {payloadSize}");
}
payload = new byte[payloadSize];
if (payload == null || payload.Length != payloadSize)
{
payload = new byte[payloadSize];
}
if (!Writer.TryGetBytesWithLength(out payload))
{
throw new ArgumentException($"Failed to read payload!.");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using BasisNetworkCore;
using LiteNetLib.Utils;
using System.Collections.Generic;
public static partial class SerializableBasis
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
using LiteNetLib.Utils;
using static SerializableBasis;

namespace DarkRift.Basis_Common.Serializable
{
public struct OwnershipTransferMessage
public static partial class SerializableBasis
{
public PlayerIdMessage playerIdMessage;
public string ownershipID;
public void Deserialize(NetDataReader Writer)
public struct OwnershipTransferMessage
{
playerIdMessage.Deserialize(Writer);
Writer.Get(out ownershipID);
}
public void Serialize(NetDataWriter Writer)
{
playerIdMessage.Serialize(Writer);
Writer.Put(ownershipID);
public PlayerIdMessage playerIdMessage;
public string ownershipID;
public void Deserialize(NetDataReader Writer)
{
playerIdMessage.Deserialize(Writer);
Writer.Get(out ownershipID);
}
public void Serialize(NetDataWriter Writer)
{
playerIdMessage.Serialize(Writer);
Writer.Put(ownershipID);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@

using DarkRift;
using LiteNetLib.Utils;

public static partial class SerializableBasis
{
public struct PlayerMetaDataMessage
Expand Down
1 change: 0 additions & 1 deletion Basis Server/BasisNetworkCore/Serializable/ReadyMessage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using LiteNetLib.Utils;
using static BasisNetworkCore.Serializable.SerializableBasis;
public static partial class SerializableBasis
{
public struct ReadyMessage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ public struct RemoteAvatarDataMessage
public PlayerIdMessage PlayerIdMessage;
public byte messageIndex;
public byte[] payload;

public void Deserialize(NetDataReader Writer)
{
PlayerIdMessage.Deserialize(Writer);
Expand All @@ -16,19 +15,29 @@ public void Deserialize(NetDataReader Writer)
{
throw new ArgumentException("Failed to read messageIndex.");
}
if (Writer.AvailableBytes > 0)
if (Writer.AvailableBytes != 0)
{
payload = Writer.GetRemainingBytes();
if (payload != null && payload.Length == Writer.AvailableBytes)
{
Writer.GetBytes(payload, Writer.AvailableBytes);
}
else
{
payload = Writer.GetRemainingBytes();
}
}
else
{
payload = null;
}
}

public void Serialize(NetDataWriter Writer)
{
PlayerIdMessage.Serialize(Writer);
// Write the messageIndex
Writer.Put(messageIndex);
// Write the payload if present
if (payload != null && payload.Length > 0)
if (payload != null && payload.Length != 0)
{
Writer.Put(payload);
}
Expand Down
14 changes: 12 additions & 2 deletions Basis Server/BasisNetworkCore/Serializable/SceneDataMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ public void Deserialize(NetDataReader Writer)
{
throw new ArgumentException($"Invalid recipientsSize: {recipientsSize}");
}
recipients = new ushort[recipientsSize];
if (recipients == null || recipients.Length != recipientsSize)
{
recipients = new ushort[recipientsSize];
}
for (int index = 0; index < recipientsSize; index++)
{
if (!Writer.TryGetUShort(out recipients[index]))
Expand All @@ -40,7 +43,14 @@ public void Deserialize(NetDataReader Writer)
// Read remaining bytes as payload
if (Writer.AvailableBytes > 0)
{
payload = Writer.GetRemainingBytes();
if (payload != null && payload.Length == Writer.AvailableBytes)
{
Writer.GetBytes(payload, Writer.AvailableBytes);
}
else
{
payload = Writer.GetRemainingBytes();
}
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ public void Deserialize(NetDataReader Writer)
int ushortCount = remainingBytes / sizeof(ushort);

// Initialize the array with the calculated size
users = new ushort[ushortCount];

if (users == null || users.Length != ushortCount)
{
users = new ushort[ushortCount];
}
// Read each ushort value into the array
for (int index = 0; index < ushortCount; index++)
{
Expand All @@ -37,10 +39,13 @@ public void Deserialize(NetDataReader Writer)
}
public void Serialize(NetDataWriter Writer)
{
int Count = users.Length;
for (int Index = 0; Index < Count; Index++)
if (users != null)
{
Writer.Put(users[Index]);
int Count = users.Length;
for (int Index = 0; Index < Count; Index++)
{
Writer.Put(users[Index]);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ public void Deserialize(NetDataReader Writer)
// Read the load mode
loadMode = Writer.GetByte();
// Initialize the byte array with the specified length
byteArray = new byte[Writer.GetUShort()];
ushort Length = Writer.GetUShort();
if (byteArray == null || byteArray.Length != Length)
{
byteArray = new byte[Length];
}

// Read each byte manually into the array
Writer.GetBytes(byteArray, 0, byteArray.Length);
Expand All @@ -44,11 +48,6 @@ public void Deserialize(NetDataReader Writer)
}
*/
}

public void Dispose()
{
}

public void Serialize(NetDataWriter Writer)
{
// Write the load mode
Expand Down
27 changes: 15 additions & 12 deletions Basis Server/BasisNetworkCore/Serializable/ServerUniqueIDMessage.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using LiteNetLib.Utils;
using System;

namespace BasisNetworkCore.Serializable
{
public static partial class SerializableBasis
Expand Down Expand Up @@ -34,7 +32,7 @@ public void Deserialize(NetDataReader reader)
}
else
{
Console.Error.WriteLine($"Unable to read remaining bytes: {bytes}");
BNL.LogError($"Unable to read remaining bytes: {bytes}");
}
}

Expand All @@ -46,7 +44,7 @@ public void Serialize(NetDataWriter writer)
}
else
{
Console.Error.WriteLine("Unable to serialize. Field was null or empty.");
BNL.LogError("Unable to serialize. Field was null or empty.");
}
}
}
Expand All @@ -64,7 +62,7 @@ public void Deserialize(NetDataReader reader)
}
else
{
Console.Error.WriteLine($"Unable to read remaining bytes: {bytes}");
BNL.LogError($"Unable to read remaining bytes: {bytes}");
}
}

Expand All @@ -85,16 +83,20 @@ public void Deserialize(NetDataReader reader)
if (bytes >= sizeof(ushort))
{
MessageCount = reader.GetUShort();
Messages = new ServerNetIDMessage[MessageCount];
for (int i = 0; i < MessageCount; i++)
if (Messages == null || Messages.Length != MessageCount)
{
Messages = new ServerNetIDMessage[MessageCount];
}
for (int Index = 0; Index < MessageCount; Index++)
{
Messages[i] = new ServerNetIDMessage();
Messages[i].Deserialize(reader);
Messages[Index] = new ServerNetIDMessage();
Messages[Index].Deserialize(reader);
}
}
else
{
Console.Error.WriteLine($"Unable to read remaining bytes for MessageCount. Available: {bytes}");
Messages = null;
BNL.LogError($"Unable to read remaining bytes for MessageCount. Available: {bytes}");
}
}

Expand All @@ -103,14 +105,15 @@ public void Serialize(NetDataWriter writer)
if (Messages != null)
{
writer.Put((ushort)Messages.Length);
foreach (var message in Messages)
for (int Index = 0; Index < Messages.Length; Index++)
{
ServerNetIDMessage message = Messages[Index];
message.Serialize(writer);
}
}
else
{
Console.Error.WriteLine("Unable to serialize. Messages array was null.");
BNL.LogError("Unable to serialize. Messages array was null.");
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions Basis Server/BasisNetworkCore/ThreadSafeMessagePool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Collections.Concurrent;

namespace BasisNetworkCore
{
public static class ThreadSafeMessagePool<T> where T : new()
{
private static readonly ConcurrentStack<T> pool = new ConcurrentStack<T>();
private static readonly int maxPoolSize = 500; // Maximum allowed size of the pool

public static T Rent()
{
if (pool.TryPop(out T obj))
{
return obj;
}
return new T();
}

public static void Return(T obj)
{
if (pool.Count < maxPoolSize) // Check if the pool size is within the limit
{
pool.Push(obj);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Basis.Network.Core;
using BasisNetworkCore;
using DarkRift.Basis_Common.Serializable;
using LiteNetLib;
using LiteNetLib.Utils;
using System.Collections.Concurrent;
using System.Collections.Generic;
using static DarkRift.Basis_Common.Serializable.SerializableBasis;
namespace Basis.Network.Server.Ownership
{
public static class BasisNetworkOwnership
Expand Down
Loading

0 comments on commit d9242d7

Please sign in to comment.