| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- using GalaSoft.MvvmLight.Messaging;
- using Muchinfo.MTPClient.Adapter.Factory;
- using Muchinfo.MTPClient.Data.Enums;
- using Muchinfo.MTPClient.Infrastructure.LinkProxy.Enum;
- using Muchinfo.MTPClient.Infrastructure.LinkProxy.TCP;
- using System;
- using Muchinfo.MTPClient.Infrastructure.Helpers;
- using Muchinfo.MTPClient.NetworkCore;
- using DatagramType = Muchinfo.MTPClient.Infrastructure.LinkProxy.Enum.DatagramType;
- namespace Muchinfo.MTPClient.Infrastructure.LinkProxy
- {
- public class LinkManagerParameters
- {
- public string TradeHost { get; set; }
- public int TradePort { get; set; }
- public string QuotationHost { get; set; }
- public int QuotationPort { get; set; }
- public LinkType TradeLinkeType { get; set; }
- public DatagramType TradeDatagramType { get; set; }
- /// <summary>
- /// GO服务地址
- /// </summary>
- public string GoCommonSearchUrl { get; set; }
- public string GuestQuotationHost { get; set; }
- public int GuestQuotationPort { get; set; }
- }
- public class LinkManager : IDisposable
- {
- #region Public Fields
- /// <summary>
- /// 链路参数
- /// </summary>
- public LinkManagerParameters Parameters { get; set; }
- /// <summary>
- /// 业务对象数据转换器
- /// </summary>
- /// <value>The biz adapter factory.</value>
- public AdapterFactory TradeAdapterFactory { get; private set; }
- /// <summary>
- /// Gets or sets the trade TCP link proxy.
- /// </summary>
- /// <value>The trade TCP link proxy.</value>
- public TradeTcpLinkProxy TradeTcpLinkProxy { get; set; }
- /// <summary>
- /// Gets or sets the Quote TCP link proxy.
- /// </summary>
- /// <value>The Quote TCP link proxy.</value>
- public QuoteTcpLinkProxy QuoteTcpLinkProxy { get; set; }
- /// <summary>
- /// 游客看行情地址
- /// </summary>
- public QuoteTcpLinkProxy GuestQuoteTcpLinkProxy { get; set; }
- #endregion
- #region Singalton
- private static LinkManager _instance;
- private static object _lock = new object();
- public static LinkManager Instance
- {
- get
- {
- if (null == _instance)
- {
- lock (_lock)
- {
- if (null == _instance)
- {
- _instance = new LinkManager();
- }
- }
- }
- return _instance;
- }
- }
- private LinkManager() { }
- #endregion
- /// <summary>
- /// 链路初始化
- /// </summary>
- /// <param name="parameters">The parameters.</param>
- public void Initialize(LinkManagerParameters parameters)
- {
- Parameters = parameters;
- if (null == Parameters) return;
- switch (Parameters.TradeDatagramType)
- {
- case DatagramType.Protobuf:
- TradeAdapterFactory = new ProtobufFactory();
- break;
- case DatagramType.Json:
- TradeAdapterFactory = new JsonFactory();
- break;
- }
- }
- /// <summary>
- /// Creates the trade link.
- /// </summary>
- public void CreateTradeLink()
- {
- switch (Parameters.TradeLinkeType)
- {
- case LinkType.TCP:
- if (TradeTcpLinkProxy != null)
- {
- TradeTcpLinkProxy.Dispose();
- }
- TradeTcpLinkProxy = new TradeTcpLinkProxy(Parameters.TradeHost, Parameters.TradePort);
- break;
- default:
- ////to do:
- break;
- }
- }
- /// <summary>
- /// Creates the quotation link.
- /// </summary>
- public void CreateQuoteTcpLink()
- {
- if (QuoteTcpLinkProxy != null)
- {
- QuoteTcpLinkProxy.Dispose();
- }
- QuoteTcpLinkProxy = new QuoteTcpLinkProxy(Parameters.QuotationHost, Parameters.QuotationPort);
- }
- /// <summary>
- /// 创建游客行情连接地址
- /// </summary>
- public void CreateGuestQuoteTcpLink()
- {
- if (QuoteTcpLinkProxy != null)
- {
- QuoteTcpLinkProxy.Dispose();
- }
- GuestQuoteTcpLinkProxy = new QuoteTcpLinkProxy(Parameters.GuestQuotationHost, Parameters.GuestQuotationPort,true);
- }
- /// <summary>
- /// 获取行情链路
- /// </summary>
- /// <param name="isTradeLevel">是否是交易链路优先</param>
- /// <param name="sumType">商品类型</param>
- /// <returns></returns>
- public QuoteTcpLinkProxy SelectQuoteProxy(bool isTradeLevel, int sumType)
- {
- if ((sumType & (int) GoodsFromScr.Trade) > 0)
- {
- if (GuestQuoteTcpLinkProxy != null && GuestQuoteTcpLinkProxy.TcpConnectState==TCPConnectState.Connected && !isTradeLevel)
- {
- return GuestQuoteTcpLinkProxy;
- }
- else
- {
- return QuoteTcpLinkProxy;
- }
- }
- else
- {
- return GuestQuoteTcpLinkProxy;
- }
- }
- /// <summary>
- /// 执行与释放或重置非托管资源相关的应用程序定义的任务。
- /// </summary>
- public void Dispose()
- {
- ////停止断线重连
- StopReconnect();
-
- if (null != TradeTcpLinkProxy)
- {
- TradeTcpLinkProxy.Dispose();
- MessengerHelper.DefaultSend(false, MessengerTokens.TradeServerConnectMsg);
- }
- if (null != QuoteTcpLinkProxy)
- {
- QuoteTcpLinkProxy.Dispose();
- MessengerHelper.DefaultSend(false, MessengerTokens.QuoteServerStateChange);
- }
- if (null != GuestQuoteTcpLinkProxy)
- {
- GuestQuoteTcpLinkProxy.Dispose();
- MessengerHelper.DefaultSend(false, MessengerTokens.QuoteServerStateChange);
- }
- }
- public void DisposeWithOutGuestProxy()
- {
- ////停止断线重连
- StopReconnectWithoutGuest();
- if (null != TradeTcpLinkProxy)
- {
- TradeTcpLinkProxy.Dispose();
- MessengerHelper.DefaultSend(false, MessengerTokens.TradeServerConnectMsg);
- }
- if (null != QuoteTcpLinkProxy)
- {
- QuoteTcpLinkProxy.Dispose();
- MessengerHelper.DefaultSend(false, MessengerTokens.QuoteServerStateChange);
- }
- }
- /// <summary>
- /// 启动断线重连
- /// </summary>
- public void StartReconnect()
- {
- if (null != TradeTcpLinkProxy) TradeTcpLinkProxy.SetReconnectFlag(true);
- if (null != QuoteTcpLinkProxy) QuoteTcpLinkProxy.SetReconnectFlag(true);
- if (null != GuestQuoteTcpLinkProxy) GuestQuoteTcpLinkProxy.SetReconnectFlag(true);
- }
- /// <summary>
- /// 停止断线重连
- /// </summary>
- public void StopReconnect()
- {
- if (null != TradeTcpLinkProxy) TradeTcpLinkProxy.SetReconnectFlag(false);
- if (null != QuoteTcpLinkProxy) QuoteTcpLinkProxy.SetReconnectFlag(false);
- if (null != GuestQuoteTcpLinkProxy) GuestQuoteTcpLinkProxy.SetReconnectFlag(false);
- }
- /// <summary>
- /// 停止断线重连
- /// </summary>
- public void StopReconnectWithoutGuest()
- {
- if (null != TradeTcpLinkProxy) TradeTcpLinkProxy.SetReconnectFlag(false);
- if (null != QuoteTcpLinkProxy) QuoteTcpLinkProxy.SetReconnectFlag(false);
-
- }
- }
- }
|