Example #1
0
        public unsafe bool Read(PooledSocket socket)
        {
            this.StatusCode = -1;

            if (!socket.IsAlive)
            {
                return(false);
            }

            var header = new byte[HeaderLength];

            socket.Read(header, 0, header.Length);

            int dataLength, extraLength;

            DeserializeHeader(header, out dataLength, out extraLength);

            if (dataLength > 0)
            {
                var data = new byte[dataLength];
                socket.Read(data, 0, dataLength);

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

            return(this.StatusCode == 0);
        }
Example #2
0
        public unsafe bool Read(PooledSocket socket)
        {
            this.StatusCode = -1;

            if (!socket.IsAlive)
                return false;

            var header = new byte[HeaderLength];
            socket.Read(header, 0, header.Length);

            int dataLength, extraLength;

            DeserializeHeader(header, out dataLength, out extraLength);

            if (dataLength > 0)
            {
                var data = new byte[dataLength];
                socket.Read(data, 0, dataLength);

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

            return this.StatusCode == 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;
		}
        public static GetResponse ReadItem(PooledSocket socket)
        {
            string description = TextSocketHelper.ReadResponse(socket);

            if (String.Compare(description, "END", StringComparison.Ordinal) == 0)
            {
                return(null);
            }

            if (description.Length < 6 || String.Compare(description, 0, "VALUE ", 0, 6, StringComparison.Ordinal) != 0)
            {
                throw new MemcachedClientException("No VALUE response received.\r\n" + description);
            }

            ulong cas = 0;

            string[] parts = description.Split(' ');

            // response is:
            // VALUE <key> <flags> <bytes> [<cas unique>]
            // 0     1     2       3       4
            //
            // cas only exists in 1.2.4+
            //
            if (parts.Length == 5)
            {
                if (!UInt64.TryParse(parts[4], out cas))
                {
                    throw new MemcachedClientException("Invalid CAS VALUE received.");
                }
            }
            else if (parts.Length < 4)
            {
                throw new MemcachedClientException("Invalid VALUE response received: " + description);
            }

            ushort flags  = UInt16.Parse(parts[2], CultureInfo.InvariantCulture);
            int    length = Int32.Parse(parts[3], CultureInfo.InvariantCulture);

            byte[] allData = new byte[length];
            byte[] eod     = new byte[2];

            socket.Read(allData, 0, length);
            socket.Read(eod, 0, 2);             // data is terminated by \r\n

            GetResponse retval = new GetResponse(parts[1], flags, cas, allData);

            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Received value. Data type: {0}, size: {1}.", retval.Item.Flags, retval.Item.Data.Count);
            }

            return(retval);
        }
Example #5
0
        //Private method for reading results of the "get" command.
        private bool readValue(PooledSocket socket, out object value, out string key, out ulong unique)
        {
            string response = socket.ReadResponse();

            string[] parts = response.Split(' ');             //Result line from server: "VALUE <key> <flags> <bytes> <cas unique>"
            if (parts[0] == "VALUE")
            {
                key = parts[1];
                byte[] bytes = new byte[Convert.ToUInt32(parts[3], CultureInfo.InvariantCulture)];
                if (parts.Length > 4)
                {
                    unique = Convert.ToUInt64(parts[4]);
                }
                else
                {
                    unique = 0;
                }
                socket.Read(bytes);
                socket.SkipUntilEndOfLine();                 //Skip the trailing \r\n
                var type = (SerializedType)Enum.Parse(typeof(SerializedType), parts[2]);
                value = Deserialize(key, type, bytes);
                return(true);
            }
            else
            {
                key    = null;
                value  = null;
                unique = 0;
                return(false);
            }
        }
Example #6
0
		public static GetResponse ReadItem(PooledSocket socket)
		{
			string description = TextSocketHelper.ReadResponse(socket);

			if (String.Compare(description, "END", StringComparison.Ordinal) == 0)
				return null;

			if (description.Length < 6 || String.Compare(description, 0, "VALUE ", 0, 6, StringComparison.Ordinal) != 0)
				throw new MemcachedClientException("No VALUE response received.\r\n" + description);

			ulong cas = 0;
			string[] parts = description.Split(' ');

			// response is:
			// VALUE <key> <flags> <bytes> [<cas unique>]
			// 0     1     2       3       4
			//
			// cas only exists in 1.2.4+
			//
			if (parts.Length == 5)
			{
				if (!UInt64.TryParse(parts[4], out cas))
					throw new MemcachedClientException("Invalid CAS VALUE received.");

			}
			else if (parts.Length < 4)
			{
				throw new MemcachedClientException("Invalid VALUE response received: " + description);
			}

			ushort flags = UInt16.Parse(parts[2], CultureInfo.InvariantCulture);
			int length = Int32.Parse(parts[3], CultureInfo.InvariantCulture);

			byte[] allData = new byte[length];
			byte[] eod = new byte[2];

			socket.Read(allData, 0, length);
			socket.Read(eod, 0, 2); // data is terminated by \r\n

			GetResponse retval = new GetResponse(parts[1], flags, cas, allData);

			if (log.IsDebugEnabled)
				log.DebugFormat("Received value. Data type: {0}, size: {1}.", retval.Item.Flags, retval.Item.Data.Count);

			return retval;
		}
Example #7
0
        protected override Enyim.Caching.Memcached.Results.IOperationResult ReadResponse(PooledSocket socket)
        {
            string description = TextSocketHelper.ReadResponse(socket);

            if (String.Compare(description, "END", StringComparison.Ordinal) == 0)
            {
                return(null);
            }

            if (description.Length < 7 || String.Compare(description, 0, "CONFIG ", 0, 7, StringComparison.Ordinal) != 0)
            {
                throw new MemcachedClientException("No CONFIG response received.\r\n" + description);
            }

            string[] parts = description.Split(' ');

            /****** Format ********
             *
             * CONFIG <key> <flags> <bytes>
             * 0        1       2       3
             *
             */

            ushort flags  = UInt16.Parse(parts[2], CultureInfo.InvariantCulture);
            int    length = Int32.Parse(parts[3], CultureInfo.InvariantCulture);

            byte[] allNodes = new byte[length];
            byte[] eod      = new byte[2];

            socket.Read(allNodes, 0, length);
            socket.Read(eod, 0, 2); // data is terminated by \r\n

            this.result       = new CacheItem(flags, new ArraySegment <byte>(allNodes, 0, length));
            this.ConfigResult = this.result;

            string response = TextSocketHelper.ReadResponse(socket);

            if (String.Compare(response, "END", StringComparison.Ordinal) != 0)
            {
                throw new MemcachedClientException("No END was received.");
            }

            var result = new TextOperationResult();

            return(result.Pass());
        }
Example #8
0
        public unsafe bool Read(PooledSocket socket)
        {
            this.StatusCode = -1;

            if (!socket.IsAlive)
            {
                return(false);
            }

            var header = new byte[HeaderLength];

            socket.Read(header, 0, header.Length);

            int dataLength, extraLength;

            DeserializeHeader(header, out dataLength, out extraLength);

            var keyHeader = new byte[4];

            socket.Read(keyHeader, 0, 4);
            var vbucket = BinaryConverter.DecodeUInt16(keyHeader, 0);
            var keylen  = BinaryConverter.DecodeUInt16(keyHeader, 2);

            var keyData = new byte[keylen];

            socket.Read(keyData, 0, keylen);
            Key = BinaryConverter.DecodeKey(keyData);

            var keyStateData = new byte[1];

            socket.Read(keyStateData, 0, keyStateData.Length);
            KeyState = (ObserveKeyState)keyStateData[0];

            var casData = new byte[8];

            socket.Read(casData, 0, casData.Length);
            Cas = BinaryConverter.DecodeUInt64(casData, 0);

            return(this.StatusCode == 0);
        }
Example #9
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);
        }