/// <summary> /// Attempts to initiate this connection. /// </summary> /// <returns>Task which completes when the connection is made.</returns> public async Task Connect() { if (IsConnected) { throw new InvalidOperationException("Already connected."); } if (RemoteEndPoint == null) { throw new InvalidOperationException("Address and port have not been specified."); } if (IsConnecting) { _connectionEvent.WaitOne(); return; } IsConnecting = true; try { await _client.ConnectAsync(RemoteEndPoint.Address, RemoteEndPoint.Port); } finally { IsConnecting = false; _connectionEvent.Set(); } Stream = new RateLimitedStream(_client.GetStream()); IsConnecting = false; _connectionEvent.Set(); }
/// <summary> /// Initializes a new instance of the <see cref="TcpTransportStream"/> class. /// </summary> /// <param name="client">Existing connection.</param> public TcpTransportStream(TcpClient client) { _client = client; Stream = new RateLimitedStream(client.GetStream()); RemoteEndPoint = (IPEndPoint)client.Client.RemoteEndPoint; }