public override void OnReceived(CT2SubscribeInterface lpSub, int subscribeIndex, void* lpData, int nLength, tagSubscribeRecvData lpRecvData) { //收到主推数据 - 开始 if (lpRecvData.iAppDataLen > 0) { unsafe { //附加数据 string strInfo = string.Format("{0}", Marshal.PtrToStringAuto(new IntPtr(lpRecvData.lpAppData))); } } //过滤字段部分 if (lpRecvData.iFilterDataLen > 0) { CT2UnPacker lpUnpacker = new CT2UnPacker(lpRecvData.lpFilterData, (uint)lpRecvData.iFilterDataLen); //解包 //..... lpUnpacker.Dispose(); } CT2UnPacker lpUnpacker1 = new CT2UnPacker((void*)lpData, (uint)nLength); if(lpUnpacker1 != null) { //解包 //.... lpUnpacker1.Dispose(); } //收到主推数据 - 结束 }
//回报 public override void OnReceived(CT2SubscribeInterface lpSub, int subscribeIndex, void *lpData, int nLength, tagSubscribeRecvData lpRecvData) { try { Debug.Print("/*********************************收到主推数据 begin***************************/"); string strInfo = string.Format("附加数据长度: {0}", lpRecvData.iAppDataLen); Debug.Print(strInfo); if (lpRecvData.iAppDataLen > 0) { unsafe { strInfo = string.Format("附加数据: {0}", Marshal.PtrToStringAuto(new IntPtr(lpRecvData.lpAppData))); Debug.Print(strInfo); } } Debug.Print("过滤字段部分:\n"); if (lpRecvData.iFilterDataLen > 0) { CT2UnPacker lpUnpack = new CT2UnPacker(lpRecvData.lpFilterData, (uint)lpRecvData.iFilterDataLen); //ufxengine.ShowUnPacker(lpUnpack); lpUnpack.Dispose(); } CT2UnPacker lpUnPack1 = new CT2UnPacker((void *)lpData, (uint)nLength); if (lpUnPack1 != null) { //ufxengine.ShowUnPacker(lpUnPack1); lpUnPack1.Dispose(); } Debug.Print("/*********************************收到主推数据 end ***************************/"); } catch (System.Exception ex) { throw ex; } }
public override void OnReceived(CT2SubscribeInterface lpSub, int subscribeIndex, void *lpData, int nLength, tagSubscribeRecvData lpRecvData) { //收到主推数据 - 开始 if (lpRecvData.iAppDataLen > 0) { unsafe { //附加数据 string strInfo = string.Format("{0}", Marshal.PtrToStringAuto(new IntPtr(lpRecvData.lpAppData))); } } UFXPushMessageType messageType = UFXPushMessageType.None; //过滤字段部分 if (lpRecvData.iFilterDataLen > 0) { CT2UnPacker lpUnpacker = new CT2UnPacker(lpRecvData.lpFilterData, (uint)lpRecvData.iFilterDataLen); //解包 //..... DataParser parser = new DataParser(); parser.Parse(lpUnpacker); Log("====推送=====过滤字段部分=====开始"); //parser.Output(); Log(parser); Log("====推送=====过滤字段部分=====结束"); lpUnpacker.Dispose(); messageType = _filterBLL.GetMessageType(parser); } if (nLength > 0) { CT2UnPacker lpUnpacker1 = new CT2UnPacker((void *)lpData, (uint)nLength); if (lpUnpacker1 != null) { //解包 //.... DataParser parser = new DataParser(); parser.Parse(lpUnpacker1); Log("====推送*****数据部分=====开始"); //parser.Output(); Log(parser); Log("====推送*****数据部分=====结束"); lpUnpacker1.Dispose(); IUFXMessageHandlerBase subscriberHandler = _handlerFactory.Create(messageType); if (subscriberHandler != null) { TaskFactory taskFactory = new TaskFactory(_taskScheduler); taskFactory.StartNew(() => subscriberHandler.Handle(parser)); } } } //收到主推数据 - 结束 }
public override void OnReceivedBizMsg(CT2Connection lpConnection, int hSend, CT2BizMessage lpMsg) { int iRetCode = lpMsg.GetErrorNo(); //获取返回码 int iErrorCode = lpMsg.GetReturnCode(); //获取错误码 int iFunction = lpMsg.GetFunction(); if (iRetCode != 0) { Debug.Print("异步接收出错:" + lpMsg.GetErrorNo().ToString() + lpMsg.GetErrorInfo()); } else { if (iFunction == 620000)//1.0消息中心心跳 { Debug.Print("收到心跳!==>" + iFunction); lpMsg.ChangeReq2AnsMessage(); connMain.SendBizMsg(lpMsg, 1); return; } else if (iFunction == 620003 || iFunction == 620025) //收到发布过来的行情 { Debug.Print("收到主推消息!==>" + iFunction); int iKeyInfo = 0; void * lpKeyInfo = lpMsg.GetKeyInfo(&iKeyInfo); CT2UnPacker unPacker = new CT2UnPacker(lpKeyInfo, (uint)iKeyInfo); //this.ShowUnPacker(unPacker); unPacker.Dispose(); } else if (iFunction == 620001) { Debug.Print("收到订阅应答!==>"); return; } else if (iFunction == 620002) { Debug.Print("收到取消订阅应答!==>"); return; } CT2UnPacker unpacker = null; unsafe { int iLen = 0; void *lpdata = lpMsg.GetContent(&iLen); unpacker = new CT2UnPacker(lpdata, (uint)iLen); } if (iFunction == 10001) { int code = unpacker.GetInt("ErrCode"); if (code == 0) { unpacker.SetCurrentDatasetByIndex(1); token = unpacker.GetStr("user_token"); } } //this.ShowUnPacker(unpacker); } }
public ConnectionCode Subscribe(LoginUser user, IUFXMessageHandlerFactory handlerFactory) { callback = new T2SubCallback(handlerFactory); subcribe = _conn.NewSubscriber(callback, SubscribeName, (int)_timeOut, 2000, 100); if (subcribe == null) { string msg = string.Format("主推业务订阅创建失败: {0}", _conn.GetMCLastError()); logger.Error(msg); return(ConnectionCode.ErrorFailSubscribe); } CT2SubscribeParamInterface args = new CT2SubscribeParamInterface(); args.SetTopicName("ufx_topic"); args.SetReplace(false); args.SetFilter("operator_no", user.Operator); CT2Packer req = new CT2Packer(2); req.BeginPack(); req.AddField("login_operator_no", Convert.ToSByte('S'), 16, 4); req.AddField("password", Convert.ToSByte('S'), 16, 4); req.AddStr(user.Operator); req.AddStr(user.Password); req.EndPack(); CT2UnPacker unpacker = null; int ret = subcribe.SubscribeTopicEx(args, 50000, out unpacker, req); req.Dispose(); //根据文档说明,返回值大于0表示有效的订阅标识,否则其他表示错误。 if (ret > 0) { string msg = string.Format("主推业务订阅创建成功, 返回码: {0}, 消息: {1}", ret, _conn.GetErrorMsg(ret)); logger.Info(msg); return(ConnectionCode.SuccessSubscribe); } else { string outMsg = string.Empty; if (unpacker != null) { //Show(back); DataParser parser = new DataParser(); parser.Parse(unpacker); unpacker.Dispose(); var errResponse = T2ErrorHandler.Handle(parser); outMsg = errResponse.MessageDetail; } string msg = string.Format("主推业务订阅创建失败,返回码: {0}, 消息: {1}, 返回数据包消息: {2}", ret, _conn.GetErrorMsg(ret), outMsg); logger.Error(msg); return(ConnectionCode.ErrorFailSubscribe); } }
public override void OnReceivedBizMsg(CT2Connection lpConnection, int hSend, CT2BizMessage lpMsg) { logger.Info("OnReceivedBizMsg: 接收业务数据!"); //获取返回码 int iRetCode = lpMsg.GetReturnCode(); //获取错误码 int iErrorCode = lpMsg.GetErrorNo(); int iFunction = lpMsg.GetFunction(); if (iRetCode != 0) { logger.Error("异步接收数据出错:" + lpMsg.GetErrorNo().ToString() + lpMsg.GetErrorInfo()); } else { CT2UnPacker unpacker = null; unsafe { int iLen = 0; void *lpdata = lpMsg.GetContent(&iLen); unpacker = new CT2UnPacker(lpdata, (uint)iLen); } switch (iFunction) { case (int)FunctionCode.QueryTradingInstance: { } break; case (int)FunctionCode.EntrustInstanceBasket: break; case (int)FunctionCode.QueryEntrustInstance: break; case (int)FunctionCode.QueryDealInstance: break; default: break; } unpacker.Dispose(); } lpMsg.Dispose(); }
public override void OnReceivedBizMsg(CT2Connection lpConnection, int hSend, CT2BizMessage lpMsg) { logger.Info("OnReceivedBizMsg: 接收业务数据!"); //获取返回码 int iRetCode = lpMsg.GetReturnCode(); //获取错误码 int iErrorCode = lpMsg.GetErrorNo(); int iFunction = lpMsg.GetFunction(); if (iRetCode != 0) { logger.Error("异步接收数据出错:" + lpMsg.GetErrorNo().ToString() + lpMsg.GetErrorInfo()); } else { CT2UnPacker unpacker = null; unsafe { int iLen = 0; void *lpdata = lpMsg.GetContent(&iLen); unpacker = new CT2UnPacker(lpdata, (uint)iLen); } switch (iFunction) { case (int)FunctionCode.Login: { var token = unpacker.GetStr("user_token"); if (string.IsNullOrEmpty(token)) { LoginManager.Instance.LoginUser.Token = token; } } break; case (int)FunctionCode.Logout: break; default: break; } PrintUnPack(unpacker); unpacker.Dispose(); } lpMsg.Dispose(); }
public ConnectionCode ReceivedBizMsg(int hSend, FunctionCode functionCode) { CT2BizMessage bizMessage = null; int retCode = _conn.RecvBizMsg(hSend, out bizMessage, (int)_timeOut, 1); if (retCode < 0) { logger.Error("同步接收出错: " + _conn.GetErrorMsg(retCode)); return(ConnectionCode.ErrorConn); } int iRetCode = bizMessage.GetReturnCode(); int iErrorCode = bizMessage.GetErrorNo(); if (iRetCode != 0) { string msg = string.Format("同步接收数据出错: {0}, {1}", iErrorCode, bizMessage.GetErrorInfo()); return(ConnectionCode.ErrorConn); } CT2UnPacker unpacker = null; unsafe { int iLen = 0; void *lpdata = bizMessage.GetContent(&iLen); unpacker = new CT2UnPacker(lpdata, (uint)iLen); } if (unpacker != null) { PrintUnPack(unpacker); switch (functionCode) { case FunctionCode.Login: { var token = unpacker.GetStr("user_token"); if (!string.IsNullOrEmpty(token)) { LoginManager.Instance.LoginUser.Token = token; } else { return(ConnectionCode.ErrorLogin); } } break; case FunctionCode.Logout: break; case FunctionCode.HeartBeat: break; default: break; } unpacker.Dispose(); } //bizMessage.Dispose(); return(ConnectionCode.Success); }
public void SendFunctionAction(int functionId, string functionIdGenerationStr, Action <ShowList> callback) { CT2Esbmsg t2Message = new CT2Esbmsg(); //构造消息 t2Message.GetItem(CT2tag_def.TAG_FUNCTION_ID).SetInt(functionId, 0); //设置功能号 t2Message.GetItem(CT2tag_def.TAG_PACKET_TYPE).SetInt(0, 0);; //设置消息类型为请求 //打包请求报文 CT2Packer packer = new CT2Packer(3); sbyte strType = Convert.ToSByte('S'); sbyte intType = Convert.ToSByte('I'); sbyte charType = Convert.ToSByte('C'); packer.BeginPack(); //插件编号 //管理功能号 foreach (var item in _sendFunctionDtoList) { if (item.Type == "int") { packer.AddField(item.Name, intType, 255, 4); } else if (item.Type == "string") { packer.AddField(item.Name, strType, 255, 4); } } foreach (var item in _sendFunctionDtoList) { if (item.Type == "int") { packer.AddInt(Convert.ToInt32(item.Value)); } else if (item.Type == "string") { packer.AddStr(item.Value); } } packer.EndPack(); unsafe { t2Message.GetItem(CT2tag_def.TAG_MESSAGE_BODY).SetRawData(packer.GetPackBuf(), packer.GetPackLen()); int iMsgLen = 0; void *lpData = t2Message.GetBuffer(&iMsgLen); int iRet = Conn.Send(lpData, iMsgLen, 0); if (iRet < 0) { MessageBox.Show(Conn.GetErrorMsg(iRet)); } else { void *lpRecvData = null; int iRecvLen = 0; iRet = Conn.Receive(iRet, &lpRecvData, &iRecvLen, 5000, 0); if (iRet == 0) { CT2Esbmsg ansMessage = new CT2Esbmsg(); //构造消息 int iParseError = ansMessage.SetBuffer(lpRecvData, iRecvLen); //解析消息 if (iParseError != 0) { MessageBox.Show("同步接收业务错误:解析消息失败\n"); } else { int iRetCode = ansMessage.GetItem(CT2tag_def.TAG_RETURN_CODE).GetInt(0); //获取返回码 int iErrorCode = ansMessage.GetItem(CT2tag_def.TAG_ERROR_NO).GetInt(0); //获取错误码 if (iErrorCode != 0) { MessageBox.Show("同步接收出错:" + ansMessage.GetItem(CT2tag_def.TAG_ERROR_NO).GetString(0) + ansMessage.GetItem(CT2tag_def.TAG_ERROR_INFO).GetString(0)); } else { CT2UnPacker unpacker = null; unsafe { int iLen = 0; void *lpdata = ansMessage.GetItem(CT2tag_def.TAG_MESSAGE_BODY).GetRawData(&iLen, 0); unpacker = new CT2UnPacker(lpdata, (uint)iLen); } //返回业务错误 if (iRetCode != 0) { MessageBox.Show("同步接收业务出错:\n"); UnPack(unpacker); } //正常业务返回 else { UnPack(unpacker); callback?.Invoke(_showlist); } unpacker.Dispose(); } } ansMessage.Dispose(); } else { Conn.GetErrorMsg(iRet); } } } t2Message.Dispose(); packer.Dispose(); }
public int OnReceivedBizMsg(CT2BizMessage bizMessage) { int iRetCode = bizMessage.GetReturnCode(); int iErrorCode = bizMessage.GetErrorNo(); int iFunction = bizMessage.GetFunction(); if (iRetCode != 0) { string msg = string.Format("异步接收数据出错: {0}, {1}", iErrorCode, bizMessage.GetErrorInfo()); return(iRetCode); } CT2UnPacker unpacker = null; unsafe { int iLen = 0; void *lpdata = bizMessage.GetContent(&iLen); unpacker = new CT2UnPacker(lpdata, (uint)iLen); } if (unpacker != null) { Console.WriteLine("功能号:" + iFunction); _t2SDKWrap.PrintUnPack(unpacker); switch ((FunctionCode)iFunction) { case FunctionCode.Entrust: { } break; case FunctionCode.Withdraw: { } break; case FunctionCode.EntrustBasket: { } break; case FunctionCode.WithdrawBasket: { } break; case FunctionCode.QuerySecurityEntrust: { } break; default: break; } unpacker.Dispose(); } //bizMessage.Dispose(); return((int)ConnectionCode.Success); }
private int HandleReceivedBizMsg(SendType sendType, UFXFunctionCode functionCode, int hSend, CT2BizMessage bizMessage) { int iRetCode = bizMessage.GetReturnCode(); int iErrorCode = bizMessage.GetErrorNo(); int iFunction = bizMessage.GetFunction(); //FunctionCode functionCode = (FunctionCode)iFunction; if (iRetCode != 0) { string msg = string.Format("同步接收数据出错: {0}, {1}", iErrorCode, bizMessage.GetErrorInfo()); logger.Error(msg); return(iRetCode); } CT2UnPacker unpacker = null; unsafe { int iLen = 0; void *lpdata = bizMessage.GetContent(&iLen); unpacker = new CT2UnPacker(lpdata, (uint)iLen); } int ret = (int)ConnectionCode.ErrorConn; if (unpacker == null) { ret = (int)ConnectionCode.ErrorFailContent; string msg = string.Format("提交UFX请求回调中,功能号[{0}]数据获取失败!", iFunction); logger.Error(msg); return(ret); } DataParser parser = new DataParser(); parser.FunctionCode = functionCode; parser.Parse(unpacker); unpacker.Dispose(); //parser.Output(); if (!_dataHandlerMap.ContainsKey(functionCode)) { ret = (int)ConnectionCode.ErrorNoCallback; string msg = string.Format("提交UFX请求时,未注册功能号[{0}]的回调方法!", iFunction); logger.Error(msg); return(ret); } var dataHandler = _dataHandlerMap[functionCode]; if (dataHandler == null) { ret = (int)ConnectionCode.ErrorNoCallback; string msg = string.Format("提交UFX请求时,未注册功能号[{0}]的回调方法!", iFunction); logger.Error(msg); return(ret); } if (sendType == SendType.Sync) { return(dataHandler(functionCode, hSend, parser)); } else { //TODO: control the maximum number of the thread? //Task task = new Task(() => dataHandler(parser)); //task.Start(); //use the TaskScheduler to limit the maximum thread number TaskFactory taskFactory = new TaskFactory(_taskScheduler); taskFactory.StartNew(() => dataHandler(functionCode, hSend, parser)); return((int)ConnectionCode.Success); } }
/// <summary> /// 通过调用UFX接口同步发送请求,并将结果封装在DataParser对象中返回。 /// </summary> /// <param name="message">CT2BizMessage对象的实例,包含用户信息,功能号,请求参数等信息</param> /// <returns>DataParser对象实例,包含错误代码和最终数据。</returns> public DataParser SendSync2(CT2BizMessage message) { DataParser dataParser = new DataParser(); int iRet = _conn.SendBizMsg(message, (int)SendType.Sync); if (iRet < 0) { string msg = string.Format("一般交易业务同步发送数据失败! 错误码:{0}, 错误消息:{1}", iRet, _conn.GetErrorMsg(iRet)); logger.Error(msg); dataParser.ErrorCode = ConnectionCode.ErrorSendMsg; return(dataParser); } CT2BizMessage bizMessage = null; int retCode = _conn.RecvBizMsg(iRet, out bizMessage, (int)_timeOut, 1); if (retCode < 0) { string msg = "一般交易业务同步接收出错: " + _conn.GetErrorMsg(retCode); logger.Error(msg); dataParser.ErrorCode = ConnectionCode.ErrorRecvMsg; return(dataParser); } int iFunction = bizMessage.GetFunction(); if (!Enum.IsDefined(typeof(UFXFunctionCode), iFunction)) { dataParser.ErrorCode = ConnectionCode.ErrorNoFunctionCode; return(dataParser); } dataParser.FunctionCode = (UFXFunctionCode)iFunction; int iRetCode = bizMessage.GetReturnCode(); int iErrorCode = bizMessage.GetErrorNo(); UFXFunctionCode functionCode = (UFXFunctionCode)iFunction; if (iRetCode != 0) { string msg = string.Format("同步接收数据出错: {0}, {1}", iErrorCode, bizMessage.GetErrorInfo()); //Console.WriteLine(msg); logger.Error(msg); dataParser.ErrorCode = ConnectionCode.ErrorRecvMsg; return(dataParser); } CT2UnPacker unpacker = null; unsafe { int iLen = 0; void *lpdata = bizMessage.GetContent(&iLen); unpacker = new CT2UnPacker(lpdata, (uint)iLen); } if (unpacker == null) { string msg = string.Format("提交UFX请求回调中,功能号[{0}]数据获取失败!", iFunction); logger.Error(msg); dataParser.ErrorCode = ConnectionCode.ErrorFailContent; } else { dataParser.Parse(unpacker); unpacker.Dispose(); dataParser.ErrorCode = ConnectionCode.Success; } return(dataParser); }
public int OnReceivedBizMsg(CT2BizMessage bizMessage) { int iRetCode = bizMessage.GetReturnCode(); int iErrorCode = bizMessage.GetErrorNo(); int iFunction = bizMessage.GetFunction(); if (iRetCode != 0) { string msg = string.Format("同步接收数据出错: {0}, {1}", iErrorCode, bizMessage.GetErrorInfo()); Console.WriteLine(msg); return(iRetCode); } CT2UnPacker unpacker = null; unsafe { int iLen = 0; void *lpdata = bizMessage.GetContent(&iLen); unpacker = new CT2UnPacker(lpdata, (uint)iLen); } if (unpacker != null) { Console.WriteLine("功能号:" + iFunction); //_t2SDKWrap.PrintUnPack(unpacker); //_t2SDKWrap.PrintUnPack(unpacker); DataParser parser = new DataParser(); parser.Parse(unpacker); parser.Output(); switch ((FunctionCode)iFunction) { case FunctionCode.Login: { var token = unpacker.GetStr("user_token"); if (!string.IsNullOrEmpty(token)) { LoginManager.Instance.LoginUser.Token = token; } else { return((int)ConnectionCode.ErrorLogin); } } break; case FunctionCode.Logout: break; case FunctionCode.HeartBeat: break; case FunctionCode.QuerymemoryData: break; case FunctionCode.QueryAccount: { if (_dataHandlerMap.ContainsKey(FunctionCode.QueryAccount)) { _dataHandlerMap[FunctionCode.QueryAccount](parser); } } break; case FunctionCode.QueryAssetUnit: break; case FunctionCode.QueryPortfolio: break; case FunctionCode.QueryHolder: break; default: break; } unpacker.Dispose(); } //bizMessage.Dispose(); return((int)ConnectionCode.Success); }