码迷,mamicode.com
首页 > 其他好文 > 详细

MessageClient

时间:2016-11-07 02:21:37      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:generic   win   .com   pen   lis   gen   wcf   try   engine   

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

标签:generic   win   .com   pen   lis   gen   wcf   try   engine   

原文地址:http://www.cnblogs.com/feige/p/6036694.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!