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)
            {
            }
        }
    }
    
  • 相关阅读:
    nginx配置url伪静态
    PHP爬虫之queryList
    PHP 判断给定两个时间是否在同一周,月,年
    服务器nginx配置显示文件而不是下载
    PHP yield占用内存测试
    PHP 函数运行的内存
    io系列之字节流
    io系列之字符流
    对于Arrays的deep相关的方法。
    常用工具类(System,Runtime,Date,Calendar,Math)
  • 原文地址:https://www.cnblogs.com/feige/p/6036694.html
Copyright © 2011-2022 走看看