/// <summary>
 ///     启动TCP服务
 /// </summary>
 /// <param name="host">TCP服务端口信息</param>
 /// <param name="certificateFileName">安全证书文件</param>
 /// <returns>HTTP服务启动状态</returns>
 private TmphStartState start(TmphHost host, string certificateFileName)
 {
     var state = TmphStartState.TcpError;
     TmphServer server = null;
     Monitor.Enter(hostLock);
     try
     {
         if (hosts.TryGetValue(host, out server))
         {
             if (server.CheckCertificate(certificateFileName))
             {
                 ++server.DomainCount;
                 return TmphStartState.Success;
             }
             server = null;
             state = TmphStartState.CertificateMatchError;
         }
         else
         {
             state = TmphStartState.CertificateError;
             server = certificateFileName == null
                 ? new TmphServer(this, host)
                 : new TmphSslServer(this, host, certificateFileName);
             state = TmphStartState.TcpError;
             if (server.Start())
             {
                 hosts.Add(host, server);
                 return TmphStartState.Success;
             }
         }
     }
     catch (Exception error)
     {
         TmphLog.Error.Add(error, null, false);
     }
     finally
     {
         Monitor.Exit(hostLock);
     }
     TmphPub.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(TmphHost host)
 {
     TmphServer 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(Tcp.TmphServer tcpServer)
 {
     server = tcpServer;
     fileWatcher = new TmphCreateFlieTimeoutWatcher(TmphProcessCopy.Default.CheckTimeoutSeconds, onFileWatcherTimeout,
         TmphProcessCopyServer.DefaultFileWatcherFilter);
     if (!Config.TmphPub.Default.IsService && TmphProcessCopy.Default.WatcherPath != null)
     {
         try
         {
             fileWatcher.Add(TmphProcessCopy.Default.WatcherPath);
         }
         catch (Exception error)
         {
             TmphLog.Error.Add(error, TmphProcessCopy.Default.WatcherPath, false);
         }
     }
     if (IsLoadCache)
     {
         try
         {
             var cacheFileName = this.cacheFileName;
             if (File.Exists(cacheFileName))
             {
                 TmphInterlocked.NoCheckCompareSetSleep0(ref loadCacheLock);
                 try
                 {
                     if (!IsLoadedCache)
                     {
                         var saveInfo =
                             TmphDataDeSerializer.DeSerialize<TmphSaveInfo>(File.ReadAllBytes(cacheFileName));
                         if (saveInfo.ForwardHost.Port != 0) setForward(saveInfo.ForwardHost);
                         if (saveInfo.Domains.length() != 0)
                         {
                             foreach (var domain in saveInfo.Domains)
                             {
                                 try
                                 {
                                     start(domain.AssemblyPath, domain.ServerType, domain.Domains,
                                         domain.IsShareAssembly);
                                 }
                                 catch (Exception error)
                                 {
                                     TmphLog.Error.Add(error, null, false);
                                 }
                             }
                         }
                         IsLoadedCache = true;
                     }
                 }
                 finally
                 {
                     loadCacheLock = 0;
                 }
             }
         }
         catch (Exception error)
         {
             TmphLog.Error.Add(error, null, false);
         }
     }
 }