mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 02:50:32 +00:00
NetworkWriter.SeekZero/StartMessage/FinishMessage removed because they aren't needed anymore after protocol update
This commit is contained in:
parent
54d5ede3e7
commit
96f48fdbba
@ -379,40 +379,6 @@ public void Write(MessageBase msg)
|
|||||||
{
|
{
|
||||||
msg.Serialize(this);
|
msg.Serialize(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SeekZero()
|
|
||||||
{
|
|
||||||
writer.BaseStream.Position = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void StartMessage(short msgType)
|
|
||||||
{
|
|
||||||
SeekZero();
|
|
||||||
|
|
||||||
// two bytes for size, will be filled out in FinishMessage
|
|
||||||
writer.Write((UInt16)0);
|
|
||||||
|
|
||||||
// two bytes for message type
|
|
||||||
Write(msgType);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void FinishMessage()
|
|
||||||
{
|
|
||||||
// size has to fit into ushort
|
|
||||||
if (Position > UInt16.MaxValue)
|
|
||||||
{
|
|
||||||
if (LogFilter.logError) { Debug.LogError("NetworkWriter FinishMessage: size is too large (" + Position + ") bytes. The maximum buffer size is " + UInt16.MaxValue + " bytes."); }
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// jump to zero, replace size (ushort) in header, jump back
|
|
||||||
long oldPosition = Position;
|
|
||||||
ushort size = (ushort)(Position - (sizeof(UInt16) * 2)); // length - two shorts header (size, msgType)
|
|
||||||
|
|
||||||
SeekZero();
|
|
||||||
Write(size);
|
|
||||||
writer.BaseStream.Position = oldPosition;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
#endif //ENABLE_UNET
|
#endif //ENABLE_UNET
|
||||||
|
@ -28,28 +28,6 @@ public void TestWritingLargeMessage()
|
|||||||
Assert.That(writer.Position, Is.EqualTo(40000));
|
Assert.That(writer.Position, Is.EqualTo(40000));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
|
||||||
public void TestResetting()
|
|
||||||
{
|
|
||||||
NetworkWriter writer = new NetworkWriter();
|
|
||||||
writer.StartMessage((short)1337);
|
|
||||||
writer.Write(1);
|
|
||||||
writer.Write(2);
|
|
||||||
writer.Write(3);
|
|
||||||
writer.Write(4);
|
|
||||||
writer.FinishMessage();
|
|
||||||
|
|
||||||
// try SeekZero and reset afterwards
|
|
||||||
int messageSize = writer.Position;
|
|
||||||
writer.SeekZero();
|
|
||||||
Assert.That(writer.Position, Is.Zero);
|
|
||||||
writer.Position = messageSize;
|
|
||||||
|
|
||||||
// check if .ToArray() returns array until .Position
|
|
||||||
writer.Position = 4;
|
|
||||||
Assert.That(writer.ToArray().Length, Is.EqualTo(4));
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void TestToArray()
|
public void TestToArray()
|
||||||
{
|
{
|
||||||
@ -73,7 +51,6 @@ public void TestToArray()
|
|||||||
public void TestPackedUInt32()
|
public void TestPackedUInt32()
|
||||||
{
|
{
|
||||||
NetworkWriter writer = new NetworkWriter();
|
NetworkWriter writer = new NetworkWriter();
|
||||||
writer.StartMessage((short)1337);
|
|
||||||
writer.WritePackedUInt32(0);
|
writer.WritePackedUInt32(0);
|
||||||
writer.WritePackedUInt32(234);
|
writer.WritePackedUInt32(234);
|
||||||
writer.WritePackedUInt32(2284);
|
writer.WritePackedUInt32(2284);
|
||||||
@ -82,11 +59,7 @@ public void TestPackedUInt32()
|
|||||||
writer.WritePackedUInt32(16777219);
|
writer.WritePackedUInt32(16777219);
|
||||||
writer.WritePackedUInt32(UInt32.MaxValue);
|
writer.WritePackedUInt32(UInt32.MaxValue);
|
||||||
|
|
||||||
writer.FinishMessage();
|
|
||||||
|
|
||||||
NetworkReader reader = new NetworkReader(writer.ToArray());
|
NetworkReader reader = new NetworkReader(writer.ToArray());
|
||||||
reader.ReadInt16();
|
|
||||||
Assert.That(reader.ReadUInt16(), Is.EqualTo(1337)); // contentSize (messasge.size - 4 bytes header)
|
|
||||||
Assert.That(reader.ReadPackedUInt32(), Is.EqualTo(0));
|
Assert.That(reader.ReadPackedUInt32(), Is.EqualTo(0));
|
||||||
Assert.That(reader.ReadPackedUInt32(), Is.EqualTo(234));
|
Assert.That(reader.ReadPackedUInt32(), Is.EqualTo(234));
|
||||||
Assert.That(reader.ReadPackedUInt32(), Is.EqualTo(2284));
|
Assert.That(reader.ReadPackedUInt32(), Is.EqualTo(2284));
|
||||||
@ -100,7 +73,6 @@ public void TestPackedUInt32()
|
|||||||
public void TestPackedUInt64()
|
public void TestPackedUInt64()
|
||||||
{
|
{
|
||||||
NetworkWriter writer = new NetworkWriter();
|
NetworkWriter writer = new NetworkWriter();
|
||||||
writer.StartMessage((short)1337);
|
|
||||||
writer.WritePackedUInt64(0);
|
writer.WritePackedUInt64(0);
|
||||||
writer.WritePackedUInt64(234);
|
writer.WritePackedUInt64(234);
|
||||||
writer.WritePackedUInt64(2284);
|
writer.WritePackedUInt64(2284);
|
||||||
@ -113,11 +85,7 @@ public void TestPackedUInt64()
|
|||||||
writer.WritePackedUInt64(72057594037927935);
|
writer.WritePackedUInt64(72057594037927935);
|
||||||
writer.WritePackedUInt64(UInt64.MaxValue);
|
writer.WritePackedUInt64(UInt64.MaxValue);
|
||||||
|
|
||||||
writer.FinishMessage();
|
|
||||||
|
|
||||||
NetworkReader reader = new NetworkReader(writer.ToArray());
|
NetworkReader reader = new NetworkReader(writer.ToArray());
|
||||||
reader.ReadInt16();
|
|
||||||
Assert.That(reader.ReadUInt16(), Is.EqualTo(1337)); // contentSize (messasge.size - 4 bytes header)
|
|
||||||
Assert.That(reader.ReadPackedUInt64(), Is.EqualTo(0));
|
Assert.That(reader.ReadPackedUInt64(), Is.EqualTo(0));
|
||||||
Assert.That(reader.ReadPackedUInt64(), Is.EqualTo(234));
|
Assert.That(reader.ReadPackedUInt64(), Is.EqualTo(234));
|
||||||
Assert.That(reader.ReadPackedUInt64(), Is.EqualTo(2284));
|
Assert.That(reader.ReadPackedUInt64(), Is.EqualTo(2284));
|
||||||
@ -136,7 +104,6 @@ public void TestWritingAndReading()
|
|||||||
{
|
{
|
||||||
// write all simple types once
|
// write all simple types once
|
||||||
NetworkWriter writer = new NetworkWriter();
|
NetworkWriter writer = new NetworkWriter();
|
||||||
writer.StartMessage((short)1337);
|
|
||||||
writer.Write((char)1);
|
writer.Write((char)1);
|
||||||
writer.Write((byte)2);
|
writer.Write((byte)2);
|
||||||
writer.Write((sbyte)3);
|
writer.Write((sbyte)3);
|
||||||
@ -159,16 +126,12 @@ public void TestWritingAndReading()
|
|||||||
writer.WriteBytesAndSize(new byte[] { 19, 20, 21 }, 1, 2); // buffer, offset, count
|
writer.WriteBytesAndSize(new byte[] { 19, 20, 21 }, 1, 2); // buffer, offset, count
|
||||||
writer.WriteBytesAndSize(new byte[] { 22, 23 }, 0, 2); // size, buffer
|
writer.WriteBytesAndSize(new byte[] { 22, 23 }, 0, 2); // size, buffer
|
||||||
|
|
||||||
writer.FinishMessage();
|
|
||||||
|
|
||||||
byte[] data = writer.ToArray();
|
byte[] data = writer.ToArray();
|
||||||
|
|
||||||
|
|
||||||
// read them
|
// read them
|
||||||
NetworkReader reader = new NetworkReader(writer.ToArray());
|
NetworkReader reader = new NetworkReader(writer.ToArray());
|
||||||
|
|
||||||
Assert.That(reader.ReadInt16(), Is.EqualTo(data.Length - sizeof(ushort) * 2)); // msgType
|
|
||||||
Assert.That(reader.ReadUInt16(), Is.EqualTo(1337)); // contentSize (messasge.size - 4 bytes header)
|
|
||||||
Assert.That(reader.ReadChar(), Is.EqualTo(1));
|
Assert.That(reader.ReadChar(), Is.EqualTo(1));
|
||||||
Assert.That(reader.ReadByte(), Is.EqualTo(2));
|
Assert.That(reader.ReadByte(), Is.EqualTo(2));
|
||||||
Assert.That(reader.ReadSByte(), Is.EqualTo(3));
|
Assert.That(reader.ReadSByte(), Is.EqualTo(3));
|
||||||
@ -199,6 +162,5 @@ public void TestWritingAndReading()
|
|||||||
reader.SeekZero();
|
reader.SeekZero();
|
||||||
Assert.That(reader.Position, Is.Zero);
|
Assert.That(reader.Position, Is.Zero);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user