Exemplo n.º 1
0
        /// <summary>初始化集群</summary>
        protected virtual ICluster <String, ISocketClient> InitCluster()
        {
            var cluster = Cluster;

            if (cluster == null)
            {
                if (UsePool)
                {
                    cluster = new ClientPoolCluster();
                }
                else
                {
                    cluster = new ClientSingleCluster();
                }
                //Cluster = cluster;
            }

            if (cluster is ClientSingleCluster sc && sc.OnCreate == null)
            {
                sc.OnCreate = OnCreate;
            }
            if (cluster is ClientPoolCluster pc && pc.OnCreate == null)
            {
                pc.OnCreate = OnCreate;
            }

            if (cluster.GetItems == null)
            {
                cluster.GetItems = () => Servers;
            }
            cluster.Open();

            return(cluster);
        }
Exemplo n.º 2
0
        /// <summary>打开客户端</summary>
        public virtual Boolean Open()
        {
            if (Active)
            {
                return(true);
            }
            lock (Root)
            {
                if (Active)
                {
                    return(true);
                }

                var ss = Servers;
                if (ss == null || ss.Length == 0)
                {
                    throw new ArgumentNullException(nameof(Servers), "未指定服务端地址");
                }

                if (Encoder == null)
                {
                    Encoder = new JsonEncoder();
                }
                //if (Encoder == null) Encoder = new BinaryEncoder();
                if (Handler == null)
                {
                    Handler = new ApiHandler {
                        Host = this
                    }
                }
                ;

                // 集群
                var cluster = Cluster;
                if (cluster == null)
                {
                    if (UsePool)
                    {
                        cluster = new ClientPoolCluster();
                    }
                    else
                    {
                        cluster = new ClientSingleCluster();
                    }
                    Cluster = cluster;
                }

                if (cluster is ClientSingleCluster sc && sc.OnCreate == null)
                {
                    sc.OnCreate = OnCreate;
                }
                if (cluster is ClientPoolCluster pc && pc.OnCreate == null)
                {
                    pc.OnCreate = OnCreate;
                }

                if (cluster.GetItems == null)
                {
                    cluster.GetItems = () => Servers;
                }
                cluster.Open();

                Encoder.Log = EncoderLog;

                // 拥有默认服务控制器之外的服务时,才显示服务
                var svcs = Manager.Services;
                if (svcs.Any(e => !(e.Value.Controller is ApiController)))
                {
                    ShowService();
                }

                // 控制性能统计信息
                var ms = StatPeriod * 1000;
                if (ms > 0)
                {
                    if (StatInvoke == null)
                    {
                        StatInvoke = new PerfCounter();
                    }
                    if (StatProcess == null)
                    {
                        StatProcess = new PerfCounter();
                    }
                    if (StatSend == null)
                    {
                        StatSend = new PerfCounter();
                    }
                    if (StatReceive == null)
                    {
                        StatReceive = new PerfCounter();
                    }

                    _Timer = new TimerX(DoWork, null, ms, ms)
                    {
                        Async = true
                    };
                }

                return(Active = true);
            }
        }