optimizing network stuff
This commit is contained in:
@@ -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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user