/// <summary>
 /// 启动TCP服务
 /// </summary>
 /// <param name="host">TCP服务端口信息</param>
 /// <param name="certificateFileName">安全证书文件</param>
 /// <returns>HTTP服务启动状态</returns>
 private startState start(host host, string certificateFileName)
 {
     startState state = startState.TcpError;
     server server = null;
     Monitor.Enter(hostLock);
     try
     {
         if (hosts.TryGetValue(host, out server))
         {
             if (server.CheckCertificate(certificateFileName))
             {
                 ++server.DomainCount;
                 return startState.Success;
             }
             server = null;
             state = startState.CertificateMatchError;
         }
         else
         {
             state = startState.CertificateError;
             server = certificateFileName == null ? new server(this, host) : new sslServer(this, host, certificateFileName);
             state = startState.TcpError;
             if (server.Start())
             {
                 hosts.Add(host, server);
                 return startState.Success;
             }
         }
     }
     catch (Exception error)
     {
         log.Error.Add(error, null, false);
     }
     finally { Monitor.Exit(hostLock); }
     pub.Dispose(ref server);
     return state;
 }
 ///// <summary>
 ///// 停止域名服务
 ///// </summary>
 ///// <param name="domainServer">域名服务</param>
 //private unsafe void stop(domainServer domainServer)
 //{
 //    if (domainServer != null && domainServer.Domains != null)
 //    {
 //        try
 //        {
 //            for (int index = domainServer.Domains.Length; index != 0; )
 //            {
 //                if (Interlocked.CompareExchange(ref domainServer.Domains[--index].Value, 1, 0) == 0)
 //                {
 //                    stop(domainServer.Domains[index].Key);
 //                    Interlocked.Decrement(ref domainServer.DomainCount);
 //                }
 //            }
 //        }
 //        catch (Exception error)
 //        {
 //            log.Default.Add(error, null, false);
 //        }
 //        finally
 //        {
 //            domainServer.Dispose();
 //        }
 //    }
 //}
 /// <summary>
 /// 停止TCP服务
 /// </summary>
 /// <param name="host">TCP服务端口信息</param>
 private void stop(host host)
 {
     server server;
     Monitor.Enter(hostLock);
     try
     {
         if (hosts.TryGetValue(host, out server))
         {
             if (--server.DomainCount == 0) hosts.Remove(host);
             else server = null;
         }
     }
     finally { Monitor.Exit(hostLock); }
     if (server != null) server.Dispose();
 }
 /// <summary>
 /// 设置TCP服务端
 /// </summary>
 /// <param name="tcpServer">TCP服务端</param>
 public void SetTcpServer(fastCSharp.net.tcp.server tcpServer)
 {
     server = tcpServer;
     fileWatcher = new createFlieTimeoutWatcher(fastCSharp.config.processCopy.Default.CheckTimeoutSeconds, onFileWatcherTimeout, fastCSharp.diagnostics.processCopyServer.DefaultFileWatcherFilter);
     if (!fastCSharp.config.pub.Default.IsService && fastCSharp.config.processCopy.Default.WatcherPath != null)
     {
         try
         {
             fileWatcher.Add(fastCSharp.config.processCopy.Default.WatcherPath);
         }
         catch (Exception error)
         {
             log.Error.Add(error, fastCSharp.config.processCopy.Default.WatcherPath, false);
         }
     }
     if (IsLoadCache)
     {
         try
         {
             string cacheFileName = this.cacheFileName;
             if (File.Exists(cacheFileName))
             {
                 interlocked.NoCheckCompareSetSleep0(ref loadCacheLock);
                 try
                 {
                     if (!IsLoadedCache)
                     {
                         saveInfo saveInfo = fastCSharp.emit.dataDeSerializer.DeSerialize<saveInfo>(File.ReadAllBytes(cacheFileName));
                         if (saveInfo.ForwardHost.Port != 0) setForward(saveInfo.ForwardHost);
                         if (saveInfo.Domains.length() != 0)
                         {
                             foreach (saveInfo.domainServer domain in saveInfo.Domains)
                             {
                                 try
                                 {
                                     start(domain.AssemblyPath, domain.ServerType, domain.Domains, domain.IsShareAssembly);
                                 }
                                 catch (Exception error)
                                 {
                                     log.Error.Add(error, null, false);
                                 }
                             }
                         }
                         IsLoadedCache = true;
                     }
                 }
                 finally { loadCacheLock = 0; }
             }
         }
         catch (Exception error)
         {
             log.Error.Add(error, null, false);
         }
     }
 }