LinkManager.cs 8.0 KB

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