Example #1
0
        public void SetRequestSerialization()
        {
            MemoryStream stream = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(stream);

            Byte[] packet =
            {
                0x80, 0x01, 0x00, 0x05,
                0x08, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x12,
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0xde, 0xad, 0xbe, 0xef,
                0x00, 0x00, 0x0e, 0x10,
                0x48, 0x65, 0x6c, 0x6c,
                0x6f, 0x57, 0x6f, 0x72,
                0x6c, 0x64
            };

            writer.Write(packet);

            stream.Seek(0, SeekOrigin.Begin);

            using (BinaryReader reader = new BinaryReader(stream))
            {
                SetRequest request = (SetRequest)ProtocolPacket.ReadRequest(reader);
                Assert.AreEqual(request.Header.Magic, 0x80);
                Assert.AreEqual(request.Header.OpCode, CommandOpCode.Set);
                Assert.AreEqual(request.Header.KeyLength, 5);
                Assert.AreEqual(request.Header.ExtrasLength, 8);
                Assert.AreEqual(request.Header.DataType, 0);
                Assert.AreEqual(request.Header.Status, ResponseStatus.NoError);
                Assert.AreEqual(request.Header.TotalBodyLength, (uint)0x12);
                Assert.AreEqual(request.Header.Opaque, (uint)0);
                Assert.AreEqual(request.Header.CAS, (ulong)0);
                Assert.AreEqual(request.Header.Extras.Flags, (uint)0xdeadbeef);
                Assert.AreEqual(request.Key, "Hello");
                Assert.AreEqual(request.Value, "World");

                MemoryStream stream2 = new MemoryStream();
                BinaryWriter writer2 = new BinaryWriter(stream2);

                request.Write(writer2);

                writer.Close();

                byte[] packet2 = stream2.ToArray();

                CollectionAssert.AreEqual(packet, packet2);
            }
        }
Example #2
0
        public void GetRequestSerialization()
        {
            MemoryStream stream = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(stream);

            Byte[] packet =
            {
                0x80, 0x00, 0x00, 0x05,
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x05,
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0x48, 0x65, 0x6c, 0x6c,
                0x6f
            };

            writer.Write(packet);

            stream.Seek(0, SeekOrigin.Begin);

            using (BinaryReader reader = new BinaryReader(stream))
            {
                GetRequest request = (GetRequest)ProtocolPacket.ReadRequest(reader);

                Assert.AreEqual(request.Header.Magic, 0x80);
                Assert.AreEqual(request.Header.OpCode, CommandOpCode.Get);
                Assert.AreEqual(request.Header.KeyLength, 5);
                Assert.AreEqual(request.Header.ExtrasLength, 0);
                Assert.AreEqual(request.Header.DataType, 0);
                Assert.AreEqual(request.Header.Status, ResponseStatus.NoError);
                Assert.AreEqual(request.Header.TotalBodyLength, (uint)5);
                Assert.AreEqual(request.Header.Opaque, (uint)0);
                Assert.AreEqual(request.Header.CAS, (ulong)0);
                Assert.IsNull(request.Header.Extras);
                Assert.AreEqual(request.Key, "Hello");
                Assert.IsTrue(String.IsNullOrEmpty(request.Value));

                MemoryStream stream2 = new MemoryStream();
                BinaryWriter writer2 = new BinaryWriter(stream2);

                request.Write(writer2);

                writer.Close();

                byte[] packet2 = stream2.ToArray();

                CollectionAssert.AreEqual(packet, packet2);
            }
        }
Example #3
0
        public void GetResponseSerialization()
        {
            MemoryStream stream = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(stream);

            Byte[] packet =
            {
                0x81, 0x00, 0x00, 0x00,
                0x04, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x09,
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x01,
                0xde, 0xad, 0xbe, 0xef,
                0x57, 0x6f, 0x72, 0x6c,
                0x64
            };

            writer.Write(packet);

            stream.Seek(0, SeekOrigin.Begin);

            using (BinaryReader reader = new BinaryReader(stream))
            {
                GetResponse response = (GetResponse)ProtocolPacket.ReadRequest(reader);
                Assert.AreEqual(response.Header.Magic, 0x81);
                Assert.AreEqual(response.Header.OpCode, CommandOpCode.Get);
                Assert.AreEqual(response.Header.KeyLength, 0);
                Assert.AreEqual(response.Header.ExtrasLength, 4);
                Assert.AreEqual(response.Header.DataType, 0);
                Assert.AreEqual(response.Header.Status, ResponseStatus.NoError);
                Assert.AreEqual(response.Header.TotalBodyLength, (uint)9);
                Assert.AreEqual(response.Header.Opaque, (uint)0);
                Assert.AreEqual(response.Header.CAS, (ulong)1);
                Assert.AreEqual(response.Header.Extras.Flags, (uint)0xdeadbeef);
                Assert.AreEqual(response.Value, "World");
                Assert.IsTrue(String.IsNullOrEmpty(response.Key));

                MemoryStream stream2 = new MemoryStream();
                BinaryWriter writer2 = new BinaryWriter(stream2);

                response.Write(writer2);

                writer.Close();

                byte[] packet2 = stream2.ToArray();

                CollectionAssert.AreEqual(packet, packet2);
            }
        }
Example #4
0
        /// <summary>
        /// This method will perform the packet framing. Each time we receive data on the
        /// socket, we peel off packets fro mit and remember the remaining data. This handles
        /// partial packets including partial headers.
        /// </summary>
        /// <param name="data">data received on the socket</param>
        /// <param name="start">start</param>
        /// <param name="length">number of bytes</param>
        public void Push(byte[] data, int start, int length)
        {
            // If we have pending data, append the new data to it.
            if (this.pendingByteCount != 0)
            {
                data = Utilities.Utilities.Combine(this.workingBuffer, data);
                this.workingBuffer    = null;
                this.pendingByteCount = 0;
            }

            using (MemoryStream memoryStream = new MemoryStream(data, start, length))
            {
                using (BinaryReader reader = new BinaryReader(memoryStream))
                {
                    int remainingBytes = (int)(memoryStream.Length - memoryStream.Position);

                    while (remainingBytes > ProtocolHeader.HeaderLength)
                    {
                        ProtocolHeader header = null;

                        // If we already have a header, use it. If not read a new one.
                        if (this.pendingHeader != null)
                        {
                            header             = this.pendingHeader;
                            this.pendingHeader = null;
                        }
                        else
                        {
                            header = new ProtocolHeader();
                            header.Read(reader);
                        }

                        remainingBytes = (int)(memoryStream.Length - memoryStream.Position);

                        if (header.TotalBodyLength - header.ExtrasLength <= remainingBytes)
                        {
                            // At this point, we have a full packet. Queue it for consumption
                            ProtocolPacket packet = ProtocolPacket.ReadRequest(reader, header);

                            this.Packets.Enqueue(packet);
                        }
                        else
                        {
                            // We have a header and a partial body. Save it for later.
                            this.pendingHeader    = header;
                            this.workingBuffer    = reader.ReadBytes(remainingBytes);
                            this.pendingByteCount = remainingBytes;
                        }

                        remainingBytes = (int)(memoryStream.Length - memoryStream.Position);
                    }

                    // We might have a partial header left out. Store the bytes till we get the next packet
                    remainingBytes = (int)(memoryStream.Length - memoryStream.Position);

                    if (remainingBytes != 0)
                    {
                        this.workingBuffer    = reader.ReadBytes(remainingBytes);
                        this.pendingByteCount = remainingBytes;
                        this.pendingHeader    = null;
                    }
                }
            }
        }