From e8ba470357145a50099924abb2940d3abea7b16b Mon Sep 17 00:00:00 2001 From: solarin Date: Sun, 5 Apr 2020 16:20:09 +0400 Subject: [PATCH 1/2] IsConnected now includes a check on sender==null. Otherwise callls to this method always return True and if(!TelegramClient.IsConnected) TelegramClient.ConnectAsync() wouldn't have the expected result. --- TLSharp.Core/TelegramClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TLSharp.Core/TelegramClient.cs b/TLSharp.Core/TelegramClient.cs index 46e0ad42..a8ead0b4 100644 --- a/TLSharp.Core/TelegramClient.cs +++ b/TLSharp.Core/TelegramClient.cs @@ -436,7 +436,7 @@ public bool IsConnected { get { - if (transport == null) + if (transport == null || sender == null) return false; return transport.IsConnected; } From 47cc3405cf249724f283942f1e15cc505c71d839 Mon Sep 17 00:00:00 2001 From: solarin Date: Wed, 8 Apr 2020 22:54:40 +0400 Subject: [PATCH 2/2] ! IsConnected is restored as it was before + TcpTransport has got a connect method which properly initializes the underlying tcpclient --- TLSharp.Core/Network/TcpTransport.cs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/TLSharp.Core/Network/TcpTransport.cs b/TLSharp.Core/Network/TcpTransport.cs index 3750a769..cdbae7bb 100644 --- a/TLSharp.Core/Network/TcpTransport.cs +++ b/TLSharp.Core/Network/TcpTransport.cs @@ -10,19 +10,28 @@ 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); + tcpClient = new TcpClient(ipAddress.AddressFamily); + } + + public async Task Connect() { if (handler == null) { - var ipAddress = IPAddress.Parse(address); - var endpoint = new IPEndPoint(ipAddress, port); - - tcpClient = new TcpClient(ipAddress.AddressFamily); - tcpClient.Connect(endpoint); + await tcpClient.ConnectAsync(ipAddress, port); } else tcpClient = handler(address, port); @@ -31,8 +40,9 @@ public TcpTransport(string address, int port, TcpClientConnectionHandler handler { stream = tcpClient.GetStream(); } + else + stream = null; } - public async Task Send(byte[] packet, CancellationToken token = default(CancellationToken)) { if (!tcpClient.Connected)