MessageClient

Posted 金融之王

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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}\r\n{1}", message.GetType().Name, stopwatch.ElapsedMilliseconds);
                        }
                    }
                    catch (Exception exception)
                    {
                        //Logger.TraceEvent(TraceEventType.Error, "MessageClient Dispatcher Message type:{0}\r\n{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)
        {
        }
    }
}

 

以上是关于MessageClient的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板

VSCode自定义代码片段6——CSS选择器

VSCode自定义代码片段——声明函数