optimizing network stuff
This commit is contained in:
@@ -7,7 +7,7 @@ using UnityEngine.Pool;
|
||||
namespace RebootKit.Engine.Network {
|
||||
public struct NetworkBufferReader : IDisposable {
|
||||
class ReaderHandle {
|
||||
public NativeArray<byte> Data;
|
||||
public NativeSlice<byte> Data;
|
||||
public int Position;
|
||||
public bool IsBigEndian;
|
||||
}
|
||||
@@ -27,8 +27,7 @@ namespace RebootKit.Engine.Network {
|
||||
|
||||
ReaderHandle m_Handle;
|
||||
|
||||
public NetworkBufferReader(NativeArray<byte> data, int position = 0) {
|
||||
Assert.IsTrue(data.IsCreated, "Trying to create a NetworkBufferReader with uncreated data.");
|
||||
public NetworkBufferReader(NativeSlice<byte> data, int position = 0) {
|
||||
Assert.IsTrue(position >= 0 && position <= data.Length,
|
||||
"Position must be within the bounds of the data array.");
|
||||
|
||||
@@ -53,6 +52,11 @@ namespace RebootKit.Engine.Network {
|
||||
Assert.IsTrue(HasNext(size),
|
||||
$"Not enough data to read the requested size. Requested: {size}, Available: {m_Handle.Data.Length - m_Handle.Position}");
|
||||
|
||||
if (size <= 0) {
|
||||
value = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
value = new NativeArray<byte>(size, allocator);
|
||||
for (int i = 0; i < size; i++) {
|
||||
value[i] = m_Handle.Data[m_Handle.Position++];
|
||||
@@ -61,6 +65,17 @@ namespace RebootKit.Engine.Network {
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Read(out NativeSlice<byte> value, int size) {
|
||||
if (!HasNext(size)) {
|
||||
value = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
value = m_Handle.Data.Slice(m_Handle.Position, size);
|
||||
m_Handle.Position += size;
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Read(out byte value) {
|
||||
if (!HasNext(1)) {
|
||||
value = 0;
|
||||
|
||||
Reference in New Issue
Block a user