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)
        {
        }
    }
}

 

posted @ 2016-11-06 22:25  金融之王  阅读(560)  评论(0编辑  收藏  举报