示例#1
0
        private void button1_Click(object sender, EventArgs e)
        {
            //初始化t2连接
            if (!InitT2())
            {
                return;
            }

            //打包请求报文
            CT2Packer packer  = new CT2Packer(2);
            sbyte     strType = Convert.ToSByte('S');
            sbyte     intType = Convert.ToSByte('I');

            packer.BeginPack();
            //插件编号
            packer.AddField("plugin_id", strType, 255, 4);
            //管理功能号
            packer.AddField("function_id", intType, 255, 4);
            packer.AddStr("com.hundsun.fbase.f2core");
            packer.AddInt(100);
            packer.EndPack();
            //功能号,业务包
            int iRet = conn.SendBiz(8, packer, 0, 0, 1);

            if (iRet < 0)
            {
                DisplayText(conn.GetErrorMsg(iRet));
            }
            else
            {
                string      error    = null;
                CT2UnPacker unpacker = null;
                iRet = conn.RecvBiz(iRet, out error, out unpacker, 4000, 0);
                if (iRet == 0 || iRet == 1)
                {
                    while (unpacker.IsEOF() != 1)
                    {
                        for (int i = 0; i < unpacker.GetColCount(); i++)
                        {
                            textBox1.Text += unpacker.GetStrByIndex(i) + " ";
                        }
                        textBox1.Text += "\r\n";
                        unpacker.Next();
                    }
                }
                else if (iRet < 0)
                {
                    DisplayText(conn.GetErrorMsg(iRet));
                }
                else if (iRet == 2)
                {
                    DisplayText("解包失败");
                }
            }

            packer.Dispose();
        }
示例#2
0
        public int SendAsync(CT2BizMessage message)
        {
            int iRet = _conn.SendBizMsg(message, 1);

            if (iRet < 0)
            {
                logger.Error(string.Format("异步发送数据失败! 错误码:{0}, 错误消息:{1}", iRet, _conn.GetErrorMsg(iRet)));
                return(iRet);
            }

            return(iRet);
        }
示例#3
0
        /// <summary>
        /// 通过调用UFX接口异步发送请求,调用前必须确保已经注册了功能号和回调代理,即调用Register函数注册。
        /// 同一功能号注册多次只会保留最后一次结果。
        /// </summary>
        /// <param name="message">CT2BizMessage对象的实例,包含用户信息,功能号,请求参数等信息</param>
        /// <returns>返回正值表示发送句柄,否则表示失败。</returns>
        public int SendAsync(CT2BizMessage message)
        {
            int hSend = _conn.SendBizMsg(message, (int)SendType.Async);

            if (hSend < 0)
            {
                string msg = string.Format("一般交易业务异步发送数据失败! 错误码:{0}, 错误消息:{1}", hSend, _conn.GetErrorMsg(hSend));
                logger.Error(msg);
                return(hSend);
            }

            return(hSend);
        }
示例#4
0
        public ConnectionCode Connect()
        {
            //新建连接
            _config = new CT2Configinterface();
            int iRet = _config.Load("config/t2sdk-subscriber.ini");

            if (iRet != 0)
            {
                string msg = "主推业务读取[config/t2sdk-subscriber.ini]连接配置对象失败!";
                logger.Error(msg);
                return(ConnectionCode.ErrorReadConf);
            }

            _conn = new CT2Connection(_config);

            //使用Create2BizMsg调用,则收到业务回调是OnReceivedBizMsg
            iRet = _conn.Create2BizMsg(this);
            if (iRet != 0)
            {
                string msg = string.Format("主推业务连接对象初始化失败: {0}, {1}", iRet, _conn.GetErrorMsg(iRet));
                logger.Error(msg);
                return(ConnectionCode.ErrorInitConn);
            }

            try
            {
                iRet = _conn.Connect(_timeOut);
            }
            catch (Exception e)
            {
                string msg = e.Message;
                logger.Error(msg);
            }
            if (iRet != 0)
            {
                string msg = string.Format("主推业务连接失败: {0}, {1}", iRet, _conn.GetErrorMsg(iRet));
                logger.Error(msg);

                return(ConnectionCode.ErrorConn);
            }
            else
            {
                string msg = "主推业务连接成功";
                logger.Info(msg);
            }

            _isInit = true;

            return(ConnectionCode.Success);
        }
