/// <summary> /// 初始化动态应用程序域 /// </summary> /// <param name="name">应用程序域名称</param> /// <param name="privatePath">程序集加载目录</param> /// <param name="configFile">配置文件</param> /// <param name="cacheDirectory">应用程序域缓存目录,null表示非缓存</param> /// <param name="log">日志处理</param> public DynamicDomain(string name, string privatePath, string configFile, string cacheDirectory, AutoCSer.ILog log = null) { this.log = log ?? AutoCSer.LogHelper.Default; if (string.IsNullOrEmpty(privatePath)) { privatePath = AutoCSer.Config.ApplicationPath; } else { privatePath = new DirectoryInfo(privatePath).fullName(); if (privatePath != AutoCSer.Config.ApplicationPath) { this.privatePath = privatePath; } } setup = new AppDomainSetup(); if (configFile != null && File.Exists(configFile)) { setup.ConfigurationFile = configFile; } setup.ApplicationName = name; setup.ApplicationBase = privatePath; setup.PrivateBinPath = privatePath; if (cacheDirectory != null && cacheDirectory.Length != 0) { setup.ShadowCopyFiles = "true"; setup.ShadowCopyDirectories = cacheDirectory; setup.CachePath = cacheDirectory; } domain = AppDomain.CreateDomain(name, null, setup); loader = (assemblyLoader)domain.CreateInstanceAndUnwrap(assemblyName, assemblyLoaderName); loader.Log = this.log; }
/// <summary> /// 队列管理器 /// </summary> /// <param name="queueCount">队列数量,默认为 0 标识 CPU 核心数量</param> /// <param name="isBackground">是否后台线程</param> /// <param name="log">日志接口</param> public QueueManager(int queueCount = 0, bool isBackground = true, AutoCSer.ILog log = null) : this(queueCount, log) { for (int index = 0; index != Queues.Length; ++index) { Queues[index] = createQueue(isBackground); } }
/// <summary> /// 发送邮件 /// </summary> /// <param name="content">电子邮件内容</param> /// <param name="onSend">邮件发送回调</param> /// <param name="log">日志处理</param> /// <returns>是否异步完成</returns> public bool Send(Content content, Action <Exception> onSend, AutoCSer.ILog log = null) { if (check(content)) { if (log == null) { log = AutoCSer.LogHelper.Default; } MailMessage message = null; try { message = new MailMessage(content.From ?? From, content.SendTo, content.Subject, content.Body); message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;//如果发送失败,SMTP 服务器将发送 失败邮件告诉我 (AutoCSer.Threading.RingPool <EventSender> .Default.Pop() ?? new EventSender()).Send(message, getSmtp(message, content), onSend, log); return(true); } catch (Exception error) { log.Exception(error, "邮件发送失败 : " + content.SendTo, LogLevel.Exception | LogLevel.AutoCSer); } if (message != null) { message.Dispose(); } } return(false); }
/// <summary> /// 原始套接字监听 /// </summary> /// <param name="ipAddress">监听地址</param> /// <param name="onPacket">数据包处理委托</param> /// <param name="packetSize">数据包字节数</param> /// <param name="log">日志处理</param> public Listener(IPAddress ipAddress, Action <Buffer> onPacket, int packetSize = defaultBufferSize, AutoCSer.ILog log = null) { if (onPacket == null) { throw new ArgumentNullException(); } if (packetSize <= 0) { packetSize = defaultBufferSize; } else if (packetSize > BufferPool.Size) { packetSize = BufferPool.Size; } this.packetSize = packetSize; maxBufferIndex = BufferPool.Size - packetSize; this.log = log ?? AutoCSer.LogHelper.Default; ipEndPoint = new IPEndPoint(ipAddress, 0); onReceiveAsyncCallback = onReceive; #if !DOTNET2 async = AutoCSer.Net.SocketAsyncEventArgsPool.Get(); async.UserToken = this; async.Completed += onReceiveAsyncCallback; #endif queueTask = new QueueTask(onPacket, this.log); AutoCSer.Threading.ThreadPool.TinyBackground.FastStart(start); }
/// <summary> /// 获取电子邮件发送器 /// </summary> /// <param name="message">邮件信息</param> /// <param name="smtpClient">STMP客户端</param> /// <param name="onSend">邮件发送回调</param> /// <param name="log">日志处理</param> /// <returns>电子邮件发送器</returns> internal void Send(MailMessage message, SmtpClient smtpClient, Action <Exception> onSend, AutoCSer.ILog log) { this.message = message; this.log = log; callback = onSend; smtpClient.SendCompleted += this.onSend; smtpClient.SendAsync(message, this); }
/// <summary> /// 文件备份 /// </summary> /// <param name="fileName">文件名称</param> /// <param name="trySeconds">失败重试间隔秒数</param> /// <param name="masterClient">缓存主服务客户端</param> /// <param name="log">日志处理</param> public FileBackup(string fileName, int trySeconds = 4, MasterServer.TcpInternalClient masterClient = null, AutoCSer.ILog log = null) { this.fileName = fileName; tryTicks = Math.Max(trySeconds, 1) * TimeSpan.TicksPerSecond; this.masterClient = masterClient ?? new MasterServer.TcpInternalClient(); this.log = log ?? AutoCSer.LogHelper.Default; onReadHandle = onRead; AutoCSer.Threading.ThreadPool.TinyBackground.Start(getVersion); }
/// <summary> /// 多线程并行建图 /// </summary> /// <param name="threadCount">并行线程数量</param> /// <param name="log">日志处理</param> private void buildGraph(int threadCount, AutoCSer.ILog log) { LeftArray <Node> reader = new LeftArray <Node>(Boot.Nodes.Values.getArray()); int taskCount = threadCount - 1; bool isError = false; AutoCSer.Threading.AutoWaitCount waitCount = new AutoCSer.Threading.AutoWaitCount(taskCount); ThreadBuilder[] builders = new ThreadBuilder[threadCount]; try { for (int builderIndex = 0; builderIndex != builders.Length; builders[builderIndex++] = new ThreadBuilder(Boot, waitCount)) { ; } do { Node[] readerArray = reader.Array; int count = reader.Length / threadCount, index = 0; for (int builderIndex = 0; builderIndex != taskCount; ++builderIndex) { builders[builderIndex].SetThread(readerArray, index, count); index += count; } builders[taskCount].Set(readerArray, index, reader.Length); builders[taskCount].Build(); waitCount.WaitSet(taskCount); reader.Length = 0; foreach (ThreadBuilder builder in builders) { if (builder.ThreadException == null) { reader.Add(ref builder.Writer); } else { log.Exception(builder.ThreadException, null, LogLevel.Exception | LogLevel.AutoCSer); isError = true; } } }while (reader.Length != 0 && !isError); } finally { foreach (ThreadBuilder builder in builders) { if (builder != null && builder.ThreadException == null) { builder.FreeThread(); } } } }
/// <summary> /// 建图 /// </summary> /// <param name="threadCount">并行线程数量</param> /// <param name="log">日志处理</param> internal void BuildGraph(int threadCount, AutoCSer.ILog log) { if (Boot.Nodes != null) { if (threadCount > AutoCSer.Common.ProcessorCount) { threadCount = AutoCSer.Common.ProcessorCount; } if (threadCount > 1) { buildGraph(threadCount, log ?? AutoCSer.LogHelper.Default); } else { buildGraph(); } } }
/// <summary> /// HTML标题获取客户端任务池 /// </summary> /// <param name="maxClientCount">最大实例数量</param> /// <param name="timeoutSeconds">套接字操作超时时间</param> /// <param name="bufferSize">收发数据缓冲区字节数</param> /// <param name="maxSearchSize">最大搜索字节数</param> /// <param name="isValidateCertificate">是否验证安全证书</param> /// <param name="log">日志处理</param> public HttpTask(int maxClientCount = 1, int timeoutSeconds = 15, AutoCSer.Memory.BufferSize bufferSize = AutoCSer.Memory.BufferSize.Kilobyte4, int maxSearchSize = 0, bool isValidateCertificate = false, AutoCSer.ILog log = null) { if (bufferSize < MinBufferSize) { bufferSize = MinBufferSize; } else if (bufferSize > AutoCSer.Memory.BufferSize.Kilobyte32) { bufferSize = AutoCSer.Memory.BufferSize.Kilobyte32; } BufferSize = (int)bufferSize; MaxSearchSize = Math.Min(BufferSize - sizeof(int), maxSearchSize); IsValidateCertificate = isValidateCertificate; this.Log = log ?? AutoCSer.LogHelper.Default; BufferPool = AutoCSer.SubBuffer.Pool.GetPool(bufferSize); uris = new Uri.Queue(new Uri()); socketTimeout = new SocketTimeoutLink(Math.Max(timeoutSeconds, 15)); clients = new HttpClient[maxClientCount <= 0 ? 1 : maxClientCount]; }
/// <summary> /// 取消调用 /// </summary> /// <param name="log"></param> internal void CancelQueue(AutoCSer.ILog log) { Uri value = this; do { try { do { value.Cancel(ref value); }while (value != null); break; } catch (Exception error) { log.Exception(error, null, LogLevel.Exception | LogLevel.AutoCSer); } }while (value != null); }
/// <summary> /// 发送邮件 /// </summary> /// <param name="content">电子邮件内容</param> /// <param name="log">日志处理</param> /// <returns>邮件是否发送成功</returns> public bool Send(Content content, AutoCSer.ILog log = null) { bool isSend = false; if (check(content)) { using (MailMessage message = new MailMessage(content.From ?? From, content.SendTo, content.Subject, content.Body)) { try { getSmtp(message, content).Send(message); isSend = true; } catch (Exception error) { log.Exception(error, "邮件发送失败 : " + content.SendTo, LogLevel.Exception | LogLevel.AutoCSer); } } } return(isSend); }
/// <summary> /// 消息队列 客户端消费者 /// </summary> /// <param name="client">TCP 客户端</param> /// <param name="config">队列数据 读取配置</param> /// <param name="log">日志处理</param> /// <param name="readerIndexNode"></param> protected Consumer(MasterServer.TcpInternalClient client, ConsumerConfig config, AutoCSer.ILog log, DataStructure.Abstract.Node readerIndexNode) { if (client == null) { throw new InvalidOperationException(); } this.client = client; if (config == null) { Config = defaultConfig; } else { config.Format(); Config = config; } Log = log ?? client._TcpClient_.Log; getDequeueIdentityNode = new DataStructure.Parameter.Value(readerIndexNode, OperationParameter.OperationType.MessageQueueGetDequeueIdentity); getDequeueIdentityNode.Parameter.SetJson((Cache.MessageQueue.ReaderConfig)Config); getMessageNode = new DataStructure.Parameter.Value(readerIndexNode, OperationParameter.OperationType.MessageQueueDequeue); setDequeueIdentityNode = new DataStructure.Parameter.Value(readerIndexNode, OperationParameter.OperationType.MessageQueueSetDequeueIdentity); }
/// <summary> /// 消息分发 客户端消费者 /// </summary> /// <param name="node">消费分发节点</param> /// <param name="config">消息分发 读取配置</param> /// <param name="log">日志处理</param> protected DistributionConsumer(DataStructure.Abstract.Node node, DistributionConsumerConfig config, AutoCSer.ILog log) { client = node.ClientDataStructure.Client.MasterClient; if (client == null) { throw new InvalidOperationException(); } this.node = node; if (config == null) { Config = defaultConfig; } else { config.Format(); Config = config; } Log = log ?? client._TcpClient_.Log; getSendCountNode = new DataStructure.Parameter.Value(node, OperationParameter.OperationType.MessageQueueGetDequeueIdentity); getSendCountNode.Parameter.SetJson((Cache.MessageQueue.DistributionConfig)Config); getMessageNode = new DataStructure.Parameter.Value(node, OperationParameter.OperationType.MessageQueueDequeue, config.LoopSendCount); }
/// <summary> /// 邮件发送回调 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void send(object sender, AsyncCompletedEventArgs e) { Action <Exception> callback = this.callback; AutoCSer.ILog log = this.log; message.Dispose(); this.callback = null; this.log = null; message = null; AutoCSer.Threading.RingPool <EventSender> .Default.PushNotNull(this); if (callback != null) { try { callback(e.Error); } catch (Exception error) { log.Exception(error, null, LogLevel.Exception | LogLevel.AutoCSer); } } }
/// <summary> /// 字符串 Trie 图 /// </summary> /// <param name="words">分词集合</param> /// <param name="threadCount">并行线程数量</param> /// <param name="log">日志处理</param> public StringTrieGraph(ref LeftArray <string> words, int threadCount = 0, AutoCSer.ILog log = null) { CharTypeData = new AutoCSer.Memory.Pointer { Data = DefaultCharTypeData.Data }; if (words.Length != 0) { buildTree(ref words); BuildGraph(threadCount, log); } if (Boot.Nodes == null) { Boot.Nodes = DictionaryCreator.CreateChar <Node>(); } else { foreach (char key in Boot.Nodes.Keys) { AnyHeadChar = key; break; } } }
/// <summary> /// JSON对象缓存文件 /// </summary> /// <param name="fileName">缓存文件名称</param> /// <param name="value">数据对象</param> /// <param name="encoding"></param> /// <param name="log">日志处理</param> public JsonFile(string fileName, valueType value = null, Encoding encoding = null, AutoCSer.ILog log = null) { this.fileName = fileName; this.encoding = encoding ?? AutoCSer.Common.Config.Encoding; this.log = log ?? AutoCSer.LogHelper.Default; bool isFile = false, isJson = false; try { FileInfo file = new FileInfo(fileName); if (file.Exists) { isFile = true; if (AutoCSer.JsonDeSerializer.DeSerialize(File.ReadAllText(fileName, this.encoding), ref value)) { Value = value; isJson = true; } } else { DirectoryInfo directory = file.Directory; if (!directory.Exists) { directory.Create(); } } } catch (Exception error) { log.Exception(error, fileName, LogLevel.Exception | LogLevel.AutoCSer); } if (isFile && !isJson) { AutoCSer.IO.File.MoveBak(fileName); } }
public static AutoCSer.CacheServer.MessageQueue.ConsumerStream <Json <valueType>, valueType> CreateConsumerStream <valueType>(this AutoCSer.CacheServer.DataStructure.MessageQueue.QueueConsumers <Json <valueType> > messageQueue, IConvertible readerIndex, Action <valueType> onMessage, AutoCSer.CacheServer.MessageQueue.ConsumerConfig config, AutoCSer.ILog log = null) { return(new AutoCSer.CacheServer.MessageQueue.ConsumerStream <Json <valueType>, valueType>(messageQueue, onMessage, config, readerIndex, Json <valueType> .GetData, log)); }
/// <summary> /// 消息分发 客户端消费者 /// </summary> /// <param name="node">消费分发节点</param> /// <param name="onMessage">消息处理委托</param> /// <param name="config">消息分发 读取配置</param> /// <param name="log">日志处理</param> public DistributionConsumer(DataStructure.MessageQueue.Distributor<valueType> node, Action<valueType> onMessage, DistributionConsumerConfig config, AutoCSer.ILog log = null) : base(node, config, log) { if (onMessage == null) throw new ArgumentNullException(); this.onMessage = onMessage; setCheckSocketVersion(); }
public static AutoCSer.CacheServer.MessageQueue.ConsumerAsynchronous <valueType> CreateConsumer <valueType>(this AutoCSer.CacheServer.DataStructure.MessageQueue.QueueConsumers <valueType> messageQueue, IConvertible readerIndex, Action <valueType, Action> onMessage, AutoCSer.CacheServer.MessageQueue.ConsumerConfig config, AutoCSer.ILog log = null) { return(new AutoCSer.CacheServer.MessageQueue.ConsumerAsynchronous <valueType>(messageQueue, onMessage, config, readerIndex, log)); }
public static AutoCSer.CacheServer.MessageQueue.DistributionConsumerStream <Binary <valueType>, valueType> CreateConsumerStream <valueType>(this AutoCSer.CacheServer.DataStructure.MessageQueue.Distributor <Binary <valueType> > distributor, Action <valueType> onMessage, AutoCSer.CacheServer.MessageQueue.DistributionConsumerConfig config, AutoCSer.ILog log = null) { return(new AutoCSer.CacheServer.MessageQueue.DistributionConsumerStream <Binary <valueType>, valueType>(distributor, onMessage, config, Binary <valueType> .GetData, log)); }
/// <summary> /// 多消费者消息队列 客户端消费者 /// </summary> /// <param name="messageQueue">队列消费节点</param> /// <param name="onMessage">消息处理委托</param> /// <param name="config">队列数据 读取配置</param> /// <param name="readerIndex">读取编号</param> /// <param name="log">日志处理</param> public ConsumerAsynchronous(DataStructure.MessageQueue.QueueConsumers <valueType> messageQueue, Action <valueType, Action> onMessage, ConsumerConfig config, int readerIndex, AutoCSer.ILog log = null) : base(messageQueue, config, log, readerIndex) { if (onMessage == null) { throw new ArgumentNullException(); } OnMessage = onMessage; setCheckSocketVersion(); }
/// <summary> /// 队列线程 /// </summary> /// <param name="onPacket">数据包处理委托</param> /// <param name="log">日志处理</param> internal QueueTask(Action <Buffer> onPacket, AutoCSer.ILog log) { this.onPacket = onPacket; this.log = log; }
public static AutoCSer.CacheServer.MessageQueue.Consumer <Binary <valueType>, valueType> CreateConsumer <valueType>(this AutoCSer.CacheServer.DataStructure.MessageQueue.QueueConsumer <Binary <valueType> > messageQueue, Action <valueType> onMessage, AutoCSer.CacheServer.MessageQueue.ConsumerConfig config, AutoCSer.ILog log = null) { return(new AutoCSer.CacheServer.MessageQueue.Consumer <Binary <valueType>, valueType>(messageQueue, onMessage, config, Binary <valueType> .GetData, log)); }
public static AutoCSer.CacheServer.MessageQueue.DistributionConsumerAsynchronous <valueType> CreateConsumer <valueType>(this AutoCSer.CacheServer.DataStructure.MessageQueue.Distributor <valueType> distributor, Action <valueType, Action> onMessage, AutoCSer.CacheServer.MessageQueue.DistributionConsumerConfig config, AutoCSer.ILog log = null) { return(new AutoCSer.CacheServer.MessageQueue.DistributionConsumerAsynchronous <valueType>(distributor, onMessage, config, log)); }
/// <summary> /// 字符串 Trie 图 /// </summary> /// <param name="words">分词集合</param> /// <param name="threadCount">并行线程数量</param> /// <param name="log">日志处理</param> public StringTrieGraph(string[] words, int threadCount = 0, AutoCSer.ILog log = null) : this(new LeftArray <string>(words), threadCount, log) { }
/// <summary> /// 创建缓存服务静态路由集群节点 /// </summary> /// <param name="index">节点编号</param> /// <param name="attribute">TCP 调用服务器端配置信息</param> /// <param name="log">日志接口</param> public TcpInternalServer CreateStaticRoute(int index, AutoCSer.Net.TcpInternalServer.ServerAttribute attribute = null, AutoCSer.ILog log = null) { return(CreateStaticRoute(index, attribute, this, log)); }
/// <summary> /// 多消费者消息队列 客户端消费者 /// </summary> /// <param name="messageQueue">队列消费节点</param> /// <param name="onMessage">消息处理委托</param> /// <param name="config">队列数据 读取配置</param> /// <param name="readerIndex">读取编号</param> /// <param name="log">日志处理</param> public ConsumerAsynchronous(DataStructure.MessageQueue.QueueConsumers <valueType> messageQueue, Action <valueType, Action> onMessage, ConsumerConfig config, IConvertible readerIndex, AutoCSer.ILog log = null) : this(messageQueue, onMessage, config, readerIndex.ToInt32(null), log) { }
/// <summary> /// 创建缓存服务静态路由集群节点 /// </summary> /// <param name="index">节点编号</param> /// <param name="attribute">TCP 调用服务器端配置信息</param> /// <param name="value">TCP 服务目标对象</param> /// <param name="log">日志接口</param> public static TcpInternalServer CreateStaticRoute(int index, AutoCSer.Net.TcpInternalServer.ServerAttribute attribute = null, SlaveServer value = null, AutoCSer.ILog log = null) { return(new TcpInternalServer(CreateStaticRouteAttribute(index, attribute ?? AutoCSer.Net.TcpInternalServer.ServerAttribute.GetConfig(ServerName, typeof(SlaveServer))), null, value, 0, null, log)); }
/// <summary> /// 字符串 Trie 图 /// </summary> /// <param name="words">分词集合</param> /// <param name="threadCount">并行线程数量</param> /// <param name="log">日志处理</param> public StringTrieGraph(LeftArray <string> words, int threadCount = 0, AutoCSer.ILog log = null) : this(ref words, threadCount, log) { }
/// <summary> /// TCP 服务器端同步调用队列处理 /// </summary> /// <param name="maxDataCount">最大数据数量</param> /// <param name="isBackground">是否后台线程</param> /// <param name="log">日志接口</param> public ServerCallCanDisposableQueue(int maxDataCount, bool isBackground = true, AutoCSer.ILog log = null) : base(isBackground, false, log) { if (maxDataCount <= 0) { throw new IndexOutOfRangeException(); } this.maxDataCount = maxDataCount; threadHandle.Start(); }