DFITCOrderCommRtnField GetOrder(int spdOrderID)
        {
            lock (this.orderList)
            {
                DFITCOrderCommRtnField order = (from item in this.orderList
                                                where item.SpdOrderID == spdOrderID
                                                select item).FirstOrDefault();

                return(order);
            }
        }
        static string GetOrderUniqueKey(object field)
        {
            //报单
            if (field is DFITCOrderCommRtnField)
            {
                DFITCOrderCommRtnField value = (DFITCOrderCommRtnField)field;
                return(string.Format("{0}_{1}", value.AccountID, value.SpdOrderID));
            }

            //报单
            //if (field is DFITCOrderRspDataRtnField)
            //{
            //  DFITCOrderRspDataRtnField value = (DFITCOrderRspDataRtnField)field;
            //  return string.Format("{0}_{1}", this.AccountID, value.SpdOrderID);
            //}

            throw new Exception("Unknow type : " + field.GetType().ToString());
        }
        protected override void ProcessBusinessResponse(XSpeedResponseType responseType, IntPtr pData, XSpeedResponseInfo rspInfo, int requestID)
        {
            Trace.WriteLine(string.Format("{0} [{1}]:{2},{3},{4}", this.wallTimeStopwatch.ElapsedMilliseconds, this.UserKey, "ProcessBusinessResponse", responseType, requestID));

            switch (responseType)
            {
                #region 当客户端与交易后台建立起通信连接时(还未登录前),该方法被调用。
            case XSpeedResponseType.FrontConnectedResponse:
            {
                this.isConnect = true;

                this.UserLogin();

                //调用事件
                OnEventHandler(XSpeedResponseType.FrontConnectedResponse, new XSpeedEventArgs());

                break;
            }
                #endregion

                #region 通信中断
            case XSpeedResponseType.FrontDisconnectedResponse:
                ///  当客户端与交易后台通信连接断开时,该方法被调用。当发生这个情况后,API会自动重新连接,客户端可不做处理。
                ///@param nReason 错误原因
                ///        0x1001 网络读失败
                ///        0x1002 网络写失败
                ///        0x2001 接收心跳超时
                ///        0x2002 发送心跳失败
                ///        0x2003 收到错误报文
            {
                this.isConnect = false;

                //调用事件
                OnEventHandler(XSpeedResponseType.FrontDisconnectedResponse, new XSpeedEventArgs());
            }
            break;
                #endregion

                #region 用户登录
            case XSpeedResponseType.UserLoginResponse:
            {
                XSpeedEventArgs <DFITCUserLoginInfoRtnField> args = CreateEventArgs <DFITCUserLoginInfoRtnField>(requestID, rspInfo);

                DFITCUserLoginInfoRtnField userLogin = args.Value;


                if (rspInfo.ErrorID == 0)
                {
                    //this.BrokerID = userLogin.BrokerID;
                    //this.FrontID = userLogin.FrontID;
                    this.SessionID = userLogin.SessionID;
                    this.isLogin   = true;

                    //最大报单引用
                    this.orderRef = userLogin.InitLocalOrderID;

                    //DateTime.TryParseExact(userLogin.TradingDay, "yyyyMMdd", null, System.Globalization.DateTimeStyles.AllowWhiteSpaces, out this.tradingDate);
                    //TimeSpan.TryParse(userLogin.DCETime, out this.loginTimeDCE);
                    //TimeSpan.TryParse(userLogin.CZCETime, out this.loginTimeCZCE);
                    //TimeSpan.TryParse(userLogin.SHFETime, out this.loginTimeSHFE);

                    this.wallTimeStopwatch.Start();
                    this.timer.Change(0, 1000);
                }

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

                #region 登出请求响应
            case XSpeedResponseType.UserLogoutResponse:
            {
                this.isLogin = false;

                //调用事件
                OnEventHandler(XSpeedResponseType.UserLogoutResponse, new XSpeedEventArgs());
                break;
            }
                #endregion

                #region 报单录入请求响应
            case XSpeedResponseType.InsertOrderResponse:
            {
                XSpeedEventArgs <DFITCOrderRspDataRtnField> args = CreateEventArgs <DFITCOrderRspDataRtnField>(pData, rspInfo);


                DFITCOrderCommRtnField order = GetOrder(args.Value.SpdOrderID);

                if (order.SpdOrderID == 0)
                {
                    //
                }
                else
                {
                    order.OrderStatus = args.Value.OrderStatus;
                }

                //调用事件
                OnEventHandler(XSpeedResponseType.InsertOrderResponse, args);

                break;
            }
                #endregion

                #region 改单响应
            case XSpeedResponseType.CancelOrderResponse:
            {
                XSpeedEventArgs <DFITCCancelOrderField> args = CreateEventArgs <DFITCCancelOrderField>(pData, rspInfo);

                //调用事件
                OnEventHandler(XSpeedResponseType.CancelOrderResponse, args);

                break;
            }
                #endregion

                #region 查询报单响应
            case XSpeedResponseType.QueryOrderInfoResponse:
            {
                XSpeedEventArgs <List <DFITCOrderCommRtnField> > args = CreateListEventArgs <DFITCOrderCommRtnField>(requestID, rspInfo);

                lock (this.OrderList)
                {
                    this.OrderList.Clear();
                    this.OrderList.AddRange(args.Value);
                }

                //调用事件
                OnEventHandler(XSpeedResponseType.QueryOrderInfoResponse, args);

                break;
            }
                #endregion

                #region 查询成交单响应
            case XSpeedResponseType.QueryMatchInfoResponse:
            {
                XSpeedEventArgs <List <DFITCMatchedRtnField> > args = CreateListEventArgs <DFITCMatchedRtnField>(requestID, rspInfo);

                this.TradeList.Clear();
                this.TradeList.AddRange(args.Value);

                //调用事件
                OnEventHandler(XSpeedResponseType.QueryMatchInfoResponse, args);

                break;
            }
                #endregion

                #region 查询投资者持仓响应
            case XSpeedResponseType.QueryPositionResponse:
            {
                XSpeedEventArgs <List <DFITCPositionInfoRtnField> > args = CreateListEventArgs <DFITCPositionInfoRtnField>(requestID, rspInfo);

                this.PositionList.Clear();
                this.PositionList.AddRange(args.Value);

                //调用事件
                OnEventHandler(XSpeedResponseType.QueryPositionResponse, args);
                break;
            }
                #endregion

                #region 查询资金账户响应
            case XSpeedResponseType.CustomerCapitalResponse:
            {
                /// 查询资金账户响应
                XSpeedEventArgs <DFITCCapitalInfoRtnField> args = CreateEventArgs <DFITCCapitalInfoRtnField>(requestID, rspInfo);

                this.tradingAccount = args.Value;

                this.OnEventHandler(XSpeedResponseType.CustomerCapitalResponse, args);

                break;
            }
                #endregion

                #region 查询标准合约响应
            case XSpeedResponseType.QuerySpecifyInstrumentResponse:
            {
                XSpeedEventArgs <DFITCInstrumentRtnField> args = CreateEventArgs <DFITCInstrumentRtnField>(pData, rspInfo);

                //this.SetInstrumentRate(args.Value);

                //调用事件
                OnEventHandler(XSpeedResponseType.QuerySpecifyInstrumentResponse, args);

                break;
            }
                #endregion

                #region 查询合约响应
            case XSpeedResponseType.QueryExchangeInstrumentResponse:
            {
                XSpeedEventArgs <List <DFITCExchangeInstrumentRtnField> > values = CreateListEventArgs <DFITCExchangeInstrumentRtnField>(requestID, rspInfo);

                foreach (var instrument in values.Value)
                {
                    //CTPInstrument ctpInstrument = new CTPInstrument();
                    //ctpInstrument.SetNativeValue(instrument);

                    ////加入到市场列表
                    //if (this.Exchanges.ContainsKey(ctpInstrument.ExchangeID))
                    //{
                    //  this.Exchanges[instrument.ExchangeID].Instruments.Add(ctpInstrument);
                    //}

                    //if (this.SymbolProducts.ContainsKey(ctpInstrument.ProductID.ToUpper()) == false)
                    //{
                    //  this.SymbolProducts.Add(ctpInstrument.ProductID.ToUpper(), new CTPSymbolProduct(instrument));
                    //}

                    //加入到合约表
                    //if (this.InstrumentDictionary.ContainsKey(ctpInstrument.ID) == false)
                    //{
                    //  this.InstrumentDictionary.Add(ctpInstrument.ID, ctpInstrument);
                    //}
                }

                //创建新的事件参数
                //List<CTPInstrument> list = new List<CTPInstrument>(this.InstrumentDictionary.Values);
                //XSpeedEventArgs<List<CTPInstrument>> args = new XSpeedEventArgs<List<CTPInstrument>>(list);

                //调用事件
                OnEventHandler(XSpeedResponseType.QueryExchangeInstrumentResponse, values);

                break;
            }
                #endregion

                #region 查询套利合约响应
            case XSpeedResponseType.ArbitrageInstrumentResponse:
            {
                XSpeedEventArgs <List <DFITCAbiInstrumentRtnField> > values = CreateListEventArgs <DFITCAbiInstrumentRtnField>(requestID, rspInfo);

                //调用事件
                OnEventHandler(XSpeedResponseType.ArbitrageInstrumentResponse, values);

                break;
            }
                #endregion

                #region 错误回报
            case XSpeedResponseType.ReturnErrorMsgResponse:
            {
                this.OnEventHandler(XSpeedResponseType.ReturnErrorMsgResponse, new XSpeedEventArgs(rspInfo));
                break;
            }
                #endregion

                #region 报单回报
            case XSpeedResponseType.ReturnOrderResponse:
            {
                XSpeedEventArgs <DFITCOrderRtnField> args = CreateEventArgs <DFITCOrderRtnField>(pData, rspInfo);

                DFITCOrderCommRtnField order = GetOrder(args.Value.SpdOrderID);

                if (order.SpdOrderID == 0)
                {
                    //
                }
                else
                {
                    order.OrderStatus = args.Value.OrderStatus;
                }

                //调用事件
                OnEventHandler(XSpeedResponseType.ReturnOrderResponse, args);

                break;
            }
                #endregion

                #region 成交回报
            case XSpeedResponseType.ReturnMatchedInfoResponse:
            {
                XSpeedEventArgs <DFITCMatchedRtnField> args = CreateEventArgs <DFITCMatchedRtnField>(pData, rspInfo);

                //插入到列表中
                AppendOrReplaceOrder(this.tradeList, args.Value);

                //调用事件
                OnEventHandler(XSpeedResponseType.ReturnMatchedInfoResponse, args);

                break;
            }
                #endregion

            default:
                Debug.Assert(true, responseType.ToString());
                break;
            }
        }