Esempio n. 1
0
        /// <summary>
        /// 查询所有合约信息。
        /// </summary>
        /// <returns></returns>
        public List <InstrumentField> QueryInstument()
        {
            int requestID = m_requetSeqIDCreator.Next();

            try
            {
                List <InstrumentField> instrumentList = new List <InstrumentField>();
                USeResetEvent          queryEvent     = new USeResetEvent(requestID);
                queryEvent.Tag = instrumentList;
                m_eventDic.Add(queryEvent.EventID, queryEvent);

                QryInstrumentField qryField = new QryInstrumentField();
                m_ctpUser.ReqQryInstrument(ref qryField, requestID);
                if (queryEvent.WaitOne(m_queryTimeOut) == false)
                {
                    throw new Exception("Query time out.");
                }
                else
                {
                    if (queryEvent.IsError)
                    {
                        Debug.Assert(queryEvent.Tag != null);
                        RspInfoField rspInfo = (RspInfoField)queryEvent.Tag;
                        throw new Exception(string.Format("({0}){1}", rspInfo.ErrorID, rspInfo.ErrorMsg));
                    }
                    while (true)
                    {
                        if (queryEvent.IsFinish)
                        {
                            break;
                        }
                        // 继续等待应答结果
                        if (queryEvent.WaitOne(m_queryTimeOut) == false)
                        {
                            throw new Exception(string.Format("({0}){1}", "99", "查询超时"));
                        }
                    }
                }

                return(instrumentList);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                m_eventDic.Remove(requestID);
            }
        }
        /// <summary>
        /// 查询所有合约代码。
        /// </summary>
        /// <param name="error"></param>
        /// <returns></returns>
        private List <InstrumentField> QueryAllInstrumentFromCtp()
        {
            List <InstrumentField> instrumentFields = new List <InstrumentField>();
            int requestID = m_requetSeqIDCreator.Next();

            try
            {
                USeResetEvent queryEvent = new USeResetEvent(requestID);
                queryEvent.Tag = instrumentFields;
                m_eventDic.Add(requestID, queryEvent);

                QryInstrumentField qryField = new QryInstrumentField();
                m_ctpUser.ReqQryInstrument(ref qryField, requestID);

                while (true)
                {
                    if (queryEvent.IsError)
                    {
                        Debug.Assert(queryEvent.Tag != null);
                        RspInfoField rspInfo = (RspInfoField)queryEvent.Tag;
                        throw new Exception(string.Format("({0}){1}", rspInfo.ErrorID, rspInfo.ErrorMsg));
                    }

                    if (queryEvent.IsFinish)
                    {
                        break;
                    }

                    if (queryEvent.WaitOne(m_queryTimeOut) == false)
                    {
                        throw new Exception(string.Format("({0}){1}", "99", "time out."));
                    }
                }
            }
            catch (Exception ex)
            {
                m_logger.WriteError(string.Format("{0}.QueryAllInstrumentFromCtp() failed,Error:{1}.",
                                                  ToString(), ex.Message));
                throw new Exception(string.Format("Query instrument failed,Error:{0}.", ex.Message));
            }
            finally
            {
                m_eventDic.Remove(requestID);
            }

            return(instrumentFields);
        }