diff --git a/TLSharp.Core/Network/TcpTransport.cs b/TLSharp.Core/Network/TcpTransport.cs index cfc7956..e5f26ab 100644 --- a/TLSharp.Core/Network/TcpTransport.cs +++ b/TLSharp.Core/Network/TcpTransport.cs @@ -12,23 +12,36 @@ namespace TLSharp.Core.Network public class TcpTransport : IDisposable { - private readonly TcpClient tcpClient; - private readonly NetworkStream stream; + private TcpClient tcpClient; + private NetworkStream stream; private int sendCounter = 0; + TcpClientConnectionHandler handler; + string address; + int port; + IPAddress ipAddress; public TcpTransport(string address, int port, TcpClientConnectionHandler handler = null) + { + this.handler = handler; + this.address = address; + this.port = port; + ipAddress = IPAddress.Parse(address); + } + + public async Task Connect() { if (handler == null) { - var ipAddress = IPAddress.Parse(address); - var endpoint = new IPEndPoint(ipAddress, port); - + if (tcpClient != null) + { + tcpClient.Close(); + } tcpClient = new TcpClient(ipAddress.AddressFamily); try { - tcpClient.Connect (endpoint); + await tcpClient.ConnectAsync(ipAddress, port); } catch (Exception ex) { - throw new Exception ($"Problem when trying to connect to {endpoint}; either there's no internet connection or the IP address version is not compatible (if the latter, consider using DataCenterIPVersion enum)", + throw new Exception ($"Problem when trying to connect to {ipAddress}:{port}; either there's no internet connection or the IP address version is not compatible (if the latter, consider using DataCenterIPVersion enum)", ex); } } @@ -102,7 +115,7 @@ namespace TLSharp.Core.Network { get { - return this.tcpClient.Connected; + return this.tcpClient != null && this.tcpClient.Connected; } } diff --git a/TLSharp.Core/TelegramClient.cs b/TLSharp.Core/TelegramClient.cs index 765ecbf..efc157a 100644 --- a/TLSharp.Core/TelegramClient.cs +++ b/TLSharp.Core/TelegramClient.cs @@ -72,6 +72,11 @@ namespace TLSharp.Core { token.ThrowIfCancellationRequested(); + if (!transport.IsConnected) + await transport.Connect(); + if (!transport.IsConnected) + throw new Exception("Connection to Telegram failed"); + if (session.AuthKey == null || reconnect) { var result = await Authenticator.DoAuthentication(transport, token).ConfigureAwait(false);