TradeTcpCallback.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. using System.Threading;
  2. using GalaSoft.MvvmLight.Ioc;
  3. using Muchinfo.MTPClient.Adapter.Abstract;
  4. using Muchinfo.MTPClient.Data.Enums;
  5. using Muchinfo.MTPClient.Data.Helper;
  6. using Muchinfo.MTPClient.Data.Model;
  7. using Muchinfo.MTPClient.Infrastructure.Helpers;
  8. using Muchinfo.MTPClient.Infrastructure.Utilities;
  9. using Muchinfo.MTPClient.IService;
  10. using Muchinfo.MTPClient.NetworkCore;
  11. using Muchinfo.MTPClient.NetworkCore.Interfaces;
  12. using System;
  13. using System.Linq;
  14. using System.Threading.Tasks;
  15. using Muchinfo.PC.Common.Extensions;
  16. using Muchinfo.MTPClient.Data;
  17. namespace Muchinfo.MTPClient.Infrastructure.LinkProxy.TCP
  18. {
  19. public class TradeTcpCallback : INetworkCore
  20. {
  21. /// <summary>
  22. /// The _message adapter
  23. /// </summary>
  24. private readonly MessageAdapter _messageAdapter;
  25. private TradeTcpLinkProxy _owner;
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="TradeTcpCallback"/> class.
  28. /// </summary>
  29. public TradeTcpCallback(TradeTcpLinkProxy owner)
  30. {
  31. _messageAdapter = LinkManager.Instance.TradeAdapterFactory.CreateMessageAdapter();
  32. _owner = owner;
  33. }
  34. /// <summary>
  35. /// 当与服务端连接成功时的回调方法
  36. /// </summary>
  37. public void OnConnected()
  38. {
  39. //todo:20160504-011
  40. // throw new NotImplementedException();
  41. }
  42. /// <summary>
  43. /// 当与服务端连接断开时的回调方法
  44. /// </summary>
  45. public void OnDisconnected()
  46. {
  47. //todo: 20160504 - 011
  48. //throw new NotImplementedException();
  49. }
  50. /// <summary>
  51. /// 当发生断网重连状态变化时的回调方法
  52. /// </summary>
  53. /// <param name="state">重连状态枚举值</param>
  54. public void OnReconnectChangeState(ReconnectChangeState state)
  55. {
  56. // throw new NotImplementedException();
  57. switch (state)
  58. {
  59. case ReconnectChangeState.BeginReconnect:
  60. // 开始重连
  61. MessengerHelper.DefaultSend(false, MessengerTokens.TradeServerConnectMsg);
  62. LogInfoHelper.WriteInfo("交易断线开始重新连接!");
  63. break;
  64. case ReconnectChangeState.ReconnectFailAndWaitPeriod:
  65. // 尝试重连失败,将在下一个周期时间后尝试重连
  66. MessengerHelper.DefaultSend(false, MessengerTokens.TradeServerConnectMsg);
  67. LogInfoHelper.WriteInfo("交易尝试重连失败,将在下一个周期时间后尝试重连!");
  68. break;
  69. case ReconnectChangeState.ReconnectSuccess:
  70. LogInfoHelper.WriteInfo("交易代理断线重连成功!");
  71. //启动发送心跳信号
  72. if (_owner != null)
  73. {
  74. _owner.StartSendBeat();
  75. }
  76. //发送链路状态到StatusBarControl
  77. MessengerHelper.DefaultSend(true, MessengerTokens.TradeServerConnectMsg);
  78. // 重连成功
  79. // 判断当前Token是否有效
  80. SimpleIoc.Default.GetInstance<ILoginService>().TokenCheck(new Action<int>((statue) =>
  81. {
  82. // Token校验成功, 重新查询资金等信息
  83. // MessengerHelper.DefaultSend(string.Empty, MessengerTokens.MoneyNoticeToken);
  84. //下载持仓单
  85. MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount, MessengerTokens.OrderNotify);
  86. ////查询基本数据,包括了解查询资金.解决电脑休眠隔交易日重新连接后 涨跌停不正确问题,
  87. MessengerHelper.DefaultSend(string.Empty, MessengerTokens.UpdateCacheGoodsToken);
  88. }), (error) =>
  89. {
  90. MessengerHelper.DefaultSend(error, MessengerTokens.ConnectCheckedToken);
  91. });
  92. break;
  93. case ReconnectChangeState.NormalSuccess:
  94. // 正常连接成功
  95. MessengerHelper.DefaultSend(true, MessengerTokens.TradeServerConnectMsg);
  96. break;
  97. }
  98. }
  99. /// <summary>
  100. /// 当接收到服务端推送类报文时的回调方法
  101. /// </summary>
  102. /// <typeparam name="T"></typeparam>
  103. /// <param name="package">The package.</param>
  104. public void OnReceiveNotification<T>(T package)
  105. {
  106. //交易回调处理TCPPackage对象
  107. var tcpPackage = package as TCPPackage;
  108. if (tcpPackage == null || tcpPackage.Length <= 0 || tcpPackage.Content == null) return;
  109. switch (tcpPackage.FunCode)
  110. {
  111. case FuncCode.FID_OrderDealedNtf:
  112. LogInfoHelper.WriteInfo("收到单据成交通知");
  113. //TODO:不解包暂时不需要
  114. var orderComplete = _messageAdapter.ComplateNotice(tcpPackage.Content);
  115. //MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount, MessengerTokens.OrderNoticeToken);
  116. //发送更新委托通知
  117. MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount.FundsAccountId, MessengerTokens.ShowMOOrdersByCapitalAccount);
  118. LogInfoHelper.WriteInfo("单据成交通知单号:" + orderComplete.OrderId);
  119. break;
  120. case FuncCode.FID_OrderCanceledNtf:
  121. LogInfoHelper.WriteInfo("委托单撤单通知");
  122. //MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount, MessengerTokens.OrderNoticeToken);
  123. //发送更新委托通知
  124. MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount.FundsAccountId, MessengerTokens.ShowMOOrdersByCapitalAccount);
  125. break;
  126. case FuncCode.FID_OrderSuccessedNtf:
  127. LogInfoHelper.WriteInfo("委托单成功通知-多点登录");
  128. MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount.FundsAccountId, MessengerTokens.ShowMOOrdersByCapitalAccount);
  129. MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount.FundsAccountId, MessengerTokens.ShowMOTransactionByCapitalAccount);
  130. MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount.FundsAccountId, MessengerTokens.ShowMOHolderByCapitalAccount);
  131. MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount.FundsAccountId, MessengerTokens.ShowMOSPSLByCapitalAccount);
  132. if (LinkManager.Instance.QuoteTcpLinkProxy !=null) LinkManager.Instance.QuoteTcpLinkProxy.Subscribe();
  133. break;
  134. case FuncCode.FID_TradeDealedNtf:
  135. LogInfoHelper.WriteInfo("交易成交通知");
  136. //发送更新成交明细
  137. MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount.FundsAccountId, MessengerTokens.ShowMOTransactionByCapitalAccount);
  138. break;
  139. //头寸变化通知
  140. case FuncCode.FID_PosChangedNtf:
  141. LogInfoHelper.WriteInfo("头寸变化通知");
  142. var orderPos = _messageAdapter.PosChangedReturnMessage(tcpPackage.Content);
  143. //MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount, MessengerTokens.OrderNoticeToken);
  144. //发送更新头寸通知
  145. MessengerHelper.DefaultSend(orderPos.AccountID, MessengerTokens.ShowMOHolderByCapitalAccount);
  146. LogInfoHelper.WriteInfo("头寸变化通知商品ID号:" + orderPos.GoodsID);
  147. break;
  148. case FuncCode.FID_LogoutRsp:
  149. LogInfoHelper.WriteInfo("交易代理用户登出应答,离线!");
  150. //手动发送登出消息
  151. var service = SimpleIoc.Default.GetInstance<ILoginService>();
  152. if (service != null) service.TradeAccountLogout(UserManager.CurrentTradeAccount);
  153. var error= _messageAdapter.LogoutRspConvert(tcpPackage.Content);
  154. if (error.ReturnCode == 0) error.ReturnCode = 1009;
  155. MessengerHelper.DefaultSend(error, MessengerTokens.UserOfflineNoticeToken);
  156. break;
  157. case FuncCode.FID_MarketStatusChangeNtf:
  158. // 待开市通知则为结算下线
  159. var ntfModel = _messageAdapter.MarketStatusChangeNtf(tcpPackage.Content);
  160. if (ntfModel.Status == 1)
  161. {
  162. var service1 = SimpleIoc.Default.GetInstance<ILoginService>();
  163. if (service1 != null) service1.TradeAccountLogout(UserManager.CurrentTradeAccount);
  164. //var error1 = _messageAdapter.LogoutRspConvert(tcpPackage.Content);
  165. //if (error1.ReturnCode == 0) error1.ReturnCode = 1009;
  166. MessengerHelper.DefaultSend(RestartType.SttleLoginOut, MessengerTokens.UserOfflineNoticesSttle);
  167. LogInfoHelper.WriteInfo("市场状态变更通知,离线(结算)!");
  168. }
  169. break;
  170. case FuncCode.FID_MoneyChangedNtf:
  171. LogInfoHelper.WriteInfo("资金变化的通知");
  172. var amountArg = _messageAdapter.ConvertAmountNotice(tcpPackage.Content);
  173. Task.Factory.StartNew(() =>
  174. {
  175. MessengerHelper.DefaultSend(amountArg, MessengerTokens.MoneyNoticeToken);
  176. });
  177. break;
  178. ////
  179. case FuncCode.FID_CustOfflineNtf:
  180. LogInfoHelper.WriteInfo("通信超时失效!");
  181. MessengerHelper.DefaultSend(RestartType.LinkFail, MessengerTokens.UserOfflineNoticeToken);
  182. break;
  183. //公告消息推送
  184. case FuncCode.FID_BltMsgNtf:
  185. var notifyBulletin = _messageAdapter.NotifyBulletin(tcpPackage.Content);
  186. MessengerHelper.DefaultSend(notifyBulletin, MessengerTokens.AnnouncementMessage);
  187. break;
  188. //case FuncCode.FID_RiskControlMsg:
  189. // var notifyControlMsg = _messageAdapter.RiskCtrlMemberNotice(tcpPackage.Content);
  190. // notifyControlMsg.RiskMsgType = RiskMsgType.StatusChange;
  191. // LogInfoHelper.WriteInfo("风控通知,账号:"+notifyControlMsg.LoginCode);
  192. // MessengerHelper.DefaultSend(notifyControlMsg, MessengerTokens.RiskControl);
  193. // break;
  194. //case FuncCode.FID_RiskCtrlMemRsp:
  195. // //暂时不处理
  196. // var RiskCtrlMem = _messageAdapter.RiskCtrlMemberBreak(tcpPackage.Content);
  197. // RiskCtrlMem.RiskMsgType = RiskMsgType.BreakLevel;
  198. // RiskCtrlMem.RiskLevel = 3; ////服务器没有给级别,斩仓级别显示红色
  199. // LogInfoHelper.WriteInfo("斩仓通知,账号:" + RiskCtrlMem.LoginCode);
  200. // MessengerHelper.DefaultSend(RiskCtrlMem, MessengerTokens.RiskControl);
  201. // break;
  202. //预埋单
  203. //case FuncCode.FID_PrepostTransferOrderNtf:
  204. // var prepostTransfer = _messageAdapter.PrepostTransferOrderNtf(tcpPackage.Content);
  205. // //todo:更新预埋单 委托单
  206. // //var cacheData = UserManager.GetTradeCacheData(UserManager.CurrentTradeAccount.AccountId);
  207. // //cacheData.LimitOrderFlag = false;
  208. // MessengerHelper.DefaultSend(true, MessengerTokens.PreOrderUpdate);
  209. // LogInfoHelper.WriteInfo("收到预埋单转委托通知!");
  210. // break;
  211. ////投资者公告
  212. //case FuncCode.FID_RiskCtrlBltRsp:
  213. // var riskCtrlBltNotice = _messageAdapter.RiskCtrlBltNotice(tcpPackage.Content);
  214. // MessengerHelper.DefaultSend(riskCtrlBltNotice, MessengerTokens.RiskControl);
  215. // break;
  216. //操作变更通知
  217. //case FuncCode.FID_OperateChangeNotifyRsp:
  218. // var OperateChangeNotifyContent = _messageAdapter.OperateChangeNotifyAd(tcpPackage.Content);
  219. // AppMessengerHelper.OperateChangeNotifyMessage.Send(OperateChangeNotifyContent);
  220. // break;
  221. //开休市计划通知[Add By DK 20160629]
  222. //case FuncCode.FID_MarketStatusChangeNotify:
  223. // LogInfoHelper.WriteInfo("开休市计划通知,FunCode:" + FuncCode.FID_MarketStatusChangeNotify);
  224. // var MarketStatusChangeNotifyContent = _messageAdapter.MarketStatusChangeNotifyAd(tcpPackage.Content);
  225. // AppMessengerHelper.MarketStatusChangeNotifyMessage.Send(MarketStatusChangeNotifyContent);
  226. // break;
  227. //case FuncCode.FID_GoodsChangeNotifyRsp:
  228. // //todo:交易端暂时不区分单个商品内容的更新
  229. // MessengerHelper.DefaultSend(string.Empty, MessengerTokens.UpdateCacheGoodsToken);
  230. // LogInfoHelper.WriteInfo("商品状态更新,FID_GoodsChangeNotifyRsp:" + FuncCode.FID_GoodsChangeNotifyRsp);
  231. // break;
  232. //case FuncCode.FID_AccountChangeNotifyRsp:
  233. // var OperateChangeNotifyContent = _messageAdapter.AccountChangeNotifyRspNotifyAd(tcpPackage.Content);
  234. // MessengerHelper.DefaultSend(string.Empty, MessengerTokens.AccountParamChangeToken);
  235. // break;
  236. //case FuncCode.FID_DeliveryAuditNtf: //交收审核通知
  237. // var DeliveryAuditNtfContent = _messageAdapter.DeliveryReturnMessage(tcpPackage.Content);
  238. // LogInfoHelper.WriteInfo("交收审核通知,FID_DeliveryAuditNtf:" + FuncCode.FID_DeliveryAuditNtf + ", ReturnCode:" + DeliveryAuditNtfContent.RetCode);
  239. // MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount, MessengerTokens.OrderNoticeToken);
  240. // break;
  241. //case FuncCode.FID_OutStoreAuditNtf: //提货审核通知
  242. // var OutStoreAuditNtfContent = _messageAdapter.OutStoreAuditReturnMessage(tcpPackage.Content);
  243. // LogInfoHelper.WriteInfo("提货审核通知,FID_OutStoreAuditNtf:" + FuncCode.FID_OutStoreAuditNtf + ", ReturnCode:" + OutStoreAuditNtfContent.RetCode);
  244. // MessengerHelper.DefaultSend(OutStoreAuditNtfContent, MessengerTokens.OutStoreAuditToken);
  245. // MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount, MessengerTokens.OrderNoticeToken);
  246. // break;
  247. //case FuncCode.FID_GoodsCloseDateNtf: //交割商品到期提货通知
  248. // var GoodsCloseDateNtfContent = _messageAdapter.GoodsCloseDateReturnMessage(tcpPackage.Content);
  249. // LogInfoHelper.WriteInfo("交割商品到期提货通知,FID_GoodsCloseDateNtf:" + FuncCode.FID_GoodsCloseDateNtf + ", ReturnCode:" + GoodsCloseDateNtfContent.RetCode);
  250. // MessengerHelper.DefaultSend(GoodsCloseDateNtfContent, MessengerTokens.GoodsCloseDateToken);
  251. // break;
  252. //case FuncCode.FID_ListingBroadcastNtf: ////挂牌改价,数量改变,审核通知
  253. // var listingNtfContent = _messageAdapter.ToListingNoticeModel(tcpPackage.Content);
  254. // LogInfoHelper.WriteInfo("挂牌改价,数量改变,审核通知,FID_ListingBroadcastNtf :" + FuncCode.FID_ListingBroadcastNtf + ", ReturnCode:" + listingNtfContent.RetCode);
  255. // MessengerHelper.DefaultSend(listingNtfContent.Entity, MessengerTokens.ListingNotice);
  256. // break;
  257. //case FuncCode.FID_ListingNtf:
  258. // var delistingNtf = _messageAdapter.ToListingNoticeModel(tcpPackage.Content);
  259. // LogInfoHelper.WriteInfo("摘牌成交,FID_ListingNtf :" + FuncCode.FID_ListingNtf + ", ReturnCode:" + delistingNtf.RetCode);
  260. // MessengerHelper.DefaultSend(delistingNtf.Entity, MessengerTokens.DelistingCompleteNotice);
  261. // break;
  262. ////会员风控通知
  263. //case FuncCode.FID_RiskControlMember:
  264. // var RiskControlMemberContent = _messageAdapter.RiskControlMemberReturnMessage(tcpPackage.Content);
  265. // LogInfoHelper.WriteInfo("会员风控通知:" + FuncCode.FID_RiskControlMember + ", ReturnCode:" + RiskControlMemberContent.RetCode);
  266. // MessengerHelper.DefaultSend(RiskControlMemberContent, MessengerTokens.RiskControlMemberToken);
  267. // break;
  268. //投资者风控通知
  269. case FuncCode.FID_RiskToWebNtf:
  270. var RiskControlInvestorContent = _messageAdapter.RiskControlInvestorReturnMessage(tcpPackage.Content);
  271. LogInfoHelper.WriteInfo("投资者风控通知:" + FuncCode.FID_RiskControlNtf + ", ReturnCode:" + RiskControlInvestorContent.RetCode);
  272. MessengerHelper.DefaultSend(RiskControlInvestorContent, MessengerTokens.RiskControlInvestorToken);
  273. break;
  274. ////通用通知
  275. //case FuncCode.FID_CommonNotify:
  276. // var CommonNtfContent = _messageAdapter.CommonNotifyReturnMessage(tcpPackage.Content);
  277. // LogInfoHelper.WriteInfo("通用通知,通知标题:" + CommonNtfContent.Title + ", ReturnCode:" + CommonNtfContent.RetCode);
  278. // MessengerHelper.DefaultSend(CommonNtfContent, MessengerTokens.CommonMessageToken);
  279. // break;
  280. //case FuncCode.FID_CommonBroadcastNtf: ////通用广播通知
  281. // var massgerfuncode = _messageAdapter.ToMessageHeadFunCode(tcpPackage.Content);
  282. // PackageBroadNtf((int) massgerfuncode, tcpPackage.Content);
  283. // break;
  284. }
  285. }
  286. /// <summary>
  287. /// 解通知具体内容
  288. /// </summary>
  289. /// <param name="funcode"></param>
  290. /// <param name="buffer"></param>
  291. private void PackageBroadNtf(int funcode, byte[] buffer)
  292. {
  293. switch (funcode)
  294. {
  295. //case FuncCode.FID_MarketStatusRsp:
  296. // var massgerfuncode = _messageAdapter.ToMarketStatusRsp(buffer);
  297. // if (massgerfuncode.RetCode == 0) ////结算成功通知 更新单据
  298. // {
  299. // Task.Factory.TryStartNew(() =>
  300. // {
  301. // var random = new Random();
  302. // var second = random.Next(500, 3000);
  303. // Thread.Sleep(second);
  304. // MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount,
  305. // MessengerTokens.OrderNoticeToken);
  306. // });
  307. // }
  308. // break;
  309. //case FuncCode.FID_ListingBroadcastNtf: ////挂牌改价,数量改变,审核通知
  310. // var listingNtfContent = _messageAdapter.ToListingNoticeModel(buffer);
  311. // LogInfoHelper.WriteInfo("挂牌改价,数量改变,审核通知,FID_ListingBroadcastNtf :" + FuncCode.FID_ListingBroadcastNtf + ", ReturnCode:" + listingNtfContent.RetCode);
  312. // MessengerHelper.DefaultSend(listingNtfContent.Entity, MessengerTokens.ListingNotice);
  313. // break;
  314. //case FuncCode.FID_ListingNtf:
  315. // var delistingNtf = _messageAdapter.ToListingNoticeModel(buffer);
  316. // LogInfoHelper.WriteInfo("摘牌成交,FID_ListingNtf :" + FuncCode.FID_ListingNtf + ", ReturnCode:" + delistingNtf.RetCode);
  317. // MessengerHelper.DefaultSend(delistingNtf.Entity, MessengerTokens.DelistingCompleteNotice);
  318. // break;
  319. default:
  320. break;
  321. }
  322. }
  323. }
  324. }