zoukankan      html  css  js  c++  java
  • MessageClient

    using Manager.Common;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    
    namespace ExchangeManager.WCF
    {
        public class MessageClient
        {
            private RelayEngine<MessageCollection> _MessageRelayEngine;
            private MessageReceiver _MessageReceiver;
            private Stopwatch stopwatch = new Stopwatch();
    
            public MessageClient()
            {
                this._MessageRelayEngine = new RelayEngine<MessageCollection>(this.ProcessMessage, this.HandleException);
                this._MessageRelayEngine.Suspend();
                this._MessageReceiver = new MessageReceiver(this._MessageRelayEngine);
            }
            public void StartMessageProcess(string hostName, int messageDispatchListenPort, string sessionId)
            {
                this._MessageReceiver.Start(hostName, messageDispatchListenPort, sessionId);
                this._MessageRelayEngine.Resume();
            }
            public void SendMessage(byte[] data)
            {
                try
                {
                    this._MessageRelayEngine.AddItem(CompressHelper.FromByteArray<MessageCollection>(data));
                    ConsoleClient.Instance.RefreshLastMsgTime();
                }
                catch (Exception ex) { }
            }
    
            public void Stop()
            {
                this._MessageReceiver.Stop();
                this._MessageRelayEngine.Suspend();
            }
    
            //接受服务端消息
            private bool ProcessMessage(MessageCollection message)
            {
                try
                {
                    App.MainFrameWindow.Dispatcher.BeginInvoke((Action)delegate()
                    {
                        try
                        {
                            stopwatch.Restart();
                            this.Process(message);
                            stopwatch.Stop();
                            //MessageStatistics.Instance.AddItem(message.GetType().Name, stopwatch.ElapsedMilliseconds);
                            if (stopwatch.ElapsedMilliseconds > 50)
                            {
                                //Logger.TraceEvent(TraceEventType.Error, "MessageClient Dispatcher Message type:{0}
    {1}", message.GetType().Name, stopwatch.ElapsedMilliseconds);
                            }
                        }
                        catch (Exception exception)
                        {
                            //Logger.TraceEvent(TraceEventType.Error, "MessageClient Dispatcher Message type:{0}
    {1}", message.GetType().Name, exception);
                        }
                    });
                }
                catch (Exception exception) { }
                return true;
            }
    
            private void Process(MessageCollection messages)
            {
                foreach (Message item in messages.Messages)
                {
                    this.Process((dynamic)item);
                }
            }
    
            private void Process(PriceMessage message)
            {
                try
                {
                    //处理消息
                    App.MainFrameWindow.PriceLable.Content = message.Price;
                }
                catch (Exception ex)
                {
                    this.HandleException(ex);
                }
            }
    
            private void HandleException(Exception exception)
            {
            }
        }
    }
    
  • 相关阅读:
    电子设计基础——有效值检波技术
    电子设计基础——电源的各项指标及测量方法
    电子设计基础——电源的各项指标及测量方法
    计算机网络——IP地址与MAC地址
    计算机网络——IP地址与MAC地址
    LDC1000学习资料
    移动端制作公共样式reset
    单行、多行溢出省略
    垂直居中常用布局
    移动端rem布局必要的meta和script
  • 原文地址:https://www.cnblogs.com/feige/p/6036694.html
Copyright © 2011-2022 走看看