예제 #1
0
        /// <summary>
        /// Initialize and start new TcpClientHandler
        /// </summary>
        /// <param name="client">TcpClient to Handle</param>
        public MemTcpClient(TcpClient client, ProtocolType protocol)
        {
            _logManager = new LogManager(client.Client.RemoteEndPoint.ToString());
            _logManager.Info("MemTcpClient", "\tNew client connected on protocol :" + protocol.ToString());
            
            _client = client;
            _stream = _client.GetStream();

            _executionManager = new SequentialExecutionManager(_logManager);

            _inputBuffer = new byte[MAX_BUFFER_SIZE];
            _inputDataStream = new DataStream();

            _protocol = protocol;
            if (protocol == ProtocolType.Text)
            {
                _parser = new TextProtocolParser(_inputDataStream, this, _logManager);
                _responseManager = new TextResponseManager(_stream, _logManager);
            }
            else
            {
                _parser = new BinaryProtocolParser(_inputDataStream, this, _logManager);
                _responseManager = new BinaryResponseManager(_stream, _logManager);
            }
            _responseManager.MemTcpClient = this;

            _parser.CommandConsumer = _executionManager;
            _executionManager.CommandConsumer = _responseManager;
        }
예제 #2
0
        public static byte[] BuildStatsResponse(StatsCommand command)
        {
            DataStream stream = new DataStream();
            Hashtable stats= command.OperationResult.Value as Hashtable;
            IDictionaryEnumerator ie = stats.GetEnumerator();

            string key = "";
            byte [] value = null;
            while (ie.MoveNext())
            {
                key = ie.Key as string;
                value = MemcachedEncoding.BinaryConverter.GetBytes(ie.Value as string);
                stream.Write(BuildResposne(command.Opcode, BinaryResponseStatus.no_error, command.Opaque, 0, key, value, null));
            }
            
            stream.Write(BuildResposne(command.Opcode, BinaryResponseStatus.no_error, command.Opaque, 0, null, null, null));
            return stream.ReadAll();
        }
예제 #3
0
        private byte[] BuildTextResponse(AbstractCommand command)
        {
            _logManager.Debug("TextResponseManager.BuildTextResponse", "Building response for command : " + command.Opcode);

            if (command.NoReply)
                return new byte[]{};

            if(command.ExceptionOccured)
                return MemcachedEncoding.BinaryConverter.GetBytes("SERVER_ERROR " + command.ErrorMessage + "\r\n");
            if (command.ErrorMessage != null)
                return MemcachedEncoding.BinaryConverter.GetBytes(command.ErrorMessage + "\r\n");

            DataStream resultStream = new DataStream();

            switch (command.Opcode)
            {
                case Opcode.Set:
                case Opcode.Add:
                case Opcode.Replace:
                case Opcode.Append:
                case Opcode.Prepend:
                    if (command.OperationResult.ReturnResult == Result.SUCCESS)
                    {
                        resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("STORED\r\n"));
                    }
                    else
                    {
                        resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("NOT_STORED\r\n"));
                    }
                    break;
                case Opcode.CAS:
                    switch (command.OperationResult.ReturnResult)
                    {
                        case Result.SUCCESS:
                            resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("STORED\r\n"));
                            break;
                        case Result.ITEM_MODIFIED:
                            resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("EXISTS\r\n"));
                            break;
                        case Result.ITEM_NOT_FOUND:
                            resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("NOT_FOUND\r\n"));
                            break;
                        default:
                            break;
                    }
                    break;
                case Opcode.Get:
                case Opcode.Gets:
                    List<GetOpResult> results = (command as GetCommand).Results;
                    foreach (GetOpResult result in results)
                    {
                        if (result == null)
                            continue;
                        byte[] value=result.Value as byte[];
                        string valueString = null;
                        if(command.Opcode==Opcode.Get)
                            valueString = string.Format("VALUE {0} {1} {2}\r\n", result.Key, result.Flag, value.Length);
                        else
                            valueString = string.Format("VALUE {0} {1} {2} {3}\r\n", result.Key, result.Flag, value.Length, result.Version);
                        resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes(valueString));
                        resultStream.Write(value);
                        resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("\r\n"));
                    }
                    resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes( "END\r\n"));
                    break;
                case Opcode.Increment:
                case Opcode.Decrement:
                    switch (command.OperationResult.ReturnResult)
                    {
                        case Result.SUCCESS:
                            long value = (long) (command.OperationResult as MutateOpResult).MutateResult;
                            resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes(value.ToString() + "\r\n"));
                            break;
                        case Result.ITEM_TYPE_MISMATCHED:
                            resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("CLIENT_ERROR cannot increment or decrement non-numeric value\r\n"));
                            break;
                        case Result.ITEM_NOT_FOUND:
                            resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("NOT_FOUND\r\n"));
                            break;
                        default:
                            resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("ERROR\r\n"));
                            break;
                    }
                    break;
                case Opcode.Delete:
                    if (command.OperationResult.ReturnResult == Result.SUCCESS)
                        resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("DELETED\r\n"));
                    else
                        resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("NOT_FOUND\r\n"));
                    break;
                case Opcode.Touch:
                    if (command.OperationResult.ReturnResult == Result.SUCCESS)
                        resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("TOUCHED\r\n"));
                    else
                        resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("NOT_FOUND\r\n"));
                    break;
                case Opcode.Flush:
                    resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("OK\r\n"));
                        break;
                case Opcode.Version:
                        string version = command.OperationResult.Value as string;
                        resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes(version + "\r\n"));
                        break;
                case Opcode.Verbosity:
                case Opcode.Slabs_Reassign:
                case Opcode.Slabs_Automove:
                        if (command.OperationResult.ReturnResult == Result.SUCCESS)
                            resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("OK\r\n"));
                    else
                            resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("ERROR\r\n"));
                    break;
                case Opcode.Stat:
                    Hashtable stats = command.OperationResult.Value as Hashtable;
                    if (stats == null)
                    {
                        resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("END\r\n"));
                        break;
                    }
                    IDictionaryEnumerator ie = stats.GetEnumerator();
                    string statString = null;
                    while (ie.MoveNext())
                    {
                        statString = string.Format("STAT {0} {1}\r\n",ie.Key,ie.Value);
                        resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes(statString));
                    }
                    resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("END\r\n"));
                    break;
                case Opcode.Quit:
                    TcpNetworkGateway.DisposeClient(_memTcpClient);
                    break;
            }

            return resultStream.ReadAll();
        }
예제 #4
0
 public BinaryProtocolParser(DataStream inputSream, MemTcpClient parent, LogManager logManager)
     : base(inputSream, parent, logManager)
 {
 }
예제 #5
0
 public ProtocolParser(DataStream inputStream, MemTcpClient parent, LogManager logManager)
 {
     _inputDataStream = inputStream;
     _memTcpClient = parent;
     _logManager = logManager;
 }