Ejemplo n.º 1
0
        private void OctetQueueChanged(OctetQueue queue)
        {
            // log.Debug("OctetQueue changed {0}", queue);
            lastReceivedPacketTime = Timestamp.Now();
            var tempBuf = new byte[32 * 1024];

            while (true)
            {
                var octetCount = queue.Peek(tempBuf, 0, tempBuf.Length);

                if (octetCount <= 0)
                {
                    return;
                }
                int octetsUsed = 0;
                var packet     = PacketDecoder.Decode(tempBuf, 0, octetCount, out octetsUsed, log);

                if (packet == null)
                {
                    return;
                }
                queue.Skip(octetsUsed);
                try
                {
                    OnPacketRead(packet);
                }
                catch (Exception e)
                {
                    log.Exception(e);
                    throw e;
                }
            }
        }
Ejemplo n.º 2
0
        public void Add(byte[] octets)
        {
            // log.Debug("Adding to receive stream:{0:X}", octets);
            queue.Enqueue(octets, 0, octets.Length);
            HastyPacket packet = null;

            do
            {
                var octetCount = queue.Peek(targetBuffer, 0, targetBuffer.Length);

                if (octetCount == 0)
                {
                    // log.Debug("octetCount zero");
                    return;
                }
                int newOffset;
                packet = PacketDecoder.Decode(targetBuffer, 0, octetCount, out newOffset, log);

                if (packet != null)
                {
                    // log.Debug("Got packet!");
                    queue.Skip(newOffset);
                    packetReceiver.ReceivePacket(packet);
                }
                else
                {
                    // log.Debug("No packet so far");
                }
            } while (packet != null);
        }
Ejemplo n.º 3
0
        public void TestCompletePacket()
        {
            var data = new byte[] { 0x06, 0x01, 0x48, 0x65, 0x6C, 0x6C, 0x6F };
            int remainingOctets;
            var packet = PacketDecoder.Decode(data, 0, data.Length, out remainingOctets);

            Assert.That(0x01, Is.EqualTo(packet.Command));
        }
Ejemplo n.º 4
0
        public void TestIncompletePacket()
        {
            var data = new byte[] { 0x06, 0x01, 0x48, 0x65, 0x6C, 0x6C };
            int nextOffset;
            var packet = PacketDecoder.Decode(data, 0, data.Length, out nextOffset);

            Assert.That(nextOffset, Is.EqualTo(0));
            Assert.That(packet, Is.Null);
        }
Ejemplo n.º 5
0
        public void TestMultiplePackets()
        {
            var data = new byte[] { 0x02, 0x01, 0x48, 0x01, 0x42 };
            int nextOctetOffset;
            var packet = PacketDecoder.Decode(data, 0, data.Length, out nextOctetOffset);

            Assert.That(nextOctetOffset, Is.EqualTo(3));
            Assert.That(packet.Command, Is.EqualTo(0x01));
            Assert.That(packet.Length, Is.EqualTo(0x02));

            int nextOctetOffset2;
            var packet2 = PacketDecoder.Decode(data, nextOctetOffset, data.Length - nextOctetOffset, out nextOctetOffset2);

            Assert.That(nextOctetOffset2, Is.EqualTo(5));
            Assert.That(packet2.Command, Is.EqualTo(0x42));
            Assert.That(packet2.Length, Is.EqualTo(0x01));
        }
Ejemplo n.º 6
0
 public int DoDecode(byte[] data, int offset, int length, out DecodedPacket pkt)
 {
     return(_decoder.Decode(data, offset, length, out pkt));
 }
Ejemplo n.º 7
0
        protected override Packet GetResult(string line)
        {
            var packet = m_decoder.Decode(line);

            return(packet);
        }