Exemplo n.º 1
0
        public unsafe bool Read(PooledSocket socket)
        {
            if (!socket.IsAlive)
            {
                this.StatusCode = -1;
                return(false);
            }

            byte[] header = new byte[24];
            socket.Read(header, 0, 24);
#if DEBUG_PROTOCOL
            if (log.IsDebugEnabled)
            {
                log.Debug("Received binary response");

                StringBuilder sb = new StringBuilder(128).AppendLine();

                for (int i = 0; i < header.Length; i++)
                {
                    byte value = header[i];
                    sb.Append(value < 16 ? "0x0" : "0x").Append(value.ToString("X"));

                    if (i % 4 == 3)
                    {
                        sb.AppendLine();
                    }
                    else
                    {
                        sb.Append(" ");
                    }
                }

                log.Debug(sb.ToString());
            }
#endif

            fixed(byte *buffer = header)
            {
                if (buffer[0] != MAGIC_VALUE)
                {
                    throw new InvalidOperationException("Expected magic value " + MAGIC_VALUE + ", received: " + buffer[0]);
                }

                int remaining   = BinaryConverter.DecodeInt32(buffer, HEADER_BODY);
                int extraLength = buffer[HEADER_EXTRA];

                byte[] data = new byte[remaining];
                socket.Read(data, 0, remaining);

                this.Extra = new ArraySegment <byte>(data, 0, extraLength);
                this.Data  = new ArraySegment <byte>(data, extraLength, data.Length - extraLength);

                this.DataType   = buffer[HEADER_DATATYPE];
                this.Opcode     = buffer[HEADER_OPCODE];
                this.StatusCode = BinaryConverter.DecodeInt16(buffer, HEADER_STATUS);

                this.KeyLength     = BinaryConverter.DecodeInt16(buffer, HEADER_KEY);
                this.CorrelationId = BinaryConverter.DecodeInt32(buffer, HEADER_OPAQUE);
                this.CAS           = BinaryConverter.DecodeUInt64(buffer, HEADER_CAS);
            }

            return(this.StatusCode == 0);
        }