Read() public method

Reads data from the server into the specified buffer.
This method blocks and will not return until the specified amount of bytes are read.
public Read ( byte buffer, int offset, int count ) : void
buffer byte An array of that is the storage location for the received data.
offset int The location in buffer to store the received data.
count int The number of bytes to read.
return void
Exemplo n.º 1
0
        public static GetResponse ReadItem(PooledSocket socket)
        {
            string description = socket.ReadResponse();

            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.Flag, retval.Item.Data.Count);
            }

            return(retval);
        }
Exemplo n.º 2
0
		public static GetResponse ReadItem(PooledSocket socket)
		{
			string description = socket.ReadResponse();

			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.Flag, retval.Item.Data.Count);

			return retval;
		}
        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;
        }