Example #1
0
        void LoadServers()
        {
            try
            {
                var servers = from c in XElement.Load(serversFile).Elements("Server")
                              select c;

                foreach (var server in servers)
                {
                    ServerItem si = new ServerItem();
                    si.Label           = server.Attribute("Label").Value;
                    si.BrokerID        = server.Attribute("BrokerID").Value;
                    si.UserProductInfo = server.Attribute("UserProductInfo").Value;
                    si.AuthCode        = server.Attribute("AuthCode").Value;

                    string[] tdarr = server.Attribute("Trading").Value.Split(';');
                    foreach (string s in tdarr)
                    {
                        if (!string.IsNullOrEmpty(s))
                        {
                            si.Trading.Add(s);
                        }
                    }

                    string[] mdarr = server.Attribute("MarketData").Value.Split(';');
                    foreach (string s in mdarr)
                    {
                        if (!string.IsNullOrEmpty(s))
                        {
                            si.MarketData.Add(s);
                        }
                    }

                    serversList.Add(si);
                }
            }
            catch (Exception)
            {
            }
        }
        void LoadServers()
        {
            try
            {
                var servers = from c in XElement.Load(serversFile).Elements("Server")
                          select c;

                foreach (var server in servers)
                {
                    ServerItem si = new ServerItem();
                    si.Label = server.Attribute("Label").Value;
                    si.BrokerID = server.Attribute("BrokerID").Value;
                    si.UserProductInfo = server.Attribute("UserProductInfo").Value;
                    si.AuthCode = server.Attribute("AuthCode").Value;

                    string[] tdarr = server.Attribute("Trading").Value.Split(';');
                    foreach (string s in tdarr)
                    {
                        if (!string.IsNullOrEmpty(s))
                            si.Trading.Add(s);
                    }

                    string[] mdarr = server.Attribute("MarketData").Value.Split(';');
                    foreach (string s in mdarr)
                    {
                        if (!string.IsNullOrEmpty(s))
                            si.MarketData.Add(s);
                    }

                    serversList.Add(si);
                }
            }
            catch (Exception)
            {
            }
        }
        private void _Connect()
        {
            CTPZQAPI.GetInstance().__RegInstrumentDictionary(_dictInstruments);
            CTPZQAPI.GetInstance().__RegInstrumentCommissionRateDictionary(_dictCommissionRate);
            //CTPZQAPI.GetInstance().__RegInstrumentMarginRateDictionary(_dictMarginRate);
            CTPZQAPI.GetInstance().__RegDepthMarketDataDictionary(_dictDepthMarketData);

            server = null;
            account = null;

            bool bCheckOk = false;

            do
            {
                if (0 == serversList.Count)
                {
                    MessageBox.Show("您还没有设置 服务器 信息,目前只选择第一条进行连接");
                    break;
                }
                if (0 == serversList.Count)
                {
                    MessageBox.Show("您还没有设置 账号 信息,目前只选择第一条进行连接");
                    break;
                }

                server = serversList[0];
                account = accountsList[0];

                if (string.IsNullOrEmpty(server.BrokerID))
                {
                    MessageBox.Show("BrokerID不能为空");
                    break;
                }

                if (_bWantTdConnect &&0 == server.Trading.Count())
                {
                    MessageBox.Show("交易服务器地址不全");
                    break;
                }

                if (_bWantMdConnect &&0 == server.MarketData.Count())
                {
                    MessageBox.Show("行情服务器信息不全");
                    break;
                }

                if (string.IsNullOrEmpty(account.InvestorId)
                || string.IsNullOrEmpty(account.Password))
                {
                    MessageBox.Show("账号信息不全");
                    break;
                }

                bCheckOk = true;

            } while (false);

            if (false == bCheckOk)
            {
                ChangeStatus(ProviderStatus.Disconnected);
                EmitDisconnectedEvent();
                return;
            }

            //新建目录
            _newTempPath = ApiTempPath + Path.DirectorySeparatorChar + server.BrokerID + Path.DirectorySeparatorChar + account.InvestorId;
            Directory.CreateDirectory(_newTempPath);

            ChangeStatus(ProviderStatus.Connecting);
            //如果前面一次连接一直连不上,新改地址后也会没响应,所以先删除
            Disconnect_MD();
            Disconnect_TD();

            Connect_MsgQueue();
            if (_bWantMdConnect)
            {
                Connect_MD();
            }
            if (_bWantTdConnect)
            {
                Connect_TD();
            }

            if (_bWantMdConnect||_bWantTdConnect)
            {
                //建立消息队列读取线程
                if (null == _thread)
                {
                    _runThread = true;
                    _thread = new Thread(new ThreadStart(ThreadProc));
                    _thread.Start();
                }
            }
        }