protected internal override IOperationResult ReadResponse(PooledSocket socket)
 {
     return(new TextOperationResult
     {
         Success = String.Compare(TextSocketHelper.ReadResponse(socket), "DELETED", StringComparison.Ordinal) == 0
     });
 }
        /// <summary>
        /// Reads the response of the server.
        /// </summary>
        /// <returns>The data sent by the memcached server.</returns>
        /// <exception cref="T:System.InvalidOperationException">The server did not sent a response or an empty line was returned.</exception>
        /// <exception cref="T:IkCaching.Memcached.MemcachedException">The server did not specified any reason just returned the string ERROR. - or - The server returned a SERVER_ERROR, in this case the Message of the exception is the message returned by the server.</exception>
        /// <exception cref="T:IkCaching.Memcached.MemcachedClientException">The server did not recognize the request sent by the client. The Message of the exception is the message returned by the server.</exception>
        public static string ReadResponse(PooledSocket socket)
        {
            string response = TextSocketHelper.ReadLine(socket);

            if (log.IsDebugEnabled)
            {
                log.Debug("Received response: " + response);
            }

            if (String.IsNullOrEmpty(response))
            {
                throw new MemcachedClientException("Empty response received.");
            }

            if (String.Compare(response, GenericErrorResponse, StringComparison.Ordinal) == 0)
            {
                throw new NotSupportedException("Operation is not supported by the server or the request was malformed. If the latter please report the bug to the developers.");
            }

            if (response.Length >= ErrorResponseLength)
            {
                if (String.Compare(response, 0, ClientErrorResponse, 0, ErrorResponseLength, StringComparison.Ordinal) == 0)
                {
                    throw new MemcachedClientException(response.Remove(0, ErrorResponseLength));
                }
                else if (String.Compare(response, 0, ServerErrorResponse, 0, ErrorResponseLength, StringComparison.Ordinal) == 0)
                {
                    throw new MemcachedException(response.Remove(0, ErrorResponseLength));
                }
            }

            return(response);
        }
Exemple #3
0
        protected internal override IList <ArraySegment <byte> > GetBuffer()
        {
            // gets key1 key2 key3 ... keyN\r\n

            var command = "gets " + String.Join(" ", Keys.ToArray()) + TextSocketHelper.CommandTerminator;

            return(TextSocketHelper.GetCommandBuffer(command));
        }
Exemple #4
0
        protected internal override IList <ArraySegment <byte> > GetBuffer()
        {
            var command = String.IsNullOrEmpty(this.type)
                                                        ? "stats" + TextSocketHelper.CommandTerminator
                                                        : "stats " + this.type + TextSocketHelper.CommandTerminator;

            return(TextSocketHelper.GetCommandBuffer(command));
        }
Exemple #5
0
        public static void FinishCurrent(PooledSocket socket)
        {
            string response = TextSocketHelper.ReadResponse(socket);

            if (String.Compare(response, "END", StringComparison.Ordinal) != 0)
            {
                throw new MemcachedClientException("No END was received.");
            }
        }
        protected internal override IList <ArraySegment <byte> > GetBuffer()
        {
            var command = (this.mode == MutationMode.Increment ? "incr " : "decr ")
                          + this.Key
                          + " "
                          + this.delta.ToString(CultureInfo.InvariantCulture)
                          + TextSocketHelper.CommandTerminator;

            return(TextSocketHelper.GetCommandBuffer(command));
        }
Exemple #7
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);
        }
        protected internal override IOperationResult ReadResponse(PooledSocket socket)
        {
            string response = TextSocketHelper.ReadResponse(socket);
            var    result   = new TextOperationResult();

            //maybe we should throw an exception when the item is not found?
            if (String.Compare(response, "NOT_FOUND", StringComparison.Ordinal) == 0)
            {
                return(result.Fail("Failed to read response.  Item not found"));
            }

            result.Success =
                UInt64.TryParse(response, NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite, CultureInfo.InvariantCulture, out this.result);
            return(result);
        }
Exemple #9
0
        protected internal override System.Collections.Generic.IList <ArraySegment <byte> > GetBuffer()
        {
            // todo adjust the size to fit a request using a fnv hashed key
            var sb      = new StringBuilder(128);
            var buffers = new List <ArraySegment <byte> >(3);

            switch (this.command)
            {
            case StoreCommand.Add: sb.Append("add "); break;

            case StoreCommand.Replace: sb.Append("replace "); break;

            case StoreCommand.Set: sb.Append("set "); break;

            case StoreCommand.Append: sb.Append("append "); break;

            case StoreCommand.Prepend: sb.Append("prepend "); break;

            case StoreCommand.CheckAndSet: sb.Append("cas "); break;

            default: throw new MemcachedClientException(command + " is not supported.");
            }

            sb.Append(this.Key);
            sb.Append(" ");
            sb.Append(this.value.Flags.ToString(CultureInfo.InvariantCulture));
            sb.Append(" ");
            sb.Append(this.expires.ToString(CultureInfo.InvariantCulture));
            sb.Append(" ");

            var data = this.value.Data;

            sb.Append(Convert.ToString(data.Count, CultureInfo.InvariantCulture));

            if (command == StoreCommand.CheckAndSet)
            {
                sb.Append(" ");
                sb.Append(Convert.ToString(this.cas, CultureInfo.InvariantCulture));
            }

            sb.Append(TextSocketHelper.CommandTerminator);

            TextSocketHelper.GetCommandBuffer(sb.ToString(), buffers);
            buffers.Add(data);
            buffers.Add(StoreOperationBase.DataTerminator);

            return(buffers);
        }
Exemple #10
0
        protected internal override IOperationResult ReadResponse(PooledSocket socket)
        {
            var serverData = new Dictionary <string, string>();

            while (true)
            {
                string line = TextSocketHelper.ReadResponse(socket);

                // stat values are terminated by END
                if (String.Compare(line, "END", StringComparison.Ordinal) == 0)
                {
                    break;
                }

                // expected response is STAT item_name item_value
                if (line.Length < 6 || String.Compare(line, 0, "STAT ", 0, 5, StringComparison.Ordinal) != 0)
                {
                    if (log.IsWarnEnabled)
                    {
                        log.Warn("Unknow response: " + line);
                    }

                    continue;
                }

                // get the key&value
                string[] parts = line.Remove(0, 5).Split(' ');
                if (parts.Length != 2)
                {
                    if (log.IsWarnEnabled)
                    {
                        log.Warn("Unknow response: " + line);
                    }

                    continue;
                }

                // store the stat item
                serverData[parts[0]] = parts[1];
            }

            this.result = serverData;

            return(new TextOperationResult().Pass());
        }
Exemple #11
0
        protected internal override System.Collections.Generic.IList <System.ArraySegment <byte> > GetBuffer()
        {
            var command = "gets " + this.Key + TextSocketHelper.CommandTerminator;

            return(TextSocketHelper.GetCommandBuffer(command));
        }
 protected internal override IOperationResult ReadResponse(PooledSocket socket)
 {
     TextSocketHelper.ReadResponse(socket);
     return(new TextOperationResult().Pass());
 }
 protected internal override IList <System.ArraySegment <byte> > GetBuffer()
 {
     return(TextSocketHelper.GetCommandBuffer("flush_all" + TextSocketHelper.CommandTerminator));
 }