protected override void Stop() { if (_stopFlag == 1) { return; } _cancellation.Cancel(); Thread.MemoryBarrier(); ModuleUtils.StopThreadWork(_receiveThread); //如果两个队列在被锁的状态则释放锁 _engineMessageQueue.FreeBlocks(); _statusMessageQueue.FreeBlocks(); _callBackMessageQueue.FreeBlocks(); // 发送停止消息,该消息只是为了释放被Receive阻塞的线程,并不会真的被处理 ControlMessage stopMessage = new ControlMessage(MessageNames.CtrlAbort, CommonConst.BroadcastSession); UpLinkMessenger.Send(stopMessage); Thread.MemoryBarrier(); ModuleUtils.StopThreadWork(_engineMessageListener); ModuleUtils.StopThreadWork(_statusMessageListener); ModuleUtils.StopThreadWork(_callBackMessageListener); ModuleUtils.StopThreadWork(_receiveThread); ZombieCleaner.Stop(); UpLinkMessenger.Clear(); Thread.VolatileWrite(ref _stopFlag, 1); GlobalInfo.LogService.Print(LogLevel.Info, CommonConst.PlatformLogSession, "Message transceiver stopped."); }
private void SynchronousReceive(object state) { try { while (!_cancellation.IsCancellationRequested) { IMessage rawMessage = UpLinkMessenger.Receive(); MessageBase message = rawMessage as MessageBase; if (null != message) { if (message.Type == MessageType.Status) { _statusMessageQueue.Enqueue(message); } else if (message.Type == MessageType.CallBack) { _callBackMessageQueue.Enqueue(message); } else { _engineMessageQueue.Enqueue(message); } GlobalInfo.LogService.Print(LogLevel.Debug, CommonConst.PlatformLogSession, $"Message received, Type:{message.Type}, Index:{message.Index}."); } else { GlobalInfo.LogService.Print(LogLevel.Warn, CommonConst.PlatformSession, $"Unexpected message received. Type:{rawMessage.GetType().Name}, id:{rawMessage.Id}"); } } } catch (ThreadAbortException) { GlobalInfo.LogService.Print(LogLevel.Warn, CommonConst.PlatformLogSession, $"thread {Thread.CurrentThread.Name} is stopped abnormally"); } catch (Exception ex) { // 只有在因为非用户操作的情况下停止才执行停止操作 this.Stop(); GlobalInfo.EventQueue.Enqueue(new ExceptionEventInfo(ex)); GlobalInfo.LogService.Print(LogLevel.Fatal, CommonConst.PlatformLogSession, ex); } finally { GlobalInfo.LogService.Print(LogLevel.Info, CommonConst.PlatformLogSession, $"thread {Thread.CurrentThread.Name} is Stopped."); } }