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; } /// /// GO服务地址 /// public string GoCommonSearchUrl { get; set; } public string GuestQuotationHost { get; set; } public int GuestQuotationPort { get; set; } } public class LinkManager : IDisposable { #region Public Fields /// /// 链路参数 /// public LinkManagerParameters Parameters { get; set; } /// /// 业务对象数据转换器 /// /// The biz adapter factory. public AdapterFactory TradeAdapterFactory { get; private set; } /// /// Gets or sets the trade TCP link proxy. /// /// The trade TCP link proxy. public TradeTcpLinkProxy TradeTcpLinkProxy { get; set; } /// /// Gets or sets the Quote TCP link proxy. /// /// The Quote TCP link proxy. public QuoteTcpLinkProxy QuoteTcpLinkProxy { get; set; } /// /// 游客看行情地址 /// 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 /// /// 链路初始化 /// /// The parameters. 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; } } /// /// Creates the trade link. /// 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; } } /// /// Creates the quotation link. /// public void CreateQuoteTcpLink() { if (QuoteTcpLinkProxy != null) { QuoteTcpLinkProxy.Dispose(); } QuoteTcpLinkProxy = new QuoteTcpLinkProxy(Parameters.QuotationHost, Parameters.QuotationPort); } /// /// 创建游客行情连接地址 /// public void CreateGuestQuoteTcpLink() { if (QuoteTcpLinkProxy != null) { QuoteTcpLinkProxy.Dispose(); } GuestQuoteTcpLinkProxy = new QuoteTcpLinkProxy(Parameters.GuestQuotationHost, Parameters.GuestQuotationPort,true); } /// /// 获取行情链路 /// /// 是否是交易链路优先 /// 商品类型 /// 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; } } /// /// 执行与释放或重置非托管资源相关的应用程序定义的任务。 /// 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); } } /// /// 启动断线重连 /// public void StartReconnect() { if (null != TradeTcpLinkProxy) TradeTcpLinkProxy.SetReconnectFlag(true); if (null != QuoteTcpLinkProxy) QuoteTcpLinkProxy.SetReconnectFlag(true); if (null != GuestQuoteTcpLinkProxy) GuestQuoteTcpLinkProxy.SetReconnectFlag(true); } /// /// 停止断线重连 /// public void StopReconnect() { if (null != TradeTcpLinkProxy) TradeTcpLinkProxy.SetReconnectFlag(false); if (null != QuoteTcpLinkProxy) QuoteTcpLinkProxy.SetReconnectFlag(false); if (null != GuestQuoteTcpLinkProxy) GuestQuoteTcpLinkProxy.SetReconnectFlag(false); } /// /// 停止断线重连 /// public void StopReconnectWithoutGuest() { if (null != TradeTcpLinkProxy) TradeTcpLinkProxy.SetReconnectFlag(false); if (null != QuoteTcpLinkProxy) QuoteTcpLinkProxy.SetReconnectFlag(false); } } }