LinkManager.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using GalaSoft.MvvmLight.Messaging;
  2. using Muchinfo.MTPClient.Adapter.Factory;
  3. using Muchinfo.MTPClient.Data.Enums;
  4. using Muchinfo.MTPClient.Infrastructure.LinkProxy.Enum;
  5. using Muchinfo.MTPClient.Infrastructure.LinkProxy.TCP;
  6. using System;
  7. using Muchinfo.MTPClient.Infrastructure.Helpers;
  8. using Muchinfo.MTPClient.NetworkCore;
  9. using DatagramType = Muchinfo.MTPClient.Infrastructure.LinkProxy.Enum.DatagramType;
  10. namespace Muchinfo.MTPClient.Infrastructure.LinkProxy
  11. {
  12. public class LinkManagerParameters
  13. {
  14. public string TradeHost { get; set; }
  15. public int TradePort { get; set; }
  16. public string QuotationHost { get; set; }
  17. public int QuotationPort { get; set; }
  18. public LinkType TradeLinkeType { get; set; }
  19. public DatagramType TradeDatagramType { get; set; }
  20. public string GuestQuotationHost { get; set; }
  21. public int GuestQuotationPort { get; set; }
  22. }
  23. public class LinkManager : IDisposable
  24. {
  25. #region Public Fields
  26. /// <summary>
  27. /// 链路参数
  28. /// </summary>
  29. public LinkManagerParameters Parameters { get; set; }
  30. /// <summary>
  31. /// 业务对象数据转换器
  32. /// </summary>
  33. /// <value>The biz adapter factory.</value>
  34. public AdapterFactory TradeAdapterFactory { get; private set; }
  35. /// <summary>
  36. /// Gets or sets the trade TCP link proxy.
  37. /// </summary>
  38. /// <value>The trade TCP link proxy.</value>
  39. public TradeTcpLinkProxy TradeTcpLinkProxy { get; set; }
  40. /// <summary>
  41. /// Gets or sets the Quote TCP link proxy.
  42. /// </summary>
  43. /// <value>The Quote TCP link proxy.</value>
  44. public QuoteTcpLinkProxy QuoteTcpLinkProxy { get; set; }
  45. /// <summary>
  46. /// 游客看行情地址
  47. /// </summary>
  48. public QuoteTcpLinkProxy GuestQuoteTcpLinkProxy { get; set; }
  49. #endregion
  50. #region Singalton
  51. private static LinkManager _instance;
  52. private static object _lock = new object();
  53. public static LinkManager Instance
  54. {
  55. get
  56. {
  57. if (null == _instance)
  58. {
  59. lock (_lock)
  60. {
  61. if (null == _instance)
  62. {
  63. _instance = new LinkManager();
  64. }
  65. }
  66. }
  67. return _instance;
  68. }
  69. }
  70. private LinkManager() { }
  71. #endregion
  72. /// <summary>
  73. /// 链路初始化
  74. /// </summary>
  75. /// <param name="parameters">The parameters.</param>
  76. public void Initialize(LinkManagerParameters parameters)
  77. {
  78. Parameters = parameters;
  79. if (null == Parameters) return;
  80. switch (Parameters.TradeDatagramType)
  81. {
  82. case DatagramType.Protobuf:
  83. TradeAdapterFactory = new ProtobufFactory();
  84. break;
  85. case DatagramType.Json:
  86. TradeAdapterFactory = new JsonFactory();
  87. break;
  88. }
  89. }
  90. /// <summary>
  91. /// Creates the trade link.
  92. /// </summary>
  93. public void CreateTradeLink()
  94. {
  95. switch (Parameters.TradeLinkeType)
  96. {
  97. case LinkType.TCP:
  98. if (TradeTcpLinkProxy != null)
  99. {
  100. TradeTcpLinkProxy.Dispose();
  101. }
  102. TradeTcpLinkProxy = new TradeTcpLinkProxy(Parameters.TradeHost, Parameters.TradePort);
  103. break;
  104. default:
  105. ////to do:
  106. break;
  107. }
  108. }
  109. /// <summary>
  110. /// Creates the quotation link.
  111. /// </summary>
  112. public void CreateQuoteTcpLink()
  113. {
  114. if (QuoteTcpLinkProxy != null)
  115. {
  116. QuoteTcpLinkProxy.Dispose();
  117. }
  118. QuoteTcpLinkProxy = new QuoteTcpLinkProxy(Parameters.QuotationHost, Parameters.QuotationPort);
  119. }
  120. /// <summary>
  121. /// 创建游客行情连接地址
  122. /// </summary>
  123. public void CreateGuestQuoteTcpLink()
  124. {
  125. if (QuoteTcpLinkProxy != null)
  126. {
  127. QuoteTcpLinkProxy.Dispose();
  128. }
  129. GuestQuoteTcpLinkProxy = new QuoteTcpLinkProxy(Parameters.GuestQuotationHost, Parameters.GuestQuotationPort,true);
  130. }
  131. /// <summary>
  132. /// 获取行情链路
  133. /// </summary>
  134. /// <param name="isTradeLevel">是否是交易链路优先</param>
  135. /// <param name="sumType">商品类型</param>
  136. /// <returns></returns>
  137. public QuoteTcpLinkProxy SelectQuoteProxy(bool isTradeLevel, int sumType)
  138. {
  139. if ((sumType & (int) GoodsFromScr.Trade) > 0)
  140. {
  141. if (GuestQuoteTcpLinkProxy != null && GuestQuoteTcpLinkProxy.TcpConnectState==TCPConnectState.Connected && !isTradeLevel)
  142. {
  143. return GuestQuoteTcpLinkProxy;
  144. }
  145. else
  146. {
  147. return QuoteTcpLinkProxy;
  148. }
  149. }
  150. else
  151. {
  152. return GuestQuoteTcpLinkProxy;
  153. }
  154. }
  155. /// <summary>
  156. /// 执行与释放或重置非托管资源相关的应用程序定义的任务。
  157. /// </summary>
  158. public void Dispose()
  159. {
  160. ////停止断线重连
  161. StopReconnect();
  162. if (null != TradeTcpLinkProxy)
  163. {
  164. TradeTcpLinkProxy.Dispose();
  165. MessengerHelper.DefaultSend(false, MessengerTokens.TradeServerConnectMsg);
  166. }
  167. if (null != QuoteTcpLinkProxy)
  168. {
  169. QuoteTcpLinkProxy.Dispose();
  170. MessengerHelper.DefaultSend(false, MessengerTokens.QuoteServerStateChange);
  171. }
  172. if (null != GuestQuoteTcpLinkProxy)
  173. {
  174. GuestQuoteTcpLinkProxy.Dispose();
  175. MessengerHelper.DefaultSend(false, MessengerTokens.QuoteServerStateChange);
  176. }
  177. }
  178. public void DisposeWithOutGuestProxy()
  179. {
  180. ////停止断线重连
  181. StopReconnectWithoutGuest();
  182. if (null != TradeTcpLinkProxy)
  183. {
  184. TradeTcpLinkProxy.Dispose();
  185. MessengerHelper.DefaultSend(false, MessengerTokens.TradeServerConnectMsg);
  186. }
  187. if (null != QuoteTcpLinkProxy)
  188. {
  189. QuoteTcpLinkProxy.Dispose();
  190. MessengerHelper.DefaultSend(false, MessengerTokens.QuoteServerStateChange);
  191. }
  192. }
  193. /// <summary>
  194. /// 启动断线重连
  195. /// </summary>
  196. public void StartReconnect()
  197. {
  198. if (null != TradeTcpLinkProxy) TradeTcpLinkProxy.SetReconnectFlag(true);
  199. if (null != QuoteTcpLinkProxy) QuoteTcpLinkProxy.SetReconnectFlag(true);
  200. if (null != GuestQuoteTcpLinkProxy) GuestQuoteTcpLinkProxy.SetReconnectFlag(true);
  201. }
  202. /// <summary>
  203. /// 停止断线重连
  204. /// </summary>
  205. public void StopReconnect()
  206. {
  207. if (null != TradeTcpLinkProxy) TradeTcpLinkProxy.SetReconnectFlag(false);
  208. if (null != QuoteTcpLinkProxy) QuoteTcpLinkProxy.SetReconnectFlag(false);
  209. if (null != GuestQuoteTcpLinkProxy) GuestQuoteTcpLinkProxy.SetReconnectFlag(false);
  210. }
  211. /// <summary>
  212. /// 停止断线重连
  213. /// </summary>
  214. public void StopReconnectWithoutGuest()
  215. {
  216. if (null != TradeTcpLinkProxy) TradeTcpLinkProxy.SetReconnectFlag(false);
  217. if (null != QuoteTcpLinkProxy) QuoteTcpLinkProxy.SetReconnectFlag(false);
  218. }
  219. }
  220. }