Wrapper around BinaryReader to parse IB Server response stream. This class is not thread-safe.
Ejemplo n.º 1
0
 public IbClientConnection()
 {
     ClientId      = -1;
     ExtraAuth     = false;
     ServerVersion = -1;
     IsConnected   = false;
     IbWriter      = null;
     IbReader      = null;
 }
Ejemplo n.º 2
0
        public void Connect(string host, int port, int clientId, bool extraAuth = false)
        {
            if (IsConnected)
            {
                throw new Exception(String.Format("Code: {0}, Msg: {1}", EClientErrors.AlreadyConnected.Code, EClientErrors.AlreadyConnected.Message));
            }

            tcpClient       = new TcpClient(host, port);
            tcpClientStream = tcpClient.GetStream();
            var tcpWriter = new BinaryWriter(tcpClientStream);

            IbWriter  = new IbWriter(tcpWriter);
            IbReader  = new IbReader(new BinaryReader(tcpClientStream));
            ClientId  = clientId;
            ExtraAuth = extraAuth;
            try
            {
                tcpWriter.Write(UTF8Encoding.UTF8.GetBytes(Constants.ClientVersion.ToString()));
                tcpWriter.Write(Constants.EOL);
            }
            catch (IOException e)
            {
                throw new IOException("Could not establish connection. Make sure the TWS is enabled to accept socket clients!", e);
            }
            // Receive the response from the remote device.
            ServerVersion = IbReader.ReadInt();
            if (ServerVersion < MinServerVer.MIN_VERSION)
            {
                throw new Exception(String.Format("Code: {0}, Msg: {1}", EClientErrors.UPDATE_TWS.Code, EClientErrors.UPDATE_TWS.Message));
            }
            if (ServerVersion >= 20)
            {
                string twsTime = IbReader.ReadString();
            }
            IsConnected = true;
            if (ServerVersion >= 3)
            {
                if (ServerVersion < MinServerVer.LINKING)
                {
                    tcpWriter.Write(UTF8Encoding.UTF8.GetBytes(clientId.ToString()));
                    tcpWriter.Write(Constants.EOL);
                }
                else if (!extraAuth)
                {
                    StartApi();
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Closes the TCP connection and cancels the task processing incoming messages.
        /// </summary>
        public void Disconnect()
        {
            if (!IsConnected)
            {
                throw new Exception(String.Format("Code: {0}, Msg: {1}", EClientErrors.NOT_CONNECTED.Code, EClientErrors.NOT_CONNECTED.Message));
            }

            IsConnected   = false;
            ServerVersion = 0;
            ClientId      = -1;
            ExtraAuth     = false;

            // close TCP connection
            if (tcpClient.Connected)
            {
                tcpClient.Close();
            }

            tcpClientStream.Close();
            tcpClientStream = null;
            tcpClient       = null;
            IbWriter        = null;
            IbReader        = null;
        }