Exemplo n.º 1
0
        public static async Task <GetResponse> ReadItemAsync(PooledSocket socket, CancellationToken cancellationToken = default)
        {
            var description = await TextSocketHelper.ReadResponseAsync(socket, cancellationToken).ConfigureAwait(false);

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

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

            // response is:
            // VALUE <key> <flags> <bytes> [<cas unique>]
            // 0     1     2       3       4
            //
            // cas only exists in 1.2.4+
            //

            ulong cas   = 0;
            var   parts = description.Split(' ');

            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);
            }

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

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

            await socket.ReceiveAsync(allData, 0, length, cancellationToken).ConfigureAwait(false);

            await socket.ReceiveAsync(eod, 0, 2, cancellationToken).ConfigureAwait(false);             // data is terminated by \r\n

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

            GetHelper.Logger = GetHelper.Logger ?? Caching.Logger.CreateLogger(typeof(GetHelper));
            if (GetHelper.Logger.IsEnabled(LogLevel.Debug))
            {
                GetHelper.Logger.LogDebug("Received value. Data type: {0}, size: {1}.", result.Item.Flags, result.Item.Data.Count);
            }

            return(result);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        protected internal override ValueTask <IOperationResult> ReadResponseAsync(PooledSocket socket)
        {
            GetResponse r      = GetHelper.ReadItem(socket);
            var         result = new TextOperationResult();

            if (r == null)
            {
                return(new ValueTask <IOperationResult>(result.Fail("Failed to read response")));
            }

            this.result = r.Item;
            this.Cas    = r.CasValue;

            GetHelper.FinishCurrent(socket);

            return(new ValueTask <IOperationResult>(result.Pass()));
        }
Exemplo n.º 4
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;
		}