/**
         * @brief Creates socket connection to TWS/IBG.
         */
        public bool EConnect(string host, int port, int clientId, bool extraAuth)
        {
            if (IsConnected)
            {
                Wrapper.Error(IncomingMessage.NotValid, EClientErrors.AlreadyConnected.Code, EClientErrors.AlreadyConnected.Message);
                return(true);
            }

            try
            {
                TcpStream       = CreateClientStream(host, port);
                this.port       = port;
                SocketTransport = new ESocket(TcpStream);

                ClientId  = clientId;
                ExtraAuth = extraAuth;

                SendConnectRequest();

                if (!AsyncEConnect)
                {
                    var eReader = new EReader(this, eReaderSignal);
                    while (ServerVersion == 0 && eReader.PutMessageToQueue())
                    {
                        eReaderSignal.WaitForSignal();
                        eReader.ProcessMsgs();
                    }
                }

                return(true);
            }
            catch (ArgumentNullException ane)
            {
                Wrapper.Error(ane);
            }
            catch (SocketException se)
            {
                Wrapper.Error(se);
            }
            catch (EClientException e)
            {
                var cmp = e.Err;
                Wrapper.Error(-1, cmp.Code, cmp.Message);
            }
            catch (Exception e)
            {
                Wrapper.Error(e);
            }

            return(false);
        }
Beispiel #2
0
        public bool Connect()
        {
            logger.LogDebug("Connecting: {0}:{1} ({2})", config.Host, config.Port, config.ClientId);
            if (connected)
            {
                throw new InvalidOperationException("Already connected");
            }

            connected         = true;
            ibClient.ClientId = config.ClientId;

            if (!ibClient.ClientSocket.EConnect(config.Host, config.Port, config.ClientId))
            {
                logger.LogError("Failed connection");
                return(false);
            }

            var reader = new EReader(ibClient.ClientSocket, signal);

            reader.Start();

            processingThread = new Thread(() =>
            {
                while (ibClient?.ClientSocket.IsConnected == true)
                {
                    signal.WaitForSignal();
                    reader.ProcessMsgs();
                }
            })
            {
                IsBackground = true
            };

            processingThread.Name = "IB Processing Thread";
            processingThread.Start();
            return(true);
        }