QuoteTcpCallback.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. 
  2. using System.Linq;
  3. using Muchinfo.MTPClient.Adapter.Quote;
  4. using Muchinfo.MTPClient.Data;
  5. using Muchinfo.MTPClient.Data.Enums;
  6. using Muchinfo.MTPClient.Data.Helper;
  7. using Muchinfo.MTPClient.Infrastructure.EntityHelpers;
  8. using Muchinfo.MTPClient.Infrastructure.Helpers;
  9. using Muchinfo.MTPClient.Infrastructure.Utilities;
  10. using Muchinfo.MTPClient.NetworkCore;
  11. using Muchinfo.MTPClient.NetworkCore.Interfaces;
  12. using System;
  13. using System.Threading;
  14. using Muchinfo.MTPClient.NetworkCore.Package;
  15. namespace Muchinfo.MTPClient.Infrastructure.LinkProxy.TCP
  16. {
  17. public class QuoteTcpCallback : INetworkCore
  18. {
  19. private readonly QuoteDateAdapter _quoeAdapter;
  20. private QuoteTcpLinkProxy _owner;
  21. public QuoteTcpCallback(QuoteTcpLinkProxy owner)
  22. {
  23. _owner = owner;
  24. _quoeAdapter=new QuoteDateAdapter();
  25. }
  26. public void OnConnected()
  27. {
  28. }
  29. public void OnDisconnected()
  30. {
  31. }
  32. public void OnReconnectChangeState(ReconnectChangeState state)
  33. {
  34. switch (state)
  35. {
  36. case ReconnectChangeState.BeginReconnect:
  37. // 开始重连
  38. MessengerHelper.DefaultSend(false, MessengerTokens.QuoteServerStateChange);
  39. LogInfoHelper.WriteInfo("行情断线开始重新连接!");
  40. break;
  41. case ReconnectChangeState.ReconnectFailAndWaitPeriod:
  42. // 尝试重连失败,将在下一个周期时间后尝试重连
  43. MessengerHelper.DefaultSend(false, MessengerTokens.QuoteServerStateChange);
  44. LogInfoHelper.WriteInfo("行情尝试重连失败,将在下一个周期时间后尝试重连!");
  45. break;
  46. case ReconnectChangeState.ReconnectSuccess:
  47. LogInfoHelper.WriteInfo("行情断线重连成功!");
  48. //发送链路状态到StatusBarControl
  49. MessengerHelper.DefaultSend(true, MessengerTokens.QuoteServerStateChange);
  50. // 重连成功重新订阅
  51. if (_owner == null) return;
  52. //停止同步盘面定时器,Subscribe()成功后会启动
  53. _owner.StopSyncTimer();
  54. _owner.ClearQuote(); //清盘
  55. // 随机500-1000毫秒后尝试重连
  56. var random = new Random();
  57. var second = random.Next(500, 1000);
  58. Thread.Sleep(second);
  59. _owner.CheckedToken(); ////行情链路取服务器时间
  60. //订阅行情
  61. _owner.Subscribe();
  62. break;
  63. case ReconnectChangeState.NormalSuccess:
  64. // 正常连接成功
  65. MessengerHelper.DefaultSend(true, MessengerTokens.QuoteServerStateChange);
  66. break;
  67. }
  68. }
  69. public void OnReceiveNotification<T>(T package)
  70. {
  71. //行情回调处理字节数组
  72. var bytes = package as byte[];
  73. if (bytes == null || bytes.Length < 15) return;
  74. var tag = bytes[5];
  75. switch (tag)
  76. {
  77. case (byte)Datagram40TagPrimaryFunction.Quote:
  78. //字节数组转对象
  79. var quoteTiks = bytes.ToQuoteTiks();
  80. //对象转换发送实时行情消息
  81. quoteTiks.UpdateQuoteGoodses(false);
  82. break;
  83. case (byte)Datagram40TagPrimaryFunction.MarketStatus: ////开收市通知
  84. var errorEntity=new ErrorEntity(){RequestFunc = "开收市状态"};
  85. var package40= TCPPackage40Utils.FromByteArray(bytes);
  86. var markets= _quoeAdapter.ConvertFromMarketStatusJson(package40.GetData(), ref errorEntity);
  87. if (markets != null && markets.Any())
  88. {
  89. AppMessengerHelper.MarketStatusChangeNotifyMessage.Send(markets);
  90. }
  91. LogInfoHelper.WriteInfo(ErrorManager.FormatErrorMsg(errorEntity));
  92. break;
  93. default:
  94. break;
  95. }
  96. }
  97. }
  98. }