示例#5
0
        public ConnectionCode Init()
        {
            ///新建连接
            //_config = new CT2Configinterface();
            //int iRet = _config.Load("config/t2sdk.ini");

            //if (iRet != 0)
            //{
            //    string msg = "读取连接配置对象失败!";
            //    logger.Error(msg);
            //    return ConnectionCode.ErrorReadConf;
            //}

            _conn = new CT2Connection(_config);
            T2SDKCallbackImpl callbackImpl = new T2SDKCallbackImpl(this);

            //使用Create2BizMsg调用,则收到业务回调是OnReceivedBizMsg
            int iRet = _conn.Create2BizMsg(callbackImpl);

            if (iRet != 0)
            {
                string msg = string.Format("连接对象初始化失败: {0}, {1}", iRet, _conn.GetErrorMsg(iRet));
                logger.Error(msg);
                return(ConnectionCode.ErrorInitConn);
            }

            try
            {
                iRet = _conn.Connect(_timeOut);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            if (iRet != 0)
            {
                string msg = string.Format("连接失败: {0}, {1}", iRet, _conn.GetErrorMsg(iRet));
                logger.Error(msg);
                return(ConnectionCode.ErrorConn);
            }
            else
            {
                string msg = "连接成功";
                logger.Info(msg);
            }

            _isInit = true;

            return(ConnectionCode.Success);
        }
示例#6
0
        //发送packer
        private int sendpacker(OptionFunctionCode functioncode, CT2Packer packer, bool IsAsync = true)
        {
            CT2BizMessage BizMessage = new CT2BizMessage();     //构造消息

            BizMessage.SetFunction((int)functioncode);          //设置功能号
            BizMessage.SetPacketType(0);                        //设置消息类型为请求

            unsafe
            {
                BizMessage.SetContent(packer.GetPackBuf(), packer.GetPackLen());
            }

            /************************************************************************/

            /* 此处使用异步发送 同步发送可以参考下面注释代码
             * connection.SendBizMsg(BizMessage, 0);
             * 1=异步,0=同步
             * /************************************************************************/
            int iRet = this.connMain.SendBizMsg(BizMessage, (IsAsync) ? 1 : 0);

            if (iRet < 0)
            {
                Debug.Print(string.Format("发送错误 错误码 {0} 错误信息 {1}", iRet, connMain.GetErrorMsg(iRet)));
            }
            packer.Dispose();
            BizMessage.Dispose();

            return(iRet);
        }
示例#7
0
        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);
            }
        }
示例#8
0
        private bool InitT2()
        {
            if (conn == null)
            {
                config = new CT2Configinterface();
                config.Load("t2sdk.ini");
                conn     = new CT2Connection(config);
                callback = new callbacktest(this);
                conn.Create(callback);
                int iret = conn.Connect(4000);
                textBox1.Text = "连接" + conn.GetErrorMsg(iret) + "\r\n" + textBox1.Text;
                if (iret != 0)
                {
                    return(false);
                }
            }

            return(true);
        }
示例#9
0
 public override void OnReceivedBiz(CT2Connection lpConnection, int hSend, String lppStr, CT2UnPacker lppUnPacker, int nResult)
 {
     if (nResult == 0 || nResult == 1)
     {
         while (lppUnPacker.IsEOF() != 1)
         {
             for (int i = 0; i < lppUnPacker.GetColCount(); i++)
             {
                 m_lpOwner.DisplayText(lppUnPacker.GetStrByIndex(i));
             }
             lppUnPacker.Next();
         }
     }
     else if (nResult < 0)
     {
         m_lpOwner.DisplayText(lpConnection.GetErrorMsg(nResult));
     }
     else if (nResult == 2)
     {
         m_lpOwner.DisplayText("解包失败");
     }
 }
