Assert.AreEqual("NO", sub.ReadString());
}
- [TestMethod]
- public void TestMessage()
- {
- string test = "5 32 0 0 0 5 0 5 255 255 255 255 15 2 0 2 2 2 9 0 1 4 110 123 233 131 255 127 255 127";
- byte[] testValues = test.Replace("-", "").Split(' ').Select(b => byte.Parse(b)).ToArray();
-
- MessageReader msg = MessageReader.Get(testValues, 0, testValues.Length);
-
- msg.ReadInt32();
- msg.ReadByte();
-
- while (msg.Position < msg.Length)
- {
- var sub = msg.ReadMessage();
- Console.WriteLine($"Position: {msg.Position}/{msg.Length}");
-
- if (sub.Tag == 4) // Spawn
- {
- uint spawnId = sub.ReadPackedUInt32();
- if (spawnId == 4)
- {
- int ownerId = sub.ReadPackedInt32();
- int numChild = sub.ReadPackedInt32();
- Console.WriteLine($"Spawning {spawnId} for {ownerId} with {numChild} children");
- for (int i = 0; i < numChild; ++i)
- {
- uint childId = sub.ReadPackedUInt32();
- var childReader = sub.ReadMessage();
- Console.WriteLine($"Child {childId} has data ({childReader.Tag}) len={childReader.Length}");
- }
- }
- else if (spawnId == 3)
- {
- int ownerId = sub.ReadPackedInt32();
- int numChild = sub.ReadPackedInt32();
- Console.WriteLine($"Spawning {spawnId} for {ownerId} with {numChild} children");
- for (int i = 0; i < numChild; ++i)
- {
- uint childId = sub.ReadPackedUInt32();
- var childReader = sub.ReadMessage();
-
- var gameGuid = new Guid(childReader.ReadBytesAndSize());
- var numPlayers = childReader.ReadByte();
- Console.WriteLine($"Child {childId} has data: {gameGuid} NumPlayers= {numPlayers}");
- Console.WriteLine($"Remainder Data = {string.Join(" ", childReader.ReadBytes(childReader.Length - childReader.Position))}");
-
- }
- }
- else
- {
- sub.Position = 0;
- Console.WriteLine($"Tag: {sub.Tag}\tLength: {sub.Length}\tData = {string.Join(" ", sub.ReadBytes(sub.Length).Select(s => s.ToString()).ToArray())}");
- }
- }
- else
- {
- sub.Position = 0;
- Console.WriteLine($"Tag: {sub.Tag}\tLength: {sub.Length}\tData = {string.Join(" ", sub.ReadBytes(sub.Length).Select(s => s.ToString()).ToArray())}");
- }
-
- }
- }
-
[TestMethod]
public void GetLittleEndian()
{
private int readHead;
- public static MessageReader Get(byte[] buffer, int offset, int length)
+ public static MessageReader Get(MessageReader srcMsg)
+ {
+ var output = objectPool.GetObject();
+ output.Buffer = srcMsg.Buffer;
+ output.Offset = srcMsg.Offset;
+ output.Position = srcMsg.Position;
+ output.Length = srcMsg.Length;
+ output.Tag = srcMsg.Tag;
+ return output;
+ }
+
+ public static MessageReader Get(byte[] buffer)
{
var output = objectPool.GetObject();
output.Buffer = buffer;
- output.Offset = offset;
+ output.Offset = 0;
output.Position = 0;
- output.Length = length;
- output.Tag = output.ReadByte();
+ output.Length = buffer.Length;
+ output.Tag = byte.MaxValue;
return output;
}
output.Offset = offset;
output.Position = 0;
+ if (output.readHead + 3 > output.Buffer.Length) return null;
output.Length = output.ReadUInt16();
output.Tag = output.ReadByte();
public MessageReader ReadMessage()
{
var output = MessageReader.Get(this.Buffer, this.readHead);
+ if (output == null) return null;
+
this.Position += output.Length + 3;
return output;
}
this.Buffer = new byte[bufferSize];
}
+ public byte[] ToByteArray(bool includeHeader)
+ {
+ if (includeHeader)
+ {
+ byte[] output = new byte[this.Length];
+ System.Buffer.BlockCopy(this.Buffer, 0, output, 0, this.Length);
+ return output;
+ }
+ else
+ {
+ switch (this.SendOption)
+ {
+ case Hazel.SendOption.Reliable:
+ {
+ byte[] output = new byte[this.Length - 3];
+ System.Buffer.BlockCopy(this.Buffer, 3, output, 0, this.Length - 3);
+ return output;
+ }
+ case Hazel.SendOption.None:
+ {
+ byte[] output = new byte[this.Length - 1];
+ System.Buffer.BlockCopy(this.Buffer, 1, output, 0, this.Length - 1);
+ return output;
+ }
+
+ }
+ }
+
+ throw new NotImplementedException();
+ }
+
///
/// <param name="sendOption">The option specifying how the message should be sent.</param>
public static MessageWriter Get(SendOption sendOption = SendOption.None)