public void TestFastGumpPacket() { var ns = new NetState( new AccountPacketTests.TestConnectionContext { RemoteEndPoint = IPEndPoint.Parse("127.0.0.1") } ); var gump = new ResurrectGump(2); var data = gump.Compile(ns).Compile(); Span <byte> expectedData = stackalloc byte[0x1000]; var pos = 0; expectedData[pos++] = 0xB0; // Packet ID pos += 2; // Length expectedData.Write(ref pos, gump.Serial); expectedData.Write(ref pos, gump.TypeID); expectedData.Write(ref pos, gump.X); expectedData.Write(ref pos, gump.Y); pos += 2; // Layout Length var layoutLength = 0; if (!gump.Draggable) { expectedData.Write(ref pos, GumpUtilities.NoMoveBuffer); layoutLength += GumpUtilities.NoMove.Length; } if (!gump.Closable) { expectedData.Write(ref pos, GumpUtilities.NoCloseBuffer); layoutLength += GumpUtilities.NoClose.Length; } if (!gump.Disposable) { expectedData.Write(ref pos, GumpUtilities.NoDisposeBuffer); layoutLength += GumpUtilities.NoDispose.Length; } if (!gump.Resizable) { expectedData.Write(ref pos, GumpUtilities.NoResizeBuffer); layoutLength += GumpUtilities.NoResize.Length; } foreach (var entry in gump.Entries) { var str = entry.Compile(ns); expectedData.WriteAscii(ref pos, str); layoutLength += str.Length; // ASCII so 1:1 } expectedData.Slice(19, 2).Write((ushort)layoutLength); expectedData.Write(ref pos, (ushort)gump.Strings.Count); for (var i = 0; i < gump.Strings.Count; ++i) { expectedData.WriteBigUni(ref pos, gump.Strings[i] ?? ""); } expectedData.Slice(1, 2).Write((ushort)pos); // Length expectedData = expectedData.Slice(0, pos); AssertThat.Equal(data, expectedData); }
public void TestPackedGumpPacket() { var ns = new NetState( new AccountPacketTests.TestConnectionContext { RemoteEndPoint = IPEndPoint.Parse("127.0.0.1") } ) { ProtocolChanges = ProtocolChanges.Unpack }; var gump = new ResurrectGump(2); var data = gump.Compile(ns).Compile(); Span <byte> expectedData = stackalloc byte[0x1000]; var pos = 0; expectedData[pos++] = 0xDD; // Packet ID pos += 2; // Length expectedData.Write(ref pos, gump.Serial); expectedData.Write(ref pos, gump.TypeID); expectedData.Write(ref pos, gump.X); expectedData.Write(ref pos, gump.Y); var layoutList = new List <string>(); var bufferLength = 1; // Null terminated if (!gump.Draggable) { layoutList.Add(GumpUtilities.NoMove); bufferLength += GumpUtilities.NoMove.Length; } if (!gump.Closable) { layoutList.Add(GumpUtilities.NoClose); bufferLength += GumpUtilities.NoClose.Length; } if (!gump.Disposable) { layoutList.Add(GumpUtilities.NoDispose); bufferLength += GumpUtilities.NoDispose.Length; } if (!gump.Resizable) { layoutList.Add(GumpUtilities.NoResize); bufferLength += GumpUtilities.NoResize.Length; } foreach (var entry in gump.Entries) { var str = entry.Compile(ns); bufferLength += str.Length; layoutList.Add(str); } var memOwner = SlabMemoryPool.Shared.Rent(bufferLength); var buffer = memOwner.Memory.Span; var bufferPos = 0; foreach (var layout in layoutList) { buffer.WriteAscii(ref bufferPos, layout); } #if NO_LOCAL_INIT buffer.Write(ref bufferPos, (byte)0); // Layout terminator #else bufferPos++; #endif expectedData.WritePacked(ref pos, buffer.Slice(0, bufferPos)); memOwner.Dispose(); expectedData.Write(ref pos, gump.Strings.Count); bufferLength = gump.Strings.Sum(str => 2 + str.Length * 2); memOwner = SlabMemoryPool.Shared.Rent(bufferLength); buffer = memOwner.Memory.Span; bufferPos = 0; foreach (var str in gump.Strings) { buffer.WriteBigUni(ref bufferPos, str); } expectedData.WritePacked(ref pos, buffer.Slice(0, bufferPos)); // Length expectedData.Slice(1, 2).Write((ushort)pos); expectedData = expectedData.Slice(0, pos); AssertThat.Equal(data, expectedData); }