optimizing network stuff

This commit is contained in:
2025-07-30 05:51:39 +02:00
parent ea99249fe2
commit 159e9adcd7
56 changed files with 2272 additions and 1781 deletions

View File

@@ -1,4 +1,5 @@
using System;
using System.Text;
using Unity.Collections;
using UnityEngine;
using UnityEngine.Assertions;
@@ -179,7 +180,7 @@ namespace RebootKit.Engine.Network {
public void Write(ulong value) {
Assert.IsTrue(sizeof(ulong) == 8, "Size of ulong must be 8 bytes.");
Assert.IsTrue(WillFit(sizeof(ulong)), "Buffer overflow: Cannot write beyond capacity.");
Assert.IsTrue(WillFit(sizeof(ulong)), $"Buffer overflow: Cannot write beyond capacity. Current position: {m_Handle.Position}, Capacity: {m_Handle.Capacity}");
if (BitConverter.IsLittleEndian) {
Write((byte) (value & 0xFF));
@@ -312,5 +313,43 @@ namespace RebootKit.Engine.Network {
}
}
}
// @NOTE: Writes ascii characters as bytes, and prepends the length as a ushort.
public static int GetStringWriteLength(string value) {
if (value.Length > ushort.MaxValue) {
return sizeof(ushort);
}
if (string.IsNullOrEmpty(value)) {
return sizeof(ushort);
}
// Length prefix
int length = sizeof(ushort);
for (int i = 0; i < value.Length; i++) {
length += sizeof(char);
}
return length;
}
public void Write(string value) {
if (value.Length > ushort.MaxValue) {
Write((ushort) 0);
return;
}
if (string.IsNullOrEmpty(value)) {
Write((ushort) 0);
return;
}
Write((ushort)value.Length);
for (int i = 0; i < value.Length; i++) {
Write((byte)value[i]);
}
}
}
}