示例#10
0
        public EntrustHunsun()
        {
            try
            {
                CT2Configinterface config = new CT2Configinterface();
                config.Load("t2sdk.ini");

                //连接主
                connMain = new CT2Connection(config);
                connMain.Create2BizMsg(this);
                int ret = connMain.Connect(5000);
                if (ret != 0)
                {
                    Debug.Print(string.Format("Main:连接{0}失败 错误号 {1} 错误信息 {2}", config.GetString("t2sdk", "servers", ""), ret, connMain.GetErrorMsg(ret)));
                }
                else
                {
                    Debug.Print((string.Format("Main:连接{0}成功", config.GetString("t2sdk", "servers", ""))));
                }

                //连接子
                connSub = new CT2Connection(config);
                connSub.Create(null);
                ret = connSub.Connect(5000);
                if (ret != 0)
                {
                    Debug.Print(string.Format("Sub:连接{0}失败 错误号 {1} 错误信息 {2}", config.GetString("t2sdk", "servers", ""), ret, connSub.GetErrorMsg(ret)));
                }
                else
                {
                    Debug.Print((string.Format("Sub:连接{0}成功", config.GetString("t2sdk", "servers", ""))));
                }
            }
            catch (Exception e)
            {
                Debug.Print(e.Message);
            }
        }
示例#11
0
        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();
        }
示例#12
0
        private int sendpacker(OptionFunction function, CT2Packer packer, bool IsAsync = true)
        {
            try
            {
                CT2BizMessage BizMessage = new CT2BizMessage();     //构造消息
                BizMessage.SetFunction((int)function);              //设置功能号
                BizMessage.SetPacketType(0);                        //设置消息类型为请求

                unsafe
                {
                    BizMessage.SetContent(packer.GetPackBuf(), packer.GetPackLen());
                }

                /************************************************************************/

                /* 此处使用异步发送 同步发送可以参考下面注释代码
                 * connection.SendBizMsg(BizMessage, 0);
                 * 1=异步,0=同步
                 * /************************************************************************/
                int iRet = this.connMain.SendBizMsg(BizMessage, (IsAsync) ? 1 : 0);
                if (iRet < 0)
                {
                    MessageManager.GetInstance().Add(MessageType.Error, string.Format("发送错误:{0}", connMain.GetErrorMsg(iRet)));
                }
                packer.Dispose();
                BizMessage.Dispose();

                return(iRet);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
示例#13
0
        public EntrustHundsun()
        {
            this.name   = "恒生UFX委托接口";
            this.Vendor = EntrustVendor.Hundsun;
            MessageManager.GetInstance().Add(MessageType.Warning, "对于x64的机器需要更换引用的ext_t2sdkEx.dll");

            try
            {
                CT2Configinterface config = new CT2Configinterface();
                config.Load(Config.GetInstance().GetParameter(Config.C_PATH_HUNDSUN_CONFIG).ToString());

                #region 打开连接
                //主连接
                connMain = new CT2Connection(config);
                connMain.Create2BizMsg(new UFXMainCallback());
                int ret = connMain.Connect(5000);
                if (ret != 0)
                {
                    MessageManager.GetInstance().Add(MessageType.Error, string.Format("连接{0}失败:main-{1}", config.GetString("t2sdk", "servers", ""), connMain.GetErrorMsg(ret)));
                    return;
                }

                //子连接
                connSub = new CT2Connection(config);
                connSub.Create(null);
                ret = connSub.Connect(5000);
                if (ret != 0)
                {
                    MessageManager.GetInstance().Add(MessageType.Error, string.Format("连接{0}失败:sub-{1}", config.GetString("t2sdk", "servers", ""), connSub.GetErrorMsg(ret)));
                }
                #endregion
            }
            catch (Exception ex)
            {
                MessageManager.GetInstance().Add(MessageType.Error, ex.Message + "请检查license.dat文件是否存在。");
                throw ex;
            }
        }