Skip to content

Commit

Permalink
Fix packets arriving corrupted after update
Browse files Browse the repository at this point in the history
LiteNetLib was forcing the maximum size of an array to that of a ushort.
Reousa committed Dec 22, 2023

Verified

This commit was signed with the committer’s verified signature.
tprrt Thomas Perrot
1 parent 215e75c commit 6c5d18a
Showing 4 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion NebulaAPI/Interfaces/INetDataReader.cs
Original file line number Diff line number Diff line change
@@ -68,7 +68,7 @@ int AvailableBytes
IPEndPoint GetNetEndPoint();
byte GetByte();
sbyte GetSByte();
T[] GetArray<T>(ushort size);
T[] GetArray<T>(int size);
bool[] GetBoolArray();
ushort[] GetUShortArray();
short[] GetShortArray();
4 changes: 2 additions & 2 deletions NebulaAPI/Interfaces/INetDataWriter.cs
Original file line number Diff line number Diff line change
@@ -66,9 +66,9 @@ int Length
void Put(string value, int maxLength);

void Put<T>(T obj) where T : INetSerializable;
void PutSBytesWithLength(sbyte[] data, int offset, ushort length);
void PutSBytesWithLength(sbyte[] data, int offset, int length);
void PutSBytesWithLength(sbyte[] data);
void PutBytesWithLength(byte[] data, int offset, ushort length);
void PutBytesWithLength(byte[] data, int offset, int length);
void PutBytesWithLength(byte[] data);
void PutArray(Array arr, int sz);
void PutArray(float[] value);
12 changes: 6 additions & 6 deletions NebulaModel/Networking/Serialization/NetDataReader.cs
Original file line number Diff line number Diff line change
@@ -127,10 +127,10 @@ public sbyte GetSByte()
return (sbyte)GetByte();
}

public T[] GetArray<T>(ushort size)
public T[] GetArray<T>(int size)
{
ushort length = BitConverter.ToUInt16(_data, _position);
_position += 2;
int length = BitConverter.ToInt32(_data, _position);
_position += 4;
T[] result = new T[length];
length *= size;
Buffer.BlockCopy(_data, _position, result, 0, length);
@@ -650,10 +650,10 @@ public bool TryGetStringArray(out string[] result)

public bool TryGetBytesWithLength(out byte[] result)
{
if (AvailableBytes >= 2)
if (AvailableBytes >= 4)
{
ushort length = PeekUShort();
if (length >= 0 && AvailableBytes >= 2 + length)
int length = PeekInt();
if (length >= 0 && AvailableBytes >= 4 + length)
{
result = GetBytesWithLength();
return true;
26 changes: 15 additions & 11 deletions NebulaModel/Networking/Serialization/NetDataWriter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Net;
using System.Runtime.CompilerServices;
using System.Text;
@@ -19,11 +19,13 @@ public int Capacity
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _data.Length;
}

public byte[] Data
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _data;
}

public int Length
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -233,21 +235,22 @@ public void Put(byte[] data)
_position += data.Length;
}

public void PutSBytesWithLength(sbyte[] data, int offset, ushort length)
public void PutSBytesWithLength(sbyte[] data, int offset, int length)
{
const int intSize = 4;
if (_autoResize)
ResizeIfNeed(_position + 2 + length);
ResizeIfNeed(_position + intSize + length);
FastBitConverter.GetBytes(_data, _position, length);
Buffer.BlockCopy(data, offset, _data, _position + 2, length);
_position += 2 + length;
Buffer.BlockCopy(data, offset, _data, _position + intSize, length);
_position += intSize + length;
}

public void PutSBytesWithLength(sbyte[] data)
{
PutArray(data, 1);
}

public void PutBytesWithLength(byte[] data, int offset, ushort length)
public void PutBytesWithLength(byte[] data, int offset, int length)
{
if (_autoResize)
ResizeIfNeed(_position + 2 + length);
@@ -268,14 +271,15 @@ public void Put(bool value)

public void PutArray(Array arr, int sz)
{
ushort length = arr == null ? (ushort) 0 : (ushort)arr.Length;
const int intSize = 4;
int length = arr == null ? 0 : arr.Length;
sz *= length;
if (_autoResize)
ResizeIfNeed(_position + sz + 2);
ResizeIfNeed(_position + sz + intSize);
FastBitConverter.GetBytes(_data, _position, length);
if (arr != null)
Buffer.BlockCopy(arr, 0, _data, _position + 2, sz);
_position += sz + 2;
Buffer.BlockCopy(arr, 0, _data, _position + intSize, sz);
_position += sz + intSize;
}

public void PutArray(float[] value)
@@ -325,7 +329,7 @@ public void PutArray(bool[] value)

public void PutArray(string[] value)
{
ushort strArrayLength = value == null ? (ushort)0 : (ushort)value.Length;
int strArrayLength = value == null ? 0 : value.Length;
Put(strArrayLength);
for (int i = 0; i < strArrayLength; i++)
Put(value[i]);

0 comments on commit 6c5d18a

Please sign in to comment.