/// <summary> /// 执行 /// </summary> /// <param name="configDir">配置文件路径</param> /// <param name="serverIp"></param> /// <param name="port"></param> public void Init() { var address = ServerAddresses.Addresses.Single(); var match = Regex.Match(address, @"^.+:(\d+)$"); if (!match.Success) { LogFatal($"Can not parsing server port,address{address}"); throw new Exception("Can not parsing server port"); } int port = Int32.Parse(match.Groups[1].Value); LogInfo($"service listhen on port:{port}"); var serverIp = this.configuration["server"]; long apiInfoId = this.configuration["api"].ToLong(); if (string.IsNullOrWhiteSpace(serverIp) || port <= 0 || apiInfoId < 0) { throw new ApplicationException(string.Format("参数异常,ApiInfoId:{0},ServerIp:{1},Port:{2}", apiInfoId, serverIp, port)); } tradeConfig = _tradeConfigLoader.GetConfig <TradeConfig>(apiInfoId); if (tradeConfig == null) { throw new ApplicationException($"加载配置文件失败! ApiInfoId:{apiInfoId}"); } var dirName = tradeConfig.ApiInfoId + "#" + Process.GetCurrentProcess().Id; var logPath = Path.Combine(ConfigManager.GetAppSetting("logpath"), tradeConfig.BrokerType, dirName + "\\"); LogRecord.SetLogPath(logPath); LogInfo($"开始启动,BrokerType:{tradeConfig.BrokerType},ApiInfoId:{tradeConfig.ApiInfoId},CompCounter:{tradeConfig.CompCounter},ServerIp:{1},Port:{2},status:{tradeConfig.ServiceStatus}"); //延迟注册,防止监听未准备好就注册成功 var thReg = new Thread(RegApiAddr); thReg.Name = "RegApiAddr"; thReg.Start($"http://{serverIp}:{port}/"); LogInfo($"启动完成,BrokerType:{tradeConfig.BrokerType},ApiInfoId:{tradeConfig.ApiInfoId},CompCounter:{tradeConfig.CompCounter},ServerIp:{1},Port:{2},status:{tradeConfig.ServiceStatus}"); }
public static int Main(string[] args) { string jsonText = File.ReadAllText("TradeConfig.json"); tradeConfig = JsonConvert.DeserializeObject <TradeConfig>(jsonText); testImpl = new EWrapperImpl(); EClientSocket clientSocket = testImpl.ClientSocket; EReaderSignal readerSignal = testImpl.Signal; //! [connect] clientSocket.eConnect("127.0.0.1", 7497, 0); //! [connect] //! [ereader] //Create a reader to consume messages from the TWS. The EReader will consume the incoming messages and put them in a queue var reader = new EReader(clientSocket, readerSignal); reader.Start(); //Once the messages are in the queue, an additional thread can be created to fetch them new Thread(() => { while (clientSocket.IsConnected()) { readerSignal.waitForSignal(); reader.processMsgs(); } }) { IsBackground = true }.Start(); //! [ereader] /*************************************************************************************************************************************************/ /* One (although primitive) way of knowing if we can proceed is by monitoring the order's nextValidId reception which comes down automatically after connecting. */ /*************************************************************************************************************************************************/ while (testImpl.NextOrderId <= 0) { } testIBMethods(clientSocket, testImpl.NextOrderId); Console.WriteLine("Disconnecting..."); clientSocket.eDisconnect(); return(0); }