示例#1
0
 public STPStartInfo(STPStartInfo stpStartInfo)
     : base(stpStartInfo)
 {
     _idleTimeout      = stpStartInfo.IdleTimeout;
     _minWorkerThreads = stpStartInfo.MinWorkerThreads;
     _maxWorkerThreads = stpStartInfo.MaxWorkerThreads;
     _threadPriority   = stpStartInfo.ThreadPriority;
     _performanceCounterInstanceName = stpStartInfo.PerformanceCounterInstanceName;
     _enableLocalPerformanceCounters = stpStartInfo._enableLocalPerformanceCounters;
 }
	    public STPStartInfo(STPStartInfo stpStartInfo)
            : base(stpStartInfo)
        {
            _idleTimeout = stpStartInfo.IdleTimeout;
            _minWorkerThreads = stpStartInfo.MinWorkerThreads;
            _maxWorkerThreads = stpStartInfo.MaxWorkerThreads;
            _threadPriority = stpStartInfo.ThreadPriority;
            _performanceCounterInstanceName = stpStartInfo.PerformanceCounterInstanceName;
            _enableLocalPerformanceCounters = stpStartInfo._enableLocalPerformanceCounters;
        }
        /// <summary>
        /// 实例化CastleService
        /// </summary>
        /// <param name="config"></param>
        public CastleService(CastleServiceConfiguration config)
        {
            this.config = config;

            if (string.Compare(config.Host, "any", true) == 0)
            {
                config.Host = IPAddress.Loopback.ToString();
                epServer = new ScsTcpEndPoint(config.Port);
            }
            else
                epServer = new ScsTcpEndPoint(config.Host, config.Port);

            this.server = ScsServerFactory.CreateServer(epServer);
            this.server.ClientConnected += server_ClientConnected;
            this.server.ClientDisconnected += server_ClientDisconnected;
            this.server.WireProtocolFactory = new CustomWireProtocolFactory(config.Compress, config.Encrypt);

            //服务端注入内存处理
            this.container = new SimpleServiceContainer(CastleFactoryType.Local);
            this.container.OnError += error => { if (OnError != null) OnError(error); };
            this.container.OnLog += (log, type) => { if (OnLog != null) OnLog(log, type); };

            //实例化SmartThreadPool
            var stp = new STPStartInfo
            {
                IdleTimeout = config.Timeout * 1000,
                MaxWorkerThreads = Math.Max(config.MaxCalls, 10),
                MinWorkerThreads = 5,
                ThreadPriority = ThreadPriority.Normal,
                WorkItemPriority = WorkItemPriority.Normal
            };

            //创建线程池
            smart = new SmartThreadPool(stp);
            smart.Start();

            //创建并发任务组
            var group = smart.CreateWorkItemsGroup(2);
            group.Start();

            //实例化调用者
            var status = new ServerStatusService(server, config, container);
            this.caller = new ServiceCaller(group, status);

            //判断是否启用httpServer
            if (config.HttpEnabled)
            {
                //设置默认的解析器
                IHttpApiResolver resolver = new DefaultApiResolver();

                //判断是否配置了HttpType
                if (config.HttpType != null && typeof(IHttpApiResolver).IsAssignableFrom(config.HttpType))
                {
                    resolver = Activator.CreateInstance(config.HttpType) as IHttpApiResolver;
                }

                var httpCaller = new HttpServiceCaller(group, config, container);

                //刷新服务委托
                status.OnRefresh += () => httpCaller.InitCaller(resolver);

                //初始化调用器
                httpCaller.InitCaller(resolver);

                var handler = new HttpServiceHandler(httpCaller);
                var factory = new HttpRequestHandlerFactory(handler);
                this.httpServer = new HTTPServer(factory, config.HttpPort);
            }

            //绑定事件
            MessageCenter.Instance.OnError += Instance_OnError;
        }