private static async Task<IClient> ConnectInternal(ConnectionParams connectionParams, CancellationToken cancellationToken, Dispatcher dispatcher, CancellationTokenSource apiCancellationTokenSource) { try { var stream = await SetupConnection(connectionParams.HostName, connectionParams.Port); var fieldsStream = new FieldsStream(stream); var serializer = new IBSerializer(); await Handshake(connectionParams.ClientId, fieldsStream, serializer, cancellationToken); var connection = new Connection.Connection(fieldsStream, serializer); var factory = new ApiObjectsFactory(connection, new IdsDispenser(connection), dispatcher, apiCancellationTokenSource); var waitForMarketConnected = factory.CreateWaitForMarketConnectedOperation(cancellationToken); var waitForAccountsList = factory.CreateReceiveManagedAccountsListOperation(cancellationToken); connection.ReadMessagesAndDispatch(); await waitForMarketConnected; var accountStorage = await factory.CreateAccountStorageOperation(await waitForAccountsList, cancellationToken); return factory.CreateClient(accountStorage); } catch { apiCancellationTokenSource.Cancel(); throw; } }
private static async Task Handshake(int clientId, FieldsStream stream, IIbSerializer clientSerializer, CancellationToken cancellationToken) { Trace.TraceInformation("Handshaking"); await clientSerializer.Write(new VersionMessage {Version = 63}, stream, cancellationToken); await clientSerializer.ReadMessageWithoutId<AcknowledgementMessage>(stream, cancellationToken); await clientSerializer.ReadMessageWithoutId<TimeMessage>(stream, cancellationToken); await clientSerializer.Write(new StartApiMessage {ClientId = clientId, Version = 1}, stream, cancellationToken); Trace.TraceInformation("Handshake done"); }
public async Task TestDeserializationWithIBDateTimeNull() { var stream = new MemoryStream(); var fieldsStream = new FieldsStream(stream); var message = new MessageWithIBDateTime {Field = null}; await this.serializer.Write(message, fieldsStream, CancellationToken.None); stream.Seek(0, SeekOrigin.Begin); var result = await this.serializer.ReadClientMessage(fieldsStream, CancellationToken.None); Assert.AreEqual(message, result); }
public void TestSerializationWithIBDateTimeNull() { var stream = new MemoryStream(); var fieldsStream = new FieldsStream(stream); var message = new MessageWithIBDateTime {Field = null}; this.serializer.Write(message, fieldsStream, CancellationToken.None); var result = new byte[6]; stream.Seek(0, SeekOrigin.Begin); stream.Read(result, 0, result.Length); Assert.AreEqual(result.Length, stream.Length); var expected = Encoding.ASCII.GetBytes( 1009.ToString() + char.MinValue + char.MinValue); Assert.IsTrue(expected.SequenceEqual(result)); }