| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
-
- using System.Linq;
- using Muchinfo.MTPClient.Adapter.Quote;
- using Muchinfo.MTPClient.Data;
- using Muchinfo.MTPClient.Data.Enums;
- using Muchinfo.MTPClient.Data.Helper;
- using Muchinfo.MTPClient.Infrastructure.EntityHelpers;
- using Muchinfo.MTPClient.Infrastructure.Helpers;
- using Muchinfo.MTPClient.Infrastructure.Utilities;
- using Muchinfo.MTPClient.NetworkCore;
- using Muchinfo.MTPClient.NetworkCore.Interfaces;
- using System;
- using System.Threading;
- using Muchinfo.MTPClient.NetworkCore.Package;
- namespace Muchinfo.MTPClient.Infrastructure.LinkProxy.TCP
- {
- public class QuoteTcpCallback : INetworkCore
- {
- private readonly QuoteDateAdapter _quoeAdapter;
- private QuoteTcpLinkProxy _owner;
- public QuoteTcpCallback(QuoteTcpLinkProxy owner)
- {
- _owner = owner;
- _quoeAdapter=new QuoteDateAdapter();
- }
- public void OnConnected()
- {
- }
- public void OnDisconnected()
- {
- }
- public void OnReconnectChangeState(ReconnectChangeState state)
- {
- switch (state)
- {
- case ReconnectChangeState.BeginReconnect:
- // 开始重连
- MessengerHelper.DefaultSend(false, MessengerTokens.QuoteServerStateChange);
- LogInfoHelper.WriteInfo("行情断线开始重新连接!");
- break;
- case ReconnectChangeState.ReconnectFailAndWaitPeriod:
- // 尝试重连失败,将在下一个周期时间后尝试重连
- MessengerHelper.DefaultSend(false, MessengerTokens.QuoteServerStateChange);
- LogInfoHelper.WriteInfo("行情尝试重连失败,将在下一个周期时间后尝试重连!");
- break;
- case ReconnectChangeState.ReconnectSuccess:
- LogInfoHelper.WriteInfo("行情断线重连成功!");
- //发送链路状态到StatusBarControl
- MessengerHelper.DefaultSend(true, MessengerTokens.QuoteServerStateChange);
- // 重连成功重新订阅
- if (_owner == null) return;
- //停止同步盘面定时器,Subscribe()成功后会启动
- _owner.StopSyncTimer();
- _owner.ClearQuote(); //清盘
- // 随机500-1000毫秒后尝试重连
- var random = new Random();
- var second = random.Next(500, 1000);
- Thread.Sleep(second);
- _owner.CheckedToken(); ////行情链路取服务器时间
- //订阅行情
- _owner.Subscribe();
- break;
- case ReconnectChangeState.NormalSuccess:
- // 正常连接成功
- MessengerHelper.DefaultSend(true, MessengerTokens.QuoteServerStateChange);
- break;
- }
- }
- public void OnReceiveNotification<T>(T package)
- {
- //行情回调处理字节数组
- var bytes = package as byte[];
- if (bytes == null || bytes.Length < 15) return;
- var tag = bytes[5];
- switch (tag)
- {
- case (byte)Datagram40TagPrimaryFunction.Quote:
- //字节数组转对象
- var quoteTiks = bytes.ToQuoteTiks();
- //对象转换发送实时行情消息
- quoteTiks.UpdateQuoteGoodses(false);
- break;
- case (byte)Datagram40TagPrimaryFunction.MarketStatus: ////开收市通知
- var errorEntity=new ErrorEntity(){RequestFunc = "开收市状态"};
- var package40= TCPPackage40Utils.FromByteArray(bytes);
- var markets= _quoeAdapter.ConvertFromMarketStatusJson(package40.GetData(), ref errorEntity);
- if (markets != null && markets.Any())
- {
- AppMessengerHelper.MarketStatusChangeNotifyMessage.Send(markets);
- }
- LogInfoHelper.WriteInfo(ErrorManager.FormatErrorMsg(errorEntity));
- break;
- default:
- break;
- }
- }
- }
- }
|