Beispiel #1
0
        /// <summary>
        /// 创建事件参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="event"></param>
        /// <param name="requestID"></param>
        /// <param name="rspInfo"></param>
        protected CTPEventArgs <T> CreateEventArgs <T>(int requestID, CTPResponseInfo rspInfo) where T : struct
        {
            if (requestID == 0)
            {
                throw new Exception("Invalid Operation in CreateEventArgs");
            }


            //转换响应数据
            List <Object> rspDataList = this.responseDataMap[requestID];

            T value = default(T);

            if (rspDataList.Count > 0)
            {
                value = (T)(rspDataList[0]);
            }

            //创建事件参数
            CTPEventArgs <T> args = new CTPEventArgs <T>(value, rspInfo);

            args.RequestData = this.GetRequestData(requestID);

            return(args);
        }
Beispiel #2
0
        /// <summary>
        /// 创建事件参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="rspData"></param>
        /// <param name="rspInfo"></param>
        /// <returns></returns>
        protected CTPEventArgs <T> CreateEventArgs <T>(IntPtr rspData, CTPResponseInfo rspInfo)
        {
            //转换响应数据
            T value = default(T);

            if (rspData != IntPtr.Zero)
            {
                value = PInvokeUtility.GetObjectFromIntPtr <T>(rspData);
            }

            //创建事件参数
            CTPEventArgs <T> args = new CTPEventArgs <T>(value, rspInfo);

            return(args);
        }
Beispiel #3
0
        protected override void ProcessBusinessResponse(CTPResponseType responseType, IntPtr pData, CTPResponseInfo rspInfo, int requestID)
        {
            switch (responseType)
            {
                #region 当客户端与交易后台建立起通信连接时(还未登录前),该方法被调用。
            case CTPResponseType.FrontConnectedResponse:
            {
                this.isConnect = true;

                this.UserLogin();

                //调用事件
                OnEventHandler(CTPResponseType.FrontConnectedResponse, new CTPEventArgs());

                break;
            }
                #endregion

                #region 用户登录
            case CTPResponseType.UserLoginResponse:
            {
                CTPEventArgs <CThostFtdcRspUserLoginField> args = CreateEventArgs <CThostFtdcRspUserLoginField>(requestID, rspInfo);

                this.isLogin = true;

                this.OnEventHandler(CTPResponseType.UserLoginResponse, args);
            }
            break;
                #endregion


            case CTPResponseType.DepthMarketDataResponse:
            {
                if (this == null || this.isDispose == true)
                {
                    return;
                }

                CTPEventArgs <CThostFtdcDepthMarketDataField> args = CreateEventArgs <CThostFtdcDepthMarketDataField>(pData, rspInfo);

                OnEventHandler(CTPResponseType.DepthMarketDataResponse, args);

                break;
            }
            }
        }
Beispiel #4
0
        /// <summary>
        /// 创建事件参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="requestID"></param>
        /// <param name="rspInfo"></param>
        /// <returns></returns>
        protected CTPEventArgs <List <T> > CreateListEventArgs <T>(int requestID, CTPResponseInfo rspInfo) where T : struct
        {
            if (requestID == 0)
            {
                throw new Exception("Invalid Operation in PreProcessResponse");
            }

            List <T> list = new List <T>();

            //转换响应数据
            foreach (Object p in this.responseDataMap[requestID])
            {
                list.Add((T)p);
            }

            //创建事件参数
            CTPEventArgs <List <T> > args = new CTPEventArgs <List <T> >(list, rspInfo);

            args.RequestData = this.GetRequestData(requestID);

            return(args);
        }
Beispiel #5
0
        protected override void ProcessBusinessResponse(CTPResponseType responseType, IntPtr pData, CTPResponseInfo rspInfo, int requestID)
        {
            try
            {
                switch (responseType)
                {
                    #region 当客户端与交易后台建立起通信连接时(还未登录前),该方法被调用。
                case CTPResponseType.FrontConnectedResponse:
                {
                    this.isConnect = true;
                    if (this.isLogin == false)
                    {
                        if (this.canLogin)
                        {
                            this.canLogin = false;
                            this.UserLogin();
                            this.reconnectedCount++;
                            //if (this.reconnectedCount < this.reconnectMaxCount)
                            //{
                            //    this.canLogin = false;
                            //    this.UserLogin();
                            //    this.reconnectedCount++;
                            //}
                        }
                        else
                        {
                            //this.canLogin = true;
                            this.needLogin = true;
                        }
                    }

                    //调用事件
                    OnEventHandler(CTPResponseType.FrontConnectedResponse, new CTPEventArgs());

                    break;
                }
                    #endregion

                    #region 错误返回
                case CTPResponseType.ErrorResponse:
                {
                    CTPEventArgs obj = new CTPEventArgs();
                    obj.RequestID    = requestID;
                    obj.ResponseInfo = rspInfo;
                    //调用事件
                    OnEventHandler(CTPResponseType.ErrorResponse, obj);
                    break;
                }
                    #endregion

                    #region 连接断开返回
                case CTPResponseType.FrontDisconnectedResponse:
                {
                    this.isConnect = false;
                    this.isLogin   = false;
                    this.needLogin = false;
                    //this.canLogin = true;
                    CTPEventArgs obj = new CTPEventArgs();
                    obj.RequestID    = requestID;
                    obj.ResponseInfo = rspInfo;
                    //调用事件
                    OnEventHandler(CTPResponseType.FrontDisconnectedResponse, obj);
                    break;
                }
                    #endregion

                    #region 心跳警告返回
                case CTPResponseType.HeartBeatWarningResponse:
                {
                    CTPEventArgs obj = new CTPEventArgs();
                    obj.RequestID    = requestID;
                    obj.ResponseInfo = rspInfo;
                    //调用事件
                    OnEventHandler(CTPResponseType.HeartBeatWarningResponse, obj);
                    break;
                }
                    #endregion

                    #region 客户端认证响应
                case CTPResponseType.AuthenticateResponse:
                {
                    CTPEventArgs <CThostFtdcRspAuthenticateField> args = CreateEventArgs <CThostFtdcRspAuthenticateField>(requestID, rspInfo);

                    this.OnEventHandler(CTPResponseType.AuthenticateResponse, args);

                    break;
                }
                    #endregion

                    #region 登出返回
                case CTPResponseType.UserLogoutResponse:
                {
                    this.isLogin = false;
                    CTPEventArgs obj = new CTPEventArgs();
                    obj.RequestID    = requestID;
                    obj.ResponseInfo = rspInfo;
                    //调用事件
                    OnEventHandler(CTPResponseType.UserLogoutResponse, obj);
                    break;
                }
                    #endregion

                    #region 用户登录
                case CTPResponseType.UserLoginResponse:
                {
                    CTPEventArgs <CThostFtdcRspUserLoginField> args = CreateEventArgs <CThostFtdcRspUserLoginField>(pData, rspInfo);
                    CThostFtdcRspUserLoginField userLogin           = args.Value;


                    if (rspInfo.ErrorID == 0)
                    {
                        this.BrokerID         = userLogin.BrokerID;
                        this.FrontID          = userLogin.FrontID;
                        this.SessionID        = userLogin.SessionID;
                        this.isLogin          = true;
                        this.needLogin        = false;
                        this.canLogin         = true;
                        this.reconnectedCount = 0;
                        //this.loginTime = userLogin.LoginTime;
                    }
                    else
                    {
                        //this.canLogin = true;
                    }
                    this.OnEventHandler(CTPResponseType.UserLoginResponse, args);
                }
                break;
                    #endregion

                    #region 接收到行情数据返回
                case CTPResponseType.DepthMarketDataResponse:
                {
                    if (this == null || this.isDispose == true)
                    {
                        return;
                    }

                    CTPEventArgs <CThostFtdcDepthMarketDataField> args = CreateEventArgs <CThostFtdcDepthMarketDataField>(pData, rspInfo);

                    OnEventHandler(CTPResponseType.DepthMarketDataResponse, args);

                    break;
                }
                    #endregion

                default:
                    break;
                }
            }
            catch (Exception ex)
            {
                try
                {
                    CTPEventArgs obj = new CTPEventArgs();
                    obj.RequestID             = requestID;
                    obj.ResponseInfo          = rspInfo;
                    obj.ResponseInfo.Message += ex.StackTrace;
                    //调用事件
                    OnEventHandler(CTPResponseType.ErrorResponse, obj);
                }
                catch (Exception e)
                {
                }
            }
        }