/// <summary> /// HttpServiceCaller初始化 /// </summary> /// <param name="group"></param> /// <param name="config"></param> /// <param name="container"></param> public HttpServiceCaller(IWorkItemsGroup group, CastleServiceConfiguration config, IServiceContainer container) { this.config = config; this.container = container; this.smart = group; this.callers = new HttpCallerInfoCollection(); this.callTimeouts = new Dictionary <string, int>(); }
/// <summary> /// 初始化方法 /// </summary> public void Init() { this.config = CastleServiceConfiguration.GetConfig(); this.server = new CastleService(config); this.server.OnLog += new LogEventHandler(server_OnLog); this.server.OnError += new ErrorLogEventHandler(server_OnError); //处理邮件地址 string address = ConfigurationManager.AppSettings["SendMailAddress"]; if (!string.IsNullOrEmpty(address)) { mailTo = address.Split(',', ';', '|'); } }
//private static readonly IMongo mongo = new Mongo("mongodb://192.168.1.223"); static void Main(string[] args) { System.Console.BackgroundColor = ConsoleColor.DarkBlue; System.Console.ForegroundColor = ConsoleColor.White; Program_OnLog("Service ready started...", LogType.Normal); var config = CastleServiceConfiguration.GetConfig(); var server = new CastleService(config); server.OnLog += new LogEventHandler(Program_OnLog); server.OnError += new ErrorLogEventHandler(Program_OnError); server.Start(); Program_OnLog(string.Format("Tcp server started. {0}", server.ServerUrl), LogType.Normal); Program_OnLog(string.Format("Service count -> {0} services.", server.ServiceCount), LogType.Normal); Program_OnLog(string.Format("Press any key to exit and stop service..."), LogType.Normal); System.Console.ReadLine(); }
/// <summary> /// 实例化ServerStatusService /// </summary> /// <param name="server"></param> /// <param name="container"></param> /// <param name="config"></param> public ServerStatusService(IScsServer server, CastleServiceConfiguration config, IServiceContainer container) { this.config = config; this.server = server; this.container = container; this.startTime = DateTime.Now; this.statuslist = new TimeStatusCollection(config.RecordHours * 3600); this.counterlist = new CounterInfoCollection(config.MinuteCalls); //启动定义推送线程 var threadPush = new Thread(DoPushWork); threadPush.IsBackground = true; threadPush.Start(); //启动自动检测线程 var threadCheck = new Thread(DoCheckWork); threadCheck.IsBackground = true; threadCheck.Start(); }
/// <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; }