using System.Threading; using GalaSoft.MvvmLight.Ioc; using Muchinfo.MTPClient.Adapter.Abstract; using Muchinfo.MTPClient.Data.Enums; using Muchinfo.MTPClient.Data.Helper; using Muchinfo.MTPClient.Data.Model; using Muchinfo.MTPClient.Infrastructure.Helpers; using Muchinfo.MTPClient.Infrastructure.Utilities; using Muchinfo.MTPClient.IService; using Muchinfo.MTPClient.NetworkCore; using Muchinfo.MTPClient.NetworkCore.Interfaces; using System; using System.Linq; using System.Threading.Tasks; using Muchinfo.PC.Common.Extensions; using Muchinfo.MTPClient.Data; namespace Muchinfo.MTPClient.Infrastructure.LinkProxy.TCP { public class TradeTcpCallback : INetworkCore { /// /// The _message adapter /// private readonly MessageAdapter _messageAdapter; private TradeTcpLinkProxy _owner; /// /// Initializes a new instance of the class. /// public TradeTcpCallback(TradeTcpLinkProxy owner) { _messageAdapter = LinkManager.Instance.TradeAdapterFactory.CreateMessageAdapter(); _owner = owner; } /// /// 当与服务端连接成功时的回调方法 /// public void OnConnected() { //todo:20160504-011 // throw new NotImplementedException(); } /// /// 当与服务端连接断开时的回调方法 /// public void OnDisconnected() { //todo: 20160504 - 011 //throw new NotImplementedException(); } /// /// 当发生断网重连状态变化时的回调方法 /// /// 重连状态枚举值 public void OnReconnectChangeState(ReconnectChangeState state) { // throw new NotImplementedException(); switch (state) { case ReconnectChangeState.BeginReconnect: // 开始重连 MessengerHelper.DefaultSend(false, MessengerTokens.TradeServerConnectMsg); LogInfoHelper.WriteInfo("交易断线开始重新连接!"); break; case ReconnectChangeState.ReconnectFailAndWaitPeriod: // 尝试重连失败,将在下一个周期时间后尝试重连 MessengerHelper.DefaultSend(false, MessengerTokens.TradeServerConnectMsg); LogInfoHelper.WriteInfo("交易尝试重连失败,将在下一个周期时间后尝试重连!"); break; case ReconnectChangeState.ReconnectSuccess: LogInfoHelper.WriteInfo("交易代理断线重连成功!"); //启动发送心跳信号 if (_owner != null) { _owner.StartSendBeat(); } //发送链路状态到StatusBarControl MessengerHelper.DefaultSend(true, MessengerTokens.TradeServerConnectMsg); // 重连成功 // 判断当前Token是否有效 SimpleIoc.Default.GetInstance().TokenCheck(new Action((statue) => { // Token校验成功, 重新查询资金等信息 // MessengerHelper.DefaultSend(string.Empty, MessengerTokens.MoneyNoticeToken); //下载持仓单 MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount, MessengerTokens.OrderNotify); ////查询基本数据,包括了解查询资金.解决电脑休眠隔交易日重新连接后 涨跌停不正确问题, MessengerHelper.DefaultSend(string.Empty, MessengerTokens.UpdateCacheGoodsToken); }), (error) => { MessengerHelper.DefaultSend(error, MessengerTokens.ConnectCheckedToken); }); break; case ReconnectChangeState.NormalSuccess: // 正常连接成功 MessengerHelper.DefaultSend(true, MessengerTokens.TradeServerConnectMsg); break; } } /// /// 当接收到服务端推送类报文时的回调方法 /// /// /// The package. public void OnReceiveNotification(T package) { //交易回调处理TCPPackage对象 var tcpPackage = package as TCPPackage; if (tcpPackage == null || tcpPackage.Length <= 0 || tcpPackage.Content == null) return; switch (tcpPackage.FunCode) { case FuncCode.FID_OrderDealedNtf: LogInfoHelper.WriteInfo("收到单据成交通知"); //TODO:不解包暂时不需要 var orderComplete = _messageAdapter.ComplateNotice(tcpPackage.Content); //MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount, MessengerTokens.OrderNoticeToken); //发送更新委托通知 MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount.FundsAccountId, MessengerTokens.ShowMOOrdersByCapitalAccount); LogInfoHelper.WriteInfo("单据成交通知单号:" + orderComplete.OrderId); break; case FuncCode.FID_OrderCanceledNtf: LogInfoHelper.WriteInfo("委托单撤单通知"); //MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount, MessengerTokens.OrderNoticeToken); //发送更新委托通知 MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount.FundsAccountId, MessengerTokens.ShowMOOrdersByCapitalAccount); break; case FuncCode.FID_OrderSuccessedNtf: LogInfoHelper.WriteInfo("委托单成功通知-多点登录"); MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount.FundsAccountId, MessengerTokens.ShowMOOrdersByCapitalAccount); MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount.FundsAccountId, MessengerTokens.ShowMOTransactionByCapitalAccount); MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount.FundsAccountId, MessengerTokens.ShowMOHolderByCapitalAccount); MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount.FundsAccountId, MessengerTokens.ShowMOSPSLByCapitalAccount); if (LinkManager.Instance.QuoteTcpLinkProxy !=null) LinkManager.Instance.QuoteTcpLinkProxy.Subscribe(); break; case FuncCode.FID_TradeDealedNtf: LogInfoHelper.WriteInfo("交易成交通知"); //发送更新成交明细 MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount.FundsAccountId, MessengerTokens.ShowMOTransactionByCapitalAccount); break; //头寸变化通知 case FuncCode.FID_PosChangedNtf: LogInfoHelper.WriteInfo("头寸变化通知"); var orderPos = _messageAdapter.PosChangedReturnMessage(tcpPackage.Content); //MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount, MessengerTokens.OrderNoticeToken); //发送更新头寸通知 MessengerHelper.DefaultSend(orderPos.AccountID, MessengerTokens.ShowMOHolderByCapitalAccount); LogInfoHelper.WriteInfo("头寸变化通知商品ID号:" + orderPos.GoodsID); break; case FuncCode.FID_LogoutRsp: LogInfoHelper.WriteInfo("交易代理用户登出应答,离线!"); //手动发送登出消息 var service = SimpleIoc.Default.GetInstance(); if (service != null) service.TradeAccountLogout(UserManager.CurrentTradeAccount); var error= _messageAdapter.LogoutRspConvert(tcpPackage.Content); if (error.ReturnCode == 0) error.ReturnCode = 1009; MessengerHelper.DefaultSend(error, MessengerTokens.UserOfflineNoticeToken); break; case FuncCode.FID_MarketStatusChangeNtf: // 待开市通知则为结算下线 var ntfModel = _messageAdapter.MarketStatusChangeNtf(tcpPackage.Content); if (ntfModel.Status == 1) { var service1 = SimpleIoc.Default.GetInstance(); if (service1 != null) service1.TradeAccountLogout(UserManager.CurrentTradeAccount); //var error1 = _messageAdapter.LogoutRspConvert(tcpPackage.Content); //if (error1.ReturnCode == 0) error1.ReturnCode = 1009; MessengerHelper.DefaultSend(RestartType.SttleLoginOut, MessengerTokens.UserOfflineNoticesSttle); LogInfoHelper.WriteInfo("市场状态变更通知,离线(结算)!"); } break; case FuncCode.FID_MoneyChangedNtf: LogInfoHelper.WriteInfo("资金变化的通知"); var amountArg = _messageAdapter.ConvertAmountNotice(tcpPackage.Content); Task.Factory.StartNew(() => { MessengerHelper.DefaultSend(amountArg, MessengerTokens.MoneyNoticeToken); }); break; //// case FuncCode.FID_CustOfflineNtf: LogInfoHelper.WriteInfo("通信超时失效!"); MessengerHelper.DefaultSend(RestartType.LinkFail, MessengerTokens.UserOfflineNoticeToken); break; //公告消息推送 case FuncCode.FID_BltMsgNtf: var notifyBulletin = _messageAdapter.NotifyBulletin(tcpPackage.Content); MessengerHelper.DefaultSend(notifyBulletin, MessengerTokens.AnnouncementMessage); break; //case FuncCode.FID_RiskControlMsg: // var notifyControlMsg = _messageAdapter.RiskCtrlMemberNotice(tcpPackage.Content); // notifyControlMsg.RiskMsgType = RiskMsgType.StatusChange; // LogInfoHelper.WriteInfo("风控通知,账号:"+notifyControlMsg.LoginCode); // MessengerHelper.DefaultSend(notifyControlMsg, MessengerTokens.RiskControl); // break; //case FuncCode.FID_RiskCtrlMemRsp: // //暂时不处理 // var RiskCtrlMem = _messageAdapter.RiskCtrlMemberBreak(tcpPackage.Content); // RiskCtrlMem.RiskMsgType = RiskMsgType.BreakLevel; // RiskCtrlMem.RiskLevel = 3; ////服务器没有给级别,斩仓级别显示红色 // LogInfoHelper.WriteInfo("斩仓通知,账号:" + RiskCtrlMem.LoginCode); // MessengerHelper.DefaultSend(RiskCtrlMem, MessengerTokens.RiskControl); // break; //预埋单 //case FuncCode.FID_PrepostTransferOrderNtf: // var prepostTransfer = _messageAdapter.PrepostTransferOrderNtf(tcpPackage.Content); // //todo:更新预埋单 委托单 // //var cacheData = UserManager.GetTradeCacheData(UserManager.CurrentTradeAccount.AccountId); // //cacheData.LimitOrderFlag = false; // MessengerHelper.DefaultSend(true, MessengerTokens.PreOrderUpdate); // LogInfoHelper.WriteInfo("收到预埋单转委托通知!"); // break; ////投资者公告 //case FuncCode.FID_RiskCtrlBltRsp: // var riskCtrlBltNotice = _messageAdapter.RiskCtrlBltNotice(tcpPackage.Content); // MessengerHelper.DefaultSend(riskCtrlBltNotice, MessengerTokens.RiskControl); // break; //操作变更通知 //case FuncCode.FID_OperateChangeNotifyRsp: // var OperateChangeNotifyContent = _messageAdapter.OperateChangeNotifyAd(tcpPackage.Content); // AppMessengerHelper.OperateChangeNotifyMessage.Send(OperateChangeNotifyContent); // break; //开休市计划通知[Add By DK 20160629] //case FuncCode.FID_MarketStatusChangeNotify: // LogInfoHelper.WriteInfo("开休市计划通知,FunCode:" + FuncCode.FID_MarketStatusChangeNotify); // var MarketStatusChangeNotifyContent = _messageAdapter.MarketStatusChangeNotifyAd(tcpPackage.Content); // AppMessengerHelper.MarketStatusChangeNotifyMessage.Send(MarketStatusChangeNotifyContent); // break; //case FuncCode.FID_GoodsChangeNotifyRsp: // //todo:交易端暂时不区分单个商品内容的更新 // MessengerHelper.DefaultSend(string.Empty, MessengerTokens.UpdateCacheGoodsToken); // LogInfoHelper.WriteInfo("商品状态更新,FID_GoodsChangeNotifyRsp:" + FuncCode.FID_GoodsChangeNotifyRsp); // break; //case FuncCode.FID_AccountChangeNotifyRsp: // var OperateChangeNotifyContent = _messageAdapter.AccountChangeNotifyRspNotifyAd(tcpPackage.Content); // MessengerHelper.DefaultSend(string.Empty, MessengerTokens.AccountParamChangeToken); // break; //case FuncCode.FID_DeliveryAuditNtf: //交收审核通知 // var DeliveryAuditNtfContent = _messageAdapter.DeliveryReturnMessage(tcpPackage.Content); // LogInfoHelper.WriteInfo("交收审核通知,FID_DeliveryAuditNtf:" + FuncCode.FID_DeliveryAuditNtf + ", ReturnCode:" + DeliveryAuditNtfContent.RetCode); // MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount, MessengerTokens.OrderNoticeToken); // break; //case FuncCode.FID_OutStoreAuditNtf: //提货审核通知 // var OutStoreAuditNtfContent = _messageAdapter.OutStoreAuditReturnMessage(tcpPackage.Content); // LogInfoHelper.WriteInfo("提货审核通知,FID_OutStoreAuditNtf:" + FuncCode.FID_OutStoreAuditNtf + ", ReturnCode:" + OutStoreAuditNtfContent.RetCode); // MessengerHelper.DefaultSend(OutStoreAuditNtfContent, MessengerTokens.OutStoreAuditToken); // MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount, MessengerTokens.OrderNoticeToken); // break; //case FuncCode.FID_GoodsCloseDateNtf: //交割商品到期提货通知 // var GoodsCloseDateNtfContent = _messageAdapter.GoodsCloseDateReturnMessage(tcpPackage.Content); // LogInfoHelper.WriteInfo("交割商品到期提货通知,FID_GoodsCloseDateNtf:" + FuncCode.FID_GoodsCloseDateNtf + ", ReturnCode:" + GoodsCloseDateNtfContent.RetCode); // MessengerHelper.DefaultSend(GoodsCloseDateNtfContent, MessengerTokens.GoodsCloseDateToken); // break; //case FuncCode.FID_ListingBroadcastNtf: ////挂牌改价,数量改变,审核通知 // var listingNtfContent = _messageAdapter.ToListingNoticeModel(tcpPackage.Content); // LogInfoHelper.WriteInfo("挂牌改价,数量改变,审核通知,FID_ListingBroadcastNtf :" + FuncCode.FID_ListingBroadcastNtf + ", ReturnCode:" + listingNtfContent.RetCode); // MessengerHelper.DefaultSend(listingNtfContent.Entity, MessengerTokens.ListingNotice); // break; //case FuncCode.FID_ListingNtf: // var delistingNtf = _messageAdapter.ToListingNoticeModel(tcpPackage.Content); // LogInfoHelper.WriteInfo("摘牌成交,FID_ListingNtf :" + FuncCode.FID_ListingNtf + ", ReturnCode:" + delistingNtf.RetCode); // MessengerHelper.DefaultSend(delistingNtf.Entity, MessengerTokens.DelistingCompleteNotice); // break; ////会员风控通知 //case FuncCode.FID_RiskControlMember: // var RiskControlMemberContent = _messageAdapter.RiskControlMemberReturnMessage(tcpPackage.Content); // LogInfoHelper.WriteInfo("会员风控通知:" + FuncCode.FID_RiskControlMember + ", ReturnCode:" + RiskControlMemberContent.RetCode); // MessengerHelper.DefaultSend(RiskControlMemberContent, MessengerTokens.RiskControlMemberToken); // break; //投资者风控通知 case FuncCode.FID_RiskToWebNtf: var RiskControlInvestorContent = _messageAdapter.RiskControlInvestorReturnMessage(tcpPackage.Content); LogInfoHelper.WriteInfo("投资者风控通知:" + FuncCode.FID_RiskControlNtf + ", ReturnCode:" + RiskControlInvestorContent.RetCode); MessengerHelper.DefaultSend(RiskControlInvestorContent, MessengerTokens.RiskControlInvestorToken); break; ////通用通知 //case FuncCode.FID_CommonNotify: // var CommonNtfContent = _messageAdapter.CommonNotifyReturnMessage(tcpPackage.Content); // LogInfoHelper.WriteInfo("通用通知,通知标题:" + CommonNtfContent.Title + ", ReturnCode:" + CommonNtfContent.RetCode); // MessengerHelper.DefaultSend(CommonNtfContent, MessengerTokens.CommonMessageToken); // break; //case FuncCode.FID_CommonBroadcastNtf: ////通用广播通知 // var massgerfuncode = _messageAdapter.ToMessageHeadFunCode(tcpPackage.Content); // PackageBroadNtf((int) massgerfuncode, tcpPackage.Content); // break; } } /// /// 解通知具体内容 /// /// /// private void PackageBroadNtf(int funcode, byte[] buffer) { switch (funcode) { //case FuncCode.FID_MarketStatusRsp: // var massgerfuncode = _messageAdapter.ToMarketStatusRsp(buffer); // if (massgerfuncode.RetCode == 0) ////结算成功通知 更新单据 // { // Task.Factory.TryStartNew(() => // { // var random = new Random(); // var second = random.Next(500, 3000); // Thread.Sleep(second); // MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount, // MessengerTokens.OrderNoticeToken); // }); // } // break; //case FuncCode.FID_ListingBroadcastNtf: ////挂牌改价,数量改变,审核通知 // var listingNtfContent = _messageAdapter.ToListingNoticeModel(buffer); // LogInfoHelper.WriteInfo("挂牌改价,数量改变,审核通知,FID_ListingBroadcastNtf :" + FuncCode.FID_ListingBroadcastNtf + ", ReturnCode:" + listingNtfContent.RetCode); // MessengerHelper.DefaultSend(listingNtfContent.Entity, MessengerTokens.ListingNotice); // break; //case FuncCode.FID_ListingNtf: // var delistingNtf = _messageAdapter.ToListingNoticeModel(buffer); // LogInfoHelper.WriteInfo("摘牌成交,FID_ListingNtf :" + FuncCode.FID_ListingNtf + ", ReturnCode:" + delistingNtf.RetCode); // MessengerHelper.DefaultSend(delistingNtf.Entity, MessengerTokens.DelistingCompleteNotice); // break; default: break; } } } }