mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 11:00:32 +00:00
Reader/Writer: sort WriteBytes(AndSize) functions
This commit is contained in:
parent
f05df29639
commit
4d7c5b6e41
@ -78,16 +78,6 @@ public static string ReadString(this NetworkReader reader)
|
|||||||
return reader.encoding.GetString(data.Array, data.Offset, data.Count);
|
return reader.encoding.GetString(data.Array, data.Offset, data.Count);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <exception cref="T:OverflowException">if count is invalid</exception>
|
|
||||||
public static byte[] ReadBytesAndSize(this NetworkReader reader)
|
|
||||||
{
|
|
||||||
// count = 0 means the array was null
|
|
||||||
// otherwise count -1 is the length of the array
|
|
||||||
uint count = reader.ReadUInt();
|
|
||||||
// Use checked() to force it to throw OverflowException if data is invalid
|
|
||||||
return count == 0 ? null : reader.ReadBytes(checked((int)(count - 1u)));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static byte[] ReadBytes(this NetworkReader reader, int count)
|
public static byte[] ReadBytes(this NetworkReader reader, int count)
|
||||||
{
|
{
|
||||||
// prevent allocation attacks with a reasonable limit.
|
// prevent allocation attacks with a reasonable limit.
|
||||||
@ -104,6 +94,17 @@ public static byte[] ReadBytes(this NetworkReader reader, int count)
|
|||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <exception cref="T:OverflowException">if count is invalid</exception>
|
||||||
|
public static byte[] ReadBytesAndSize(this NetworkReader reader)
|
||||||
|
{
|
||||||
|
// count = 0 means the array was null
|
||||||
|
// otherwise count -1 is the length of the array
|
||||||
|
uint count = reader.ReadUInt();
|
||||||
|
// Use checked() to force it to throw OverflowException if data is invalid
|
||||||
|
return count == 0 ? null : reader.ReadBytes(checked((int)(count - 1u)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reads ArraySegment and size header
|
||||||
/// <exception cref="T:OverflowException">if count is invalid</exception>
|
/// <exception cref="T:OverflowException">if count is invalid</exception>
|
||||||
public static ArraySegment<byte> ReadBytesAndSizeSegment(this NetworkReader reader)
|
public static ArraySegment<byte> ReadBytesAndSizeSegment(this NetworkReader reader)
|
||||||
{
|
{
|
||||||
|
@ -82,11 +82,6 @@ public static void WriteString(this NetworkWriter writer, string value)
|
|||||||
writer.Position += written;
|
writer.Position += written;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void WriteBytesAndSizeSegment(this NetworkWriter writer, ArraySegment<byte> buffer)
|
|
||||||
{
|
|
||||||
writer.WriteBytesAndSize(buffer.Array, buffer.Offset, buffer.Count);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Weaver needs a write function with just one byte[] parameter
|
// Weaver needs a write function with just one byte[] parameter
|
||||||
// (we don't name it .Write(byte[]) because it's really a WriteBytesAndSize since we write size / null info too)
|
// (we don't name it .Write(byte[]) because it's really a WriteBytesAndSize since we write size / null info too)
|
||||||
|
|
||||||
@ -113,6 +108,13 @@ public static void WriteBytesAndSize(this NetworkWriter writer, byte[] buffer, i
|
|||||||
writer.WriteBytes(buffer, offset, count);
|
writer.WriteBytes(buffer, offset, count);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// writes ArraySegment and size header
|
||||||
|
public static void WriteBytesAndSizeSegment(this NetworkWriter writer, ArraySegment<byte> buffer)
|
||||||
|
{
|
||||||
|
writer.WriteBytesAndSize(buffer.Array, buffer.Offset, buffer.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
// writes ArraySegment and size header
|
||||||
public static void WriteArraySegment<T>(this NetworkWriter writer, ArraySegment<T> segment)
|
public static void WriteArraySegment<T>(this NetworkWriter writer, ArraySegment<T> segment)
|
||||||
{
|
{
|
||||||
int length = segment.Count;
|
int length = segment.Count;
|
||||||
|
Loading…
Reference in New Issue
Block a user