public void PacketHeaderConstructorTest1() { byte[] bytes = { 0x48, 0x41, 0x4B, 0x00, 0x00, 0x00, 0x20, 0x00, 0x01 }; PacketHeader target = new PacketHeader(bytes); Assert.AreEqual(32, target.Length); Assert.AreEqual(0x01, target.Command); }
public void getBytesTest() { byte[] bytes = { 0x48, 0x41, 0x4B, 0x00, 0x00, 0x00, 0x20, 0x00, 0x01 }; PacketHeader target = new PacketHeader(bytes); byte[] expected = { 0x48, 0x41, 0x4B, 0x00, 0x00, 0x00, 0x20, 0x00, 0x01 }; ; byte[] actual; actual = target.getBytes(); Assert.IsTrue(expected.SequenceEqual(actual)); }
public static void SendPosUpdate(string nPlayerId, Vector2 nPos) { PacketHeader header = new PacketHeader(32, 0x01); byte[] packet = new byte[32]; Array.Copy(ArrayUtil.StringToBytes(nPlayerId), packet, 24); byte[] temp = BitConverter.GetBytes((int)nPos.X); temp.Reverse(); Array.Copy(temp, 0, packet, 24, 4); temp = BitConverter.GetBytes((int)nPos.Y); temp.Reverse(); Array.Copy(temp, 0, packet, 28, 4); Packet realpacket = new Packet(header, packet); mConnection.Write(realpacket.getBytes()); }
public void PacketConstructorTest() { PacketHeader header = new PacketHeader(32, 0x01); byte[] body = new byte[32] { 0x48, 0x41, 0x4B, 0x48, 0x41, 0x4B, 0x48, 0x41, 0x4B, 0x48, 0x41, 0x4B, 0x48, 0x41, 0x4B, 0x48, 0x41, 0x4B, 0x48, 0x41, 0x4B, 0x48, 0x41, 0x4B, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01 }; Packet target = new Packet(header, body); Assert.IsTrue(body.SequenceEqual(target.Body)); Assert.IsTrue(header.Equals(target.Header)); }
private static void process() { // There needs to be at least a header to get while (Queue.Count > 9) { byte[] hak = Queue.GetRange(0, 3).ToArray(); if (Encoding.ASCII.GetString(hak) == PacketHeader.TAG) { PacketHeader ph = new PacketHeader(Queue.GetRange(0, 9).ToArray()); Packet pack = new Packet(ph, Queue.GetRange(9, ph.Length).ToArray()); // Remove the stuff we've gathered Queue.RemoveRange(0, ph.Length + 9); if (CommandDelegates.CommandInitialized(ph.Command)) { switch (ph.Command) { case 0x01: // Update position CommandDelegates.UpdatePositionDelegate upd = (CommandDelegates.UpdatePositionDelegate)CommandDelegates.getInitializedDelegate(ph.Command); upd.Invoke(CommandParamsParser.parseBytes(pack.Body, ph.Command)); break; case 0x02: // Spawn Monster CommandDelegates.SpawnMonsterDelegate smd = (CommandDelegates.SpawnMonsterDelegate)CommandDelegates.getInitializedDelegate(ph.Command); smd.Invoke(CommandParamsParser.parseBytes(pack.Body, ph.Command)); break; } } else { // This means we're not supposed to act uppon the command } } else { // for now, throw away the first byte and try to look for the tag again Queue.RemoveAt(0); continue; } } // Pause the thread. It will be resumed when data is enqueued. runnerReset.WaitOne(); }
public Packet(PacketHeader header, byte[] body) { this.Header = header; this.Body = body; }
public Packet(byte[] bytes) { this.Header = new PacketHeader(ArrayUtil.ByteSubArray(bytes, 0, 9)); this.Body = ArrayUtil.ByteSubArray(bytes, 9, bytes.Length - 10); }