/// <summary> /// 加载程序集 /// </summary> /// <param name="assemblyFileName">程序集文件名</param> public Assembly Load(string assemblyFileName) { if (!string.IsNullOrEmpty(assemblyFileName)) { Assembly assembly; HashString nameKey = assemblyFileName; if (assemblys.TryGetValue(nameKey, out assembly)) { return(assembly); } try { if (assemblyFileName.Length > 4 && assemblyFileName.EndsWith(".dll")) { assembly = Assembly.LoadFrom(assemblyFileName.Substring(0, assemblyFileName.Length - 4)); } else { assembly = Assembly.LoadFrom(assemblyFileName); } assemblys[nameKey] = assembly; return(assembly); } catch (Exception error) { Log.add(AutoCSer.Log.LogType.Error, error, "动态应用程序域加载程序集 " + assemblyFileName + " 失败"); } } return(null); }
/// <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.Log.ILog log = null) { this.fileName = fileName; this.encoding = encoding ?? AutoCSer.Config.Pub.Default.Encoding; this.log = log ?? AutoCSer.Log.Pub.Log; bool isFile = false, isJson = false; try { if (File.Exists(fileName)) { isFile = true; if (AutoCSer.Json.Parser.Parse(File.ReadAllText(fileName, this.encoding), ref value)) { Value = value; isJson = true; } } } catch (Exception error) { log.add(Log.LogType.Error, error, fileName); } if (isFile && !isJson) { AutoCSer.IO.File.MoveBak(fileName); } }
/// <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.Log.ILog log = null) { if (check(content)) { if (log == null) { log = AutoCSer.Log.Pub.Log; } MailMessage message = null; try { message = new MailMessage(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.add(Log.LogType.Debug | Log.LogType.Info, error, "邮件发送失败 : " + content.SendTo); } if (message != null) { message.Dispose(); } } return(false); }
/// <summary> /// TCP 服务器端同步调用任务处理 /// </summary> protected override void run() { do { waitHandle.Wait(); while (System.Threading.Interlocked.CompareExchange(ref queueLock, 1, 0) != 0) { AutoCSer.Threading.ThreadYield.YieldOnly(); } Buffer value = head; end = null; head = null; queueLock = 0; do { try { while (value != null) { onPacket(value); value = value.LinkNext; } break; } catch (Exception error) { log.add(Log.LogType.Error, error); } value = value.LinkNext; }while (true); }while (!isDisposed); }
/// <summary> /// 多线程并行建图 /// </summary> /// <param name="threadCount">并行线程数量</param> /// <param name="log">日志处理</param> private void buildGraph(int threadCount, AutoCSer.Log.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.add(Log.LogType.Error, builder.ThreadException); isError = true; } } }while (reader.Length != 0 && !isError); } finally { foreach (ThreadBuilder builder in builders) { if (builder != null && builder.ThreadException == null) { builder.FreeThread(); } } } }
/// <summary> /// 卸载应用程序域 /// </summary> public void Dispose() { if (domain != null) { try { AppDomain.Unload(domain); } catch (Exception error) { log.add(Log.LogType.Error, error); } domain = null; loader = null; } }
/// <summary> /// 写文件 /// </summary> /// <param name="json">JSON字符串</param> /// <returns>是否成功</returns> private bool write(string json) { try { if (File.Exists(fileName)) { AutoCSer.IO.File.MoveBak(fileName); } File.WriteAllText(fileName, json, this.encoding); return(true); } catch (Exception error) { log.add(Log.LogType.Error, error, fileName); } return(false); }
/// <summary> /// 取消调用 /// </summary> /// <param name="log"></param> internal void CancelQueue(AutoCSer.Log.ILog log) { Uri value = this; do { Uri nextValue = value.LinkNext; try { value.Cancel(); } catch (Exception error) { log.add(Log.LogType.Error, error); } value = nextValue; }while (value != null); }
/// <summary> /// 发送邮件 /// </summary> /// <param name="content">电子邮件内容</param> /// <param name="log">日志处理</param> /// <returns>邮件是否发送成功</returns> public bool Send(Content content, AutoCSer.Log.ILog log = null) { bool isSend = false; if (check(content)) { using (MailMessage message = new MailMessage(From, content.SendTo, content.Subject, content.Body)) { try { getSmtp(message, content).Send(message); isSend = true; } catch (Exception error) { log.add(Log.LogType.Debug | Log.LogType.Info, error, "邮件发送失败 : " + content.SendTo); } } } return(isSend); }
/// <summary> /// 邮件发送回调 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void send(object sender, AsyncCompletedEventArgs e) { Action <Exception> callback = this.callback; AutoCSer.Log.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.add(Log.LogType.Debug | Log.LogType.Info, error); } } }
/// <summary> /// 获取HTML标题 /// </summary> /// <param name="uri">Uri与回调函数信息</param> /// <param name="isOnGet"></param> private void get(Uri uri, ref bool isOnGet) { HttpClient client = null; while (System.Threading.Interlocked.CompareExchange(ref clientLock, 1, 0) != 0) { AutoCSer.Threading.ThreadYield.Yield(AutoCSer.Threading.ThreadYield.Type.HtmlTitleHttpClient); } if (isDisposed == 0) { if (clientIndex == 0) { if (clientCount == clients.Length) { uris.Push(uri); clientLock = 0; isOnGet = true; return; } ++clientCount; clientLock = 0; try { client = new HttpClient(this); } catch (Exception error) { Log.add(AutoCSer.Log.LogType.Error, error); } if (client == null) { while (System.Threading.Interlocked.CompareExchange(ref clientLock, 1, 0) != 0) { AutoCSer.Threading.ThreadYield.Yield(AutoCSer.Threading.ThreadYield.Type.HtmlTitleHttpClient); } if (isDisposed == 0) { uris.Push(uri); --clientCount; clientLock = 0; isOnGet = true; return; } clientLock = 0; isOnGet = true; uri.Cancel(); return; } } else { client = clients[--clientIndex]; clientLock = 0; } isOnGet = client.Get(uri); } else { clientLock = 0; isOnGet = true; uri.Cancel(); } }