コード例 #1
0
 /// <summary>
 /// 设置TCP服务端
 /// </summary>
 /// <param name="tcpServer">TCP服务端</param>
 public override void SetTcpServer(AutoCSer.Net.TcpInternalServer.Server tcpServer)
 {
     base.SetTcpServer(tcpServer);
     cacheFileName = AutoCSer.Config.Pub.Default.CachePath + ServerName + (ServerName == tcpServer.Attribute.ServerName ? null : ("_" + tcpServer.Attribute.ServerName)) + ".cache";
     FileWatcher   = new AutoCSer.IO.CreateFlieTimeoutWatcher(ProcessCopyer.Config.CheckTimeoutSeconds, this, AutoCSer.IO.CreateFlieTimeoutType.HttpServerRegister, tcpServer.Log);
     if (!AutoCSer.Config.Pub.Default.IsService && ProcessCopyer.Config.WatcherPath != null)
     {
         try
         {
             FileWatcher.Add(ProcessCopyer.Config.WatcherPath);
         }
         catch (Exception error)
         {
             tcpServer.Log.Add(AutoCSer.Log.LogType.Error, error, ProcessCopyer.Config.WatcherPath);
         }
     }
     try
     {
         if (System.IO.File.Exists(cacheFileName))
         {
             Cache[] cache = AutoCSer.BinarySerialize.DeSerializer.DeSerialize <Cache[]>(System.IO.File.ReadAllBytes(cacheFileName));
             isLoadCache = true;
             if (OnLoadCacheDomain != null)
             {
                 OnLoadCacheDomain();
             }
             for (int index = cache.Length; index != 0;)
             {
                 try
                 {
                     start(ref cache[--index]);
                 }
                 catch (Exception error)
                 {
                     tcpServer.Log.Add(AutoCSer.Log.LogType.Error, error);
                 }
             }
         }
     }
     catch (Exception error)
     {
         tcpServer.Log.Add(AutoCSer.Log.LogType.Error, error);
     }
     if (isLoadCache)
     {
         isLoadCache = false;
     }
     else if (OnLoadCacheDomain != null)
     {
         OnLoadCacheDomain();
     }
 }
コード例 #2
0
        /// <summary>
        /// 启动域名服务
        /// </summary>
        /// <param name="register">域名注册信息</param>
        /// <returns>域名服务启动状态</returns>
        private RegisterState start(ref Cache register)
        {
            if (isDisposed != 0)
            {
                return(RegisterState.Disposed);
            }
            if (register.Domains.length() == 0)
            {
                return(RegisterState.DomainError);
            }
            FileInfo assemblyFile = new FileInfo(register.AssemblyFile);

            if (!System.IO.File.Exists(register.AssemblyFile))
            {
                server.Log.Add(AutoCSer.Log.LogType.Error, "未找到程序集 " + register.AssemblyFile);
                return(RegisterState.NotFoundAssembly);
            }
            RegisterState state        = RegisterState.Unknown;
            DomainServer  domainServer = new DomainServer {
                Register = register, RegisterServer = this
            };
            int domainCount = domainServer.CheckDomain(ref state);

            if (state == RegisterState.Success)
            {
                try
                {
                    state = RegisterState.StartError;
                    Assembly                 assembly    = null;
                    DirectoryInfo            directory   = assemblyFile.Directory;
                    KeyValue <Domain, int>[] domainFlags = register.Domains.getArray(value => new KeyValue <Domain, int>(value, 0));
                    HashString               pathKey     = register.AssemblyFile;
                    Monitor.Enter(assemblyLock);
                    try
                    {
                        if (!register.IsShareAssembly || !assemblyCache.TryGetValue(pathKey, out assembly))
                        {
                            string serverPath = Config.WorkPath + ((ulong)AutoCSer.Pub.StartTime.Ticks).toHex() + ((ulong)AutoCSer.Pub.Identity).toHex() + AutoCSer.Extension.DirectoryExtension.Separator;
                            Directory.CreateDirectory(serverPath);
                            foreach (FileInfo file in directory.GetFiles())
                            {
                                file.CopyTo(serverPath + file.Name);
                            }
                            assembly = Assembly.LoadFrom(serverPath + assemblyFile.Name);
                            if (register.IsShareAssembly)
                            {
                                assemblyCache.Add(pathKey, assembly);
                            }
                        }
                    }
                    finally { Monitor.Exit(assemblyLock); }
                    domainServer.HttpDomainServer = (HttpDomainServer.Server)Activator.CreateInstance(assembly.GetType(register.ServerTypeName));
                    domainServer.HttpDomainServer.LoadCheckPath = getLoadCheckPath(directory).FullName;
                    if (domainServer.HttpDomainServer.Start(this, register.Domains, domainServer.RemoveFileWatcher))
                    {
                        if (FileWatcher != null)
                        {
                            FileWatcher.Add(directory.FullName);
                        }
                        domainServer.FileWatcherPath = directory.FullName;
                        if ((state = start(register.Domains)) == RegisterState.Success)
                        {
                            domainServer.DomainCount = register.Domains.Length;
                            domainServer.Domains     = domainFlags;
                            domainServer.IsStart     = true;
                            if (!isLoadCache)
                            {
                                Monitor.Enter(domainLock);
                                try
                                {
                                    Cache[] registers = domains.GetSaveCache();
                                    if (registers != null)
                                    {
                                        System.IO.File.WriteAllBytes(cacheFileName, AutoCSer.BinarySerialize.Serializer.Serialize(registers));
                                    }
                                }
                                finally { Monitor.Exit(domainLock); }
                            }
                            server.Log.Add(AutoCSer.Log.LogType.Info, @"domain success
" + register.Domains.joinString(@"
", domain => domain.DomainData.toStringNotNull() + (domain.Host.Host == null ? null : (" [" + domain.Host.Host + ":" + domain.Host.Port.toString() + "]")) + (domain.SslHost.Host == null ? null : (" [" + domain.SslHost.Host + ":" + domain.SslHost.Port.toString() + "]"))), new System.Diagnostics.StackFrame(), false);
                            return(RegisterState.Success);
                        }
                    }
                }
                catch (Exception error)
                {
                    server.Log.Add(AutoCSer.Log.LogType.Error, error);
                }
            }
            foreach (Domain domain in register.Domains)
            {
                if (domainCount-- == 0)
                {
                    break;
                }
                Monitor.Enter(domainLock);
                domains.Remove(domain.DomainData);
                Monitor.Exit(domainLock);
            }
            domainServer.Dispose();
            return(state);
        }