Exemplo n.º 1
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
                    }
                }
                ;

                // 集群
                Cluster = InitCluster();
                WriteLog("集群:{0}", Cluster);

                Encoder.Log = EncoderLog;

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

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

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

            if (Client == null)
            {
                throw new ArgumentNullException(nameof(Client), "未指定通信客户端");
            }
            //if (Encoder == null) throw new ArgumentNullException(nameof(Encoder), "未指定编码器");

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

            Encoder.Log = EncoderLog;

            // 设置过滤器
            SetFilter();

            Client.Provider = this;
            Client.Log      = Log;
            Client.Opened  += Client_Opened;
            if (!Client.Open())
            {
                return(false);
            }

            var ms = Manager.Services;

            if (ms.Count > 0)
            {
                Log.Info("客户端可用接口{0}个:", ms.Count);
                foreach (var item in ms)
                {
                    Log.Info("\t{0,-16}\t{1}", item.Key, item.Value);
                }
            }

            // 打开连接后马上就可以登录
            Timer = new TimerX(OnTimer, this, 0, 30000);

            return(Active = true);
        }
Exemplo n.º 3
0
        /// <summary>开始服务</summary>
        public virtual void Start()
        {
            if (Active)
            {
                return;
            }

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

            Encoder.Log = EncoderLog;

            // 设置过滤器
            SetFilter();

            Log.Info("启动{0},共有服务器{1}个", GetType().Name, Servers.Count);
            Log.Info("编码:{0}", Encoder);
            Log.Info("处理:{0}", Handler);

            foreach (var item in Servers)
            {
                if (item.Handler == null)
                {
                    item.Handler = Handler;
                }
                if (item.Encoder == null)
                {
                    item.Encoder = Encoder;
                }
                item.Provider = this;
                item.Log      = Log;
                item.Start();
            }

            Log.Info("服务端可用接口{0}个:", Manager.Services.Count);
            foreach (var item in Manager.Services)
            {
                Log.Info("\t{0,-16}\t{1}", item.Key, item.Value);
            }

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

            var ct = Client;

            if (ct == null)
            {
                throw new ArgumentNullException(nameof(Client), "未指定通信客户端");
            }

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

            Encoder.Log = EncoderLog;

            // 设置过滤器
            SetFilter();

            ct.Provider = this;
            ct.Log      = Log;
            ct.Opened  += Client_Opened;

            // 打开网络连接
            if (!ct.Open())
            {
                return(false);
            }

            ShowService();

            // 打开连接后马上就可以登录
            Timer = new TimerX(OnTimer, this, 0, 30000);

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

            if (Client == null)
            {
                throw new ArgumentNullException(nameof(Client), "未指定通信客户端");
            }
            //if (Encoder == null) throw new ArgumentNullException(nameof(Encoder), "未指定编码器");

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

#if DEBUG
            Client.Log  = Log;
            Encoder.Log = Log;
#endif

            Client.Opened += Client_Opened;
            if (!Client.Open())
            {
                return(false);
            }

            var ms = Manager.Services;
            if (ms.Count > 0)
            {
                Log.Info("客户端可用接口{0}个:", ms.Count);
                foreach (var item in ms)
                {
                    Log.Info("\t{0,-16}{1}", item.Key, item.Value);
                }
            }

            return(Active = true);
        }
Exemplo n.º 6
0
        /// <summary>开始服务</summary>
        public virtual void Start()
        {
            if (Active)
            {
                return;
            }

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

            Encoder.Log = EncoderLog;

            // 设置过滤器
            SetFilter();

            Log.Info("启动{0},共有服务器{1}个", GetType().Name, Servers.Count);
            Log.Info("编码:{0}", Encoder);
            Log.Info("处理:{0}", Handler);

            foreach (var item in Servers)
            {
                if (item.Handler == null)
                {
                    item.Handler = Handler;
                }
                if (item.Encoder == null)
                {
                    item.Encoder = Encoder;
                }
                item.Provider = this;
                item.Log      = Log;
                item.Start();
            }

            ShowService();

            Active = true;
        }
Exemplo n.º 7
0
        /// <summary>开始服务</summary>
        public virtual void Start()
        {
            if (Active)
            {
                return;
            }

#if DEBUG
            Encoder.Log = Log;
#endif
            if (Encoder == null)
            {
                Encoder = new JsonEncoder();
            }
            if (Handler == null)
            {
                Handler = new ApiHandler {
                    Host = this
                }
            }
            ;

            Log.Info("启动{0},共有服务器{1}个 编码:{2} 处理器:{3}", GetType().Name, Servers.Count, Encoder, Handler);

            foreach (var item in Servers)
            {
                if (item.Handler == null)
                {
                    item.Handler = Handler;
                }
                if (item.Encoder == null)
                {
                    item.Encoder = Encoder;
                }
                item.Provider = this;
                item.Log      = Log;
                item.Start();
            }

            Log.Info("服务端可用接口{0}个:", Manager.Services.Count);
            foreach (var item in Manager.Services)
            {
                Log.Info("\t{0,-16}{1}", item.Key, item.Value);
            }

            Active = true;
        }
Exemplo n.º 8
0
        /// <summary>开始服务</summary>
        public virtual void Start()
        {
            if (Active)
            {
                return;
            }

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

            Encoder.Log = EncoderLog;

            Log.Info("启动{0},服务器 {1}", GetType().Name, Server);
            Log.Info("编码:{0}", Encoder);
            //Log.Info("处理:{0}", Handler);

            var svr = Server;

            if (svr.Handler == null)
            {
                svr.Handler = Handler;
            }
            if (svr.Encoder == null)
            {
                svr.Encoder = Encoder;
            }
            svr.Host = this;
            //svr.Log = Log;
            svr.Start();

            ShowService();

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

            var ct = Client;

            if (ct == null)
            {
                throw new ArgumentNullException(nameof(Client), "未指定通信客户端");
            }

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

            Encoder.Log = EncoderLog;

            //ct.Log = Log;

            // 打开网络连接
            if (!ct.Open())
            {
                return(false);
            }

            ShowService();

            return(Active = true);
        }
Exemplo n.º 10
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
                    }
                }
                ;

                // 集群
                Cluster = InitCluster();
                WriteLog("集群:{0}", Cluster);

                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);
            }
        }
Exemplo n.º 11
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 (Pool == null)
                {
                    Pool = new MyPool {
                        Host = this
                    }
                }
                ;

                if (Encoder == null)
                {
                    Encoder = new JsonEncoder();
                }
                //if (Encoder == null) Encoder = new BinaryEncoder();
                if (Handler == null)
                {
                    Handler = new ApiHandler {
                        Host = this
                    }
                }
                ;
                if (StatInvoke == null)
                {
                    StatInvoke = new PerfCounter();
                }
                if (StatProcess == null)
                {
                    StatProcess = new PerfCounter();
                }
                if (StatSend == null)
                {
                    StatSend = new PerfCounter();
                }
                if (StatReceive == null)
                {
                    StatReceive = new PerfCounter();
                }

                Encoder.Log = EncoderLog;

                // 不要阻塞打开,各个线程从池里借出连接来使用
                //var ct = Pool.Get();
                //try
                //{
                //    // 打开网络连接
                //    if (!ct.Open()) return false;
                //}
                //finally
                //{
                //    Pool.Put(ct);
                //}

                ShowService();

                var ms = StatPeriod * 1000;
                if (ms > 0)
                {
                    _Timer = new TimerX(DoWork, null, ms, ms)
                    {
                        Async = true
                    }
                }
                ;

                return(Active = true);
            }
        }
Exemplo n.º 12
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);
            }
        }