private void Init() { Status = TWSClientStatus.Unknown; // Create buffered read stream to minimize the # of recv system calls _enc = new TWSEncoding(_stream); _stillConnected = true; _marketDataSubscriptions = new Dictionary <int, IBContract>(); _marketDepthSubscriptions = new Dictionary <int, IBContract>(); }
private void Init() { Status = TWSClientStatus.Unknown; // Create buffered read stream to minimize the # of recv system calls _enc = new TWSEncoding(_stream); _stillConnected = true; _marketDataSubscriptions = new Dictionary<int, IBContract>(); _marketDepthSubscriptions = new Dictionary<int, IBContract>(); }
private static DateTime? DecodeIBExpiry(ITWSEncoding enc) { var v = enc.DecodeString(); return String.IsNullOrEmpty(v) ? (DateTime?) null : DateTime.ParseExact(v, IB_EXPIRY_DATE_FORMAT, CultureInfo.InvariantCulture); }
/// <summary> /// Connect to the specified IB Trader Workstation Endpoint /// </summary> /// <param name="clientId">The client id to use when connecting</param> public void Connect(int clientId) { lock (this) { if (IsConnected) { OnError(TWSErrors.ALREADY_CONNECTED); return; } try { _tcpClient = new TcpClient(); _tcpClient.NoDelay = true; _tcpClient.Connect(_endPoint); lock (_socketLock) { if (RecordForPlayback) { if (_recordStream == null) _recordStream = SetupDefaultRecordStream(); _enc = new TWSPlaybackRecorderEncoding(new BufferedReadStream(_tcpClient.GetStream()), _recordStream); } else _enc = new TWSEncoding(new BufferedReadStream(_tcpClient.GetStream())); _enc.Encode(ClientInfo); _enc.Flush(); _doWork = true; // Only create a reader thread if this Feed IS NOT reconnecting if (!_reconnect) { _thread = new Thread(ProcessMessages) { IsBackground = true, Name = "IBReader" }; } // Get the server version ServerInfo = _enc.DecodeServerInfo(); if (ServerInfo.Version >= 20) { _twsTime = _enc.DecodeString(); } if (ServerInfo.Version < TWSServerInfo.SERVER_VERSION) { Disconnect(); throw new Exception("Server version is too low, please update the TWS server"); } // Send the client id if (ServerInfo.Version >= 3) { if (clientId == -1) { if (_tcpClient.Client.LocalEndPoint is IPEndPoint) { var p = _tcpClient.Client.LocalEndPoint as IPEndPoint; byte[] ab = p.Address.GetAddressBytes(); clientId = ab[ab.Length - 1] << 16 | p.Port; } else clientId = new Random().Next(); } var id = new TWSClientId(clientId); _enc.Encode(id); } } // Only start the thread if this Feed IS NOT reconnecting if (!_reconnect) _thread.Start(); _clientId = clientId; OnStatusChanged(Status = TWSClientStatus.Connected); } catch (Exception e) { Disconnect(); throw; } } }
public TWSLoopbackServer(Stream stream, ITWSEncoding encoding) : base(stream) { _enc = encoding; }
/// <summary> /// Connect to the specified IB Trader Workstation Endpoint /// </summary> /// <param name="clientId">The client id to use when connecting</param> public void Connect(int clientId) { lock (this) { if (IsConnected) { OnError(TWSErrors.ALREADY_CONNECTED); return; } try { _tcpClient = new TcpClient(); _tcpClient.Connect(_endPoint); _tcpClient.NoDelay = true; if (RecordForPlayback) { if (_recordStream == null) _recordStream = SetupDefaultRecordStream(); _enc = new TWSPlaybackEncoding(new BufferedReadStream(_tcpClient.GetStream()), _recordStream); } else _enc = new TWSEncoding(new BufferedReadStream(_tcpClient.GetStream())); _enc.Encode(ClientInfo); _doWork = true; // Only create a reader thread if this Feed IS NOT reconnecting if (!_reconnect) { _thread = new Thread(new ThreadStart(ProcessMessages)); _thread.Name = "IB Reader"; } // Get the server version ServerInfo = _enc.DecodeServerInfo(); if (ServerInfo.Version >= 20) { _twsTime = _enc.DecodeString(); } // Send the client id if (ServerInfo.Version >= 3) { if (clientId == -1) { if (_tcpClient.Client.LocalEndPoint is IPEndPoint) { IPEndPoint p = _tcpClient.Client.LocalEndPoint as IPEndPoint; byte[] ab = p.Address.GetAddressBytes(); clientId = ab[ab.Length - 1] << 16 | p.Port; } else clientId = new Random().Next(); } TWSClientId id = new TWSClientId(clientId); _enc.Encode(id); } // Only start the thread if this Feed IS NOT reconnecting if (!_reconnect) _thread.Start(); _clientId = clientId; OnStatusChanged(Status = TWSClientStatus.Connected); } catch (Exception e) { OnError(e.Message); OnError(TWSErrors.CONNECT_FAIL); } } }