/// <summary> /// 关闭并清理 由 <see cref="CreateInstance"/> 创建的串口实例 /// <para>注意:静态函数,引用参数 serialPort, 该方法会将 实例变量 设为 null </para> /// </summary> /// <param name="serialPort"></param> /// <exception cref="ArgumentNullException"></exception> public static void CloseAndDispose(ref SerialPort serialPort) { SpaceCGUtils.Log.InfoFormat("Close And Dispose Serial Port."); if (serialPort == null) { throw new ArgumentNullException("参数不能为空"); } SpaceCGUtils.RemoveAnonymousEvents(serialPort, "PinChanged"); SpaceCGUtils.RemoveAnonymousEvents(serialPort, "DataReceived"); SpaceCGUtils.RemoveAnonymousEvents(serialPort, "ErrorReceived"); try { if (serialPort.IsOpen) { serialPort.DiscardInBuffer(); serialPort.DiscardOutBuffer(); serialPort.Close(); } } catch (Exception ex) { SpaceCGUtils.Log.ErrorFormat("关闭并清理串口时产生异常:{0}", ex); } serialPort.Dispose(); serialPort = null; }
/// <summary> /// 销毁由 <see cref="CreateServer"/> 创建的服务端对象 /// <para>注意:静态函数,非引用参数 server, 需实例变量 设为 null </para> /// </summary> /// <param name="server"></param> /// <exception cref="ArgumentNullException"></exception> public static void DisposeServer(this HPSocket.IServer server) { if (server == null) { throw new ArgumentNullException("参数 server 不能为空"); } ushort port = server.Port; string address = server.Address; Type type = server.GetType(); SpaceCGUtils.RemoveAnonymousEvents(server, "OnSend"); SpaceCGUtils.RemoveAnonymousEvents(server, "OnClose"); SpaceCGUtils.RemoveAnonymousEvents(server, "OnAccept"); SpaceCGUtils.RemoveAnonymousEvents(server, "OnReceive"); SpaceCGUtils.RemoveAnonymousEvents(server, "OnShutdown"); SpaceCGUtils.RemoveAnonymousEvents(server, "OnHandShake"); SpaceCGUtils.RemoveAnonymousEvents(server, "OnPrepareListen"); List <IntPtr> clients = server.GetAllConnectionIds(); foreach (IntPtr client in clients) { if (server.IsConnected(client)) { server.Disconnect(client, true); } } server.Dispose(); server = null; SpaceCGUtils.Log.InfoFormat("服务端({0}) {1}:{2} 已停止服务并销毁释放", type, address, port); }
/// <summary> /// 销毁由 <see cref="CreateClient"/> 创建的客户端对象 /// <para>注意:静态函数,非引用参数 client, 需实例变量 设为 null </para> /// </summary> /// <param name="client"></param> /// <exception cref="ArgumentNullException"></exception> public static void DisposeClient(this HPSocket.IClient client) { Console.WriteLine("connectid:{0}", client.ConnectionId); if (client == null) { throw new ArgumentNullException("参数 client 不能为空"); } //if(client.ExtraData != null && client.ExtraData is bool) client.ExtraData = false; ushort localPort = 0; string localAddr = "0.0.0.0"; Type type = client.GetType(); if (client.IsConnected) { client.GetListenAddress(out localAddr, out localPort); } SpaceCGUtils.RemoveAnonymousEvents(client, "OnClose"); SpaceCGUtils.RemoveAnonymousEvents(client, "OnConnect"); SpaceCGUtils.RemoveAnonymousEvents(client, "OnReceive"); SpaceCGUtils.RemoveAnonymousEvents(client, "OnSend"); SpaceCGUtils.RemoveAnonymousEvents(client, "OnHandShake"); SpaceCGUtils.RemoveAnonymousEvents(client, "OnPrepareConnect"); client.Dispose(); client = null; SpaceCGUtils.Log.InfoFormat("客户端({0}) {1}:{2} 断开连接并销毁释放", type, localAddr, localPort); }
/// <summary> /// 移除并销毁 由 <see cref="ListenInstanceChange(string, TimeSpan, Action{ManagementBaseObject}, log4net.ILog)"/> 创建的监听。 /// <para>移除监听 "__InstanceModificationEvent" 事件</para> /// </summary> public static void RemoveInstanceModification() { if (InstanceModificationEvent == null) { return; } SpaceCGUtils.RemoveAnonymousEvents(InstanceModificationEvent, "EventArrived"); InstanceModificationEvent?.Stop(); InstanceModificationEvent?.Dispose(); InstanceModificationEvent = null; }
/// <summary> /// 移除并销毁 由 <see cref="ListenInstanceChange(string, TimeSpan, Action{ManagementBaseObject}, log4net.ILog)"/> 创建的监听。 /// <para>移除监听 "__InstanceCreationEvent" AND "__InstanceDeletionEvent" 事件</para> /// </summary> public static void RemoveInstanceChange() { if (InstanceCreationEvent != null) { SpaceCGUtils.RemoveAnonymousEvents(InstanceCreationEvent, "EventArrived"); InstanceCreationEvent?.Stop(); InstanceCreationEvent?.Dispose(); } if (InstanceDeletionEvent != null) { SpaceCGUtils.RemoveAnonymousEvents(InstanceDeletionEvent, "EventArrived"); InstanceDeletionEvent?.Stop(); InstanceDeletionEvent?.Dispose(); } InstanceCreationEvent = null; InstanceDeletionEvent = null; }