private void Exec(IEntity p, FollowPacketHandler h, short target) { var b = new Blob(16); b.Write16(target); h.HandleAll(p, o => PacketMessage.Success((byte)o, b)); }
private void Exec(ChatPacketHandler h, IEntity p, ChatMessage.TextEffect e, ChatMessage.TextColor c, string m) { var b = new Blob(512); b.Write((byte)e); b.Write((byte)c); b.WriteString(m); h.HandleAll(p, o => PacketMessage.Success((byte)o, b)); }
public void NoNpc() { var server = Mock.Server(); var p = Mock.Player("xd", server).Get(); var h = new TalkToPacketHandler(server.Services); var b = new Blob(sizeof(short)); b.Write16(5); h.HandleAll(p, o => PacketMessage.Success((byte)o, b)); }
public void WrongNpc() { var server = Mock.Server(); var p = Mock.Player("xd", server); var npc = Mock.Npc(server, Mock.Invariant, 5, "test npc").Get(); var h = new TalkToPacketHandler(server.Services); var b = new Blob(sizeof(short)); b.Write16(1234); h.HandleAll(p.Get(), o => PacketMessage.Success((byte)o, b)); }
public void ValidNpc() { var server = Mock.Server(); var p = Mock.Player("xd", server).Get(); var npc = Mock.Npc(server, Mock.Invariant, 1, "testnpc").Get(); var h = new TalkToPacketHandler(server.Services); var b = new Blob(sizeof(short)); b.Write16(npc.AssertGetNpc().InstanceId); h.HandleAll(p, o => PacketMessage.Success((byte)o, b)); }
public static void SpamTrash(this IPacketHandler h, IEntity ent) { const int defaultShortSize = 1024; // size used for short size opcodes var s = Server(); var p = Player(s); void Spam(int size, bool randomSize, int op) { var iterations = (int)Math.Pow(2, 13); for (var i = 0; i < iterations; i++) { Blob b = randomSize ? RandomData(Rng.Next(0, size + 1)) : RandomData(size); h.Handle(ent, PacketMessage.Success((byte)op, b)); } } if (PacketDb == null) { var dirBuild = Path.GetDirectoryName(typeof(MockServer).GetTypeInfo().Assembly.Location); PacketDb = new JsonPacketDatabase(Path.Combine(dirBuild, "packet-lengths.json")); } foreach (var op in h.Handles) { // chose size var size = PacketDb.GetIncoming(op); switch (size) { case PacketLength.NextByte: Spam(byte.MaxValue, true, op); break; case PacketLength.NextShort: Spam(defaultShortSize, true, op); break; case PacketLength.Undefined: throw new NotSupportedException($"Undefined packet in {h.GetType().Name}"); default: var finalSize = (int) size; if (finalSize == 0) return; Spam(finalSize, false, op); break; } } }
public IEnumerable <PacketMessage> Parse(CircularBlob stream) { while (stream.CanRead()) { // peek everything untill we 100% have the packet. var opcodePeek = stream.Peek(); var lenType = _db.GetIncoming(opcodePeek); var lenPayloadPeek = 0; var payloadOffset = 0; switch (lenType) { case PacketLength.NextByte: if (!stream.CanRead(1)) { break; } lenPayloadPeek = stream.Peek(1); payloadOffset = 1; break; case PacketLength.NextShort: if (!stream.CanRead(2)) { break; } lenPayloadPeek = stream.Peek(1) << 8 + stream.Peek(2); payloadOffset = 2; break; case PacketLength.Undefined: yield return(PacketMessage.Undefined(opcodePeek)); yield break; default: lenPayloadPeek = (byte)lenType; break; } if (!stream.CanRead(payloadOffset + lenPayloadPeek)) { break; } // we can read the whole packet, do so. // do some assertions on the way var opcode = stream.ReadByte(); Debug.Assert(opcode == opcodePeek); var lenPayload = 0; switch (lenType) { case PacketLength.NextByte: lenPayload = stream.ReadByte(); break; case PacketLength.NextShort: lenPayload = stream.ReadInt16(); break; case PacketLength.Undefined: yield return(PacketMessage.Undefined(opcodePeek)); yield break; default: lenPayload = (byte)lenType; break; } Debug.Assert(lenPayload == lenPayloadPeek); // don't bother creating a new Blob if we're storing nothing. if (lenPayload == 0) { yield return(PacketMessage.Success(opcode, Blob.Empty)); } else { var payload = new byte[lenPayload]; stream.ReadBlock(payload, 0, lenPayload); yield return(PacketMessage.Success(opcode, new Blob(payload))); } } }
private void TestSuccess( CommandPacketHandler h, MockCommandHandler ch, IEntity p, Blob b) { h.HandleAll(p, o => PacketMessage.Success((byte)o, b), () => Assert.IsTrue(ch.WasCalled)); }
private void TestForFailAll(ChatPacketHandler h, Blob b, IEntity p) { h.HandleAll(p, a => PacketMessage.Success((byte)a, b), () => TestForFail(p)); }