Esempio n. 1
0
        public int SendCommand(string command, SocketManager write, IDictionary <string, byte[]> datas, params string[] args)
        {
            // http://redis.io/topics/protocol

            StringBuilder sb = new StringBuilder();

            int acount = args != null ? args.Length + 1 : 1;

            acount += datas != null && datas.Count > 0 ? datas.Count * 2 : 0;

            sb.AppendFormat("*{0}\r\n", acount);

            var cmd = command.ToString();

            sb.AppendFormat("${0}\r\n{1}\r\n", cmd.Length, cmd);

            if (args != null)
            {
                foreach (var arg in args)
                {
                    sb.AppendFormat("${0}\r\n{1}\r\n", arg.Length, arg);
                }
            }
            log.Info(sb);
            MemoryStream ms = new MemoryStream();

            byte[] r = Encoding.UTF8.GetBytes(sb.ToString());
            ms.Write(r, 0, r.Length);

            if (datas != null && datas.Count > 0)
            {
                foreach (var data in datas)
                {
                    r = Encoding.UTF8.GetBytes(string.Format("${0}\r\n{1}\r\n", data.Key.Length, data.Key));
                    ms.Write(r, 0, r.Length);
                    r = Encoding.UTF8.GetBytes(string.Format("${0}\r\n", data.Value.Length));
                    ms.Write(r, 0, r.Length);
                    ms.Write(data.Value, 0, data.Value.Length);
                    ms.Write(_end_data, 0, 2);
                }
            }

            if (write == null)
            {
                write = GetRead();
            }
            var socket = write.Client;

            try
            {
                socket.Send(ms.ToArray());

                string msg = ReadLine(write).Item1;

                if (msg.Contains("-MOVED"))
                {
                    string[]  m     = msg.ToString().Split(' ');
                    string[]  h     = m[2].Split(':');
                    string    host  = h[0];
                    int       port  = int.Parse(h[1]);
                    RedisItem items = GetItem(Writes, host, port);
                    items.Clients.TryPop(out write);
                    items.Clients.Push(write);
                    socket = write.Client;
                    socket.Send(ms.ToArray());
                    msg = ReadLine(write).Item1;
                    log.Info(msg);
                }
                if (msg.IndexOf("+") > -1)
                {
                    return(1);
                }
                if (msg.IndexOf(":") > -1)
                {
                    return(int.Parse(msg.Replace(":", "")));
                }
            }
            catch (SocketException e)
            {
                throw e;
            }
            return(0);
        }
Esempio n. 2
0
        public string SendCommand(string command, SocketManager read, params string[] args)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("*{0}\r\n", args != null ? args.Length + 1 : 1);

            var cmd = command.ToString();

            sb.AppendFormat("${0}\r\n{1}\r\n", cmd.Length, cmd);

            if (args != null)
            {
                foreach (var arg in args)
                {
                    sb.AppendFormat("${0}\r\n{1}\r\n", arg.Length, arg);
                }
            }

            byte[] r = Encoding.UTF8.GetBytes(sb.ToString());
            if (read == null)
            {
                read = GetRead();
            }
            var socket = read.Client;

            try
            {
                socket.Send(r);
                string msg = ReadLine(read).Item1;
                log.Info(msg);
                if (msg.Contains("-MOVED"))
                {
                    string[]  m     = msg.ToString().Split(' ');
                    string[]  h     = m[2].Split(':');
                    string    host  = h[0];
                    int       port  = int.Parse(h[1]);
                    RedisItem items = GetItem(Reads, host, port);
                    items.Clients.TryPop(out read);
                    items.Clients.Push(read);
                    socket = read.Client;
                    socket.Send(r);
                    msg = ReadLine(read).Item1;
                    log.Info(msg);
                }
                if (!msg.Contains("$-1") && !msg.Contains("-MOVED"))
                {
                    if (msg.IndexOf("$") > -1)
                    {
                        int l = int.Parse(msg.Replace("$", ""));
                        msg = ReadLine(read) + "\r\n";
                        if (msg.Length > l)
                        {
                            return(msg);
                        }
                        while (true)
                        {
                            msg = msg + ReadLine(read).Item1 + "\r\n";
                            if (msg.Length > l)
                            {
                                break;
                            }
                        }
                        log.Info(msg);
                        return(msg);
                    }
                    ///读取对象长度
                    if (msg.IndexOf("*") > -1)
                    {
                        string result = string.Empty;
                        int    l      = int.Parse(msg.Replace("*", ""));
                        if (msg.IndexOf("-") == 1)
                        {
                            return("0");
                        }
                        for (int i = 0; i < l * 2; i++)
                        {
                            string val = ReadLine(read).Item1;
                            result = result + val + "\r\n";
                        }

                        return(result);
                    }
                    //操作影响行数 删除
                    if (msg.IndexOf(":") > -1)
                    {
                        int result = int.Parse(msg.Replace(":", ""));

                        return(result.ToString());
                    }
                    if (msg.IndexOf("+") > -1)
                    {
                        return("1");
                    }
                    if (msg.IndexOf("-") == 0)
                    {
                        return("0");
                    }
                    ;
                }
            }
            catch (SocketException e)
            {
                return("0");
            }

            return("0");
        }
Esempio n. 3
0
        public int SendCommand(string command, SocketManager write, byte[] datas, params string[] args)
        {
            StringBuilder sb = new StringBuilder();

            int acount = args != null ? args.Length + 1 : 1;

            acount += datas != null && datas.Length > 0 ? 1 : 0;

            sb.AppendFormat("*{0}\r\n", acount);

            var cmd = command.ToString();

            sb.AppendFormat("${0}\r\n{1}\r\n", cmd.Length, cmd);

            if (args != null)
            {
                foreach (var arg in args)
                {
                    sb.AppendFormat("${0}\r\n{1}\r\n", arg.Length, arg);
                }
            }
            sb.AppendFormat("${0}\r\n", datas.Length);
            log.Info(sb);
            byte[] r = Encoding.UTF8.GetBytes(sb.ToString());
            if (write == null)
            {
                write = GetWrite();
            }
            var socket = write.Client;

            try
            {
                socket.Send(r);


                if (datas != null && datas.Length > 0)
                {
                    socket.Send(datas);
                    socket.Send(_end_data);
                }

                string msg = ReadLine(write).Item1;
                log.Info(msg);
                if (msg.Contains("-MOVED"))
                {
                    string[]  m     = msg.ToString().Split(' ');
                    string[]  h     = m[2].Split(':');
                    string    host  = h[0];
                    int       port  = int.Parse(h[1]);
                    RedisItem items = GetItem(Writes, host, port);
                    items.Clients.TryPop(out write);
                    items.Clients.Push(write);
                    socket = write.Client;
                    socket.Send(r);
                    if (datas != null && datas.Length > 0)
                    {
                        socket.Send(datas);
                        socket.Send(_end_data);
                    }
                    msg = ReadLine(write).Item1;
                    log.Info(msg);
                }
                if (msg.IndexOf("+") > -1)
                {
                    return(1);
                }
                if (msg.IndexOf(":") > -1)
                {
                    return(int.Parse(msg.Replace(":", "")));
                }
            }
            catch (SocketException e)
            {
                return(0);
            }
            return(0);
        }