From 2bcae5bd9b4fdb20b0e655b5b187c5bfa176b15a Mon Sep 17 00:00:00 2001 From: solarin Date: Wed, 8 Apr 2020 23:20:54 +0400 Subject: [PATCH 1/8] fixed not to conflict ! IsConnected is restored as it was before + TcpTransport has got a connect method which properly initializes the underlying tcpclient --- TLSharp.Core/Network/TcpTransport.cs | 29 ++++++++++++++++++++-------- TLSharp.Core/TelegramClient.cs | 5 +++++ 2 files changed, 26 insertions(+), 8 deletions(-) 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); From 5778366b297d543194e870b2b8fb31bfb446d37c Mon Sep 17 00:00:00 2001 From: solarin Date: Thu, 9 Apr 2020 04:47:41 +0400 Subject: [PATCH 2/8] the connection was re-estabilished after a "connection forcibly closed by the server" but the following communication was not succesfully because the session contained dirty information as well as the sendCounter in TcpTransport. this commit fixes this situation, so a connection can be succesfully re-enstabilished and the communication restarted. --- TLSharp.Core/Network/TcpTransport.cs | 4 +++- TLSharp.Core/TelegramClient.cs | 13 ++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/TLSharp.Core/Network/TcpTransport.cs b/TLSharp.Core/Network/TcpTransport.cs index e5f26ab..0adfd31 100644 --- a/TLSharp.Core/Network/TcpTransport.cs +++ b/TLSharp.Core/Network/TcpTransport.cs @@ -37,8 +37,10 @@ namespace TLSharp.Core.Network tcpClient.Close(); } tcpClient = new TcpClient(ipAddress.AddressFamily); + sendCounter = 0; - try { + try + { await tcpClient.ConnectAsync(ipAddress, port); } catch (Exception ex) { 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)", diff --git a/TLSharp.Core/TelegramClient.cs b/TLSharp.Core/TelegramClient.cs index efc157a..494a281 100644 --- a/TLSharp.Core/TelegramClient.cs +++ b/TLSharp.Core/TelegramClient.cs @@ -32,6 +32,8 @@ namespace TLSharp.Core private List dcOptions; private TcpClientConnectionHandler handler; private DataCenterIPVersion dcIpVersion; + private ISessionStore store; + string sessionUserId; public Session Session { @@ -56,15 +58,15 @@ namespace TLSharp.Core if (string.IsNullOrEmpty(apiHash)) throw new MissingApiConfigurationException("API_HASH"); - if (store == null) - store = new FileSessionStore(); + this.store = store ?? new FileSessionStore(); + this.sessionUserId = sessionUserId; this.apiHash = apiHash; this.apiId = apiId; this.handler = handler; this.dcIpVersion = dcIpVersion; - session = Session.TryLoadOrCreateNew(store, sessionUserId); + session = Session.TryLoadOrCreateNew(this.store, sessionUserId); transport = new TcpTransport (session.DataCenter.Address, session.DataCenter.Port, this.handler); } @@ -73,7 +75,12 @@ namespace TLSharp.Core token.ThrowIfCancellationRequested(); if (!transport.IsConnected) + { + // we must recreate the session because it might track dirty information + // of a connection that maybe was disconnected, reusing that session will cause errors + session = Session.TryLoadOrCreateNew(store, sessionUserId); await transport.Connect(); + } if (!transport.IsConnected) throw new Exception("Connection to Telegram failed"); From afd6d06252902e576ba29c7228aa607057b92dc2 Mon Sep 17 00:00:00 2001 From: solarin Date: Thu, 9 Apr 2020 12:30:10 +0400 Subject: [PATCH 3/8] minor stylish changes --- TLSharp.Core/Network/TcpTransport.cs | 6 +++--- TLSharp.Core/TelegramClient.cs | 2 -- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/TLSharp.Core/Network/TcpTransport.cs b/TLSharp.Core/Network/TcpTransport.cs index 0adfd31..36542f6 100644 --- a/TLSharp.Core/Network/TcpTransport.cs +++ b/TLSharp.Core/Network/TcpTransport.cs @@ -16,9 +16,9 @@ namespace TLSharp.Core.Network private NetworkStream stream; private int sendCounter = 0; TcpClientConnectionHandler handler; - string address; - int port; - IPAddress ipAddress; + readonly string address; + readonly int port; + readonly IPAddress ipAddress; public TcpTransport(string address, int port, TcpClientConnectionHandler handler = null) { diff --git a/TLSharp.Core/TelegramClient.cs b/TLSharp.Core/TelegramClient.cs index 494a281..d67a593 100644 --- a/TLSharp.Core/TelegramClient.cs +++ b/TLSharp.Core/TelegramClient.cs @@ -81,8 +81,6 @@ namespace TLSharp.Core session = Session.TryLoadOrCreateNew(store, sessionUserId); await transport.Connect(); } - if (!transport.IsConnected) - throw new Exception("Connection to Telegram failed"); if (session.AuthKey == null || reconnect) { From 0dd6fdbac70437e6b2e763458b52b374a1d997ca Mon Sep 17 00:00:00 2001 From: solarin Date: Thu, 9 Apr 2020 12:32:06 +0400 Subject: [PATCH 4/8] minor changes --- TLSharp.Core/Network/TcpTransport.cs | 2 +- TLSharp.Core/TelegramClient.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/TLSharp.Core/Network/TcpTransport.cs b/TLSharp.Core/Network/TcpTransport.cs index 36542f6..b2c08c1 100644 --- a/TLSharp.Core/Network/TcpTransport.cs +++ b/TLSharp.Core/Network/TcpTransport.cs @@ -28,7 +28,7 @@ namespace TLSharp.Core.Network ipAddress = IPAddress.Parse(address); } - public async Task Connect() + public async Task ConnectAsync() { if (handler == null) { diff --git a/TLSharp.Core/TelegramClient.cs b/TLSharp.Core/TelegramClient.cs index d67a593..5a83bd8 100644 --- a/TLSharp.Core/TelegramClient.cs +++ b/TLSharp.Core/TelegramClient.cs @@ -79,7 +79,7 @@ namespace TLSharp.Core // we must recreate the session because it might track dirty information // of a connection that maybe was disconnected, reusing that session will cause errors session = Session.TryLoadOrCreateNew(store, sessionUserId); - await transport.Connect(); + await transport.ConnectAsync(); } if (session.AuthKey == null || reconnect) From f697dac02c0d622adc2bf1e6a9320fe794dd0620 Mon Sep 17 00:00:00 2001 From: solarin Date: Thu, 9 Apr 2020 16:50:46 +0400 Subject: [PATCH 5/8] initialization fix --- TLSharp.Core/Network/TcpTransport.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TLSharp.Core/Network/TcpTransport.cs b/TLSharp.Core/Network/TcpTransport.cs index b2c08c1..2f7c2df 100644 --- a/TLSharp.Core/Network/TcpTransport.cs +++ b/TLSharp.Core/Network/TcpTransport.cs @@ -37,7 +37,6 @@ namespace TLSharp.Core.Network tcpClient.Close(); } tcpClient = new TcpClient(ipAddress.AddressFamily); - sendCounter = 0; try { @@ -54,6 +53,7 @@ namespace TLSharp.Core.Network { stream = tcpClient.GetStream(); } + sendCounter = 0; } public async Task Send(byte[] packet, CancellationToken token = default(CancellationToken)) From bc87d0aea7ece0172bd65d41486f962888ac3b18 Mon Sep 17 00:00:00 2001 From: solarin Date: Thu, 9 Apr 2020 16:52:48 +0400 Subject: [PATCH 6/8] minor fix --- TLSharp.Core/Network/TcpTransport.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/TLSharp.Core/Network/TcpTransport.cs b/TLSharp.Core/Network/TcpTransport.cs index 2f7c2df..60c86d2 100644 --- a/TLSharp.Core/Network/TcpTransport.cs +++ b/TLSharp.Core/Network/TcpTransport.cs @@ -49,11 +49,12 @@ namespace TLSharp.Core.Network else tcpClient = handler(address, port); + sendCounter = 0; + if (tcpClient.Connected) { stream = tcpClient.GetStream(); } - sendCounter = 0; } public async Task Send(byte[] packet, CancellationToken token = default(CancellationToken)) From a266b896d25d13e164f4a7dfb7703e24270e4648 Mon Sep 17 00:00:00 2001 From: solarin Date: Sat, 11 Apr 2020 18:59:59 +0400 Subject: [PATCH 7/8] mediaonly filter reverted tcptransport --- TLSharp.Core/Network/TcpTransport.cs | 27 +++++++++------------------ TLSharp.Core/TelegramClient.cs | 25 ++++++++++++++----------- 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/TLSharp.Core/Network/TcpTransport.cs b/TLSharp.Core/Network/TcpTransport.cs index 60c86d2..71bcdd9 100644 --- a/TLSharp.Core/Network/TcpTransport.cs +++ b/TLSharp.Core/Network/TcpTransport.cs @@ -18,39 +18,30 @@ namespace TLSharp.Core.Network TcpClientConnectionHandler handler; readonly string address; readonly int port; - readonly IPAddress ipAddress; + readonly 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 ConnectAsync() { if (handler == null) { - if (tcpClient != null) - { - tcpClient.Close(); - } + var ipAddress = IPAddress.Parse(address); + var endpoint = new IPEndPoint(ipAddress, port); + tcpClient = new TcpClient(ipAddress.AddressFamily); try { - await tcpClient.ConnectAsync(ipAddress, port); - } catch (Exception ex) { - 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)", + tcpClient.Connect(endpoint); + } + 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)", ex); } } else tcpClient = handler(address, port); - sendCounter = 0; - if (tcpClient.Connected) { stream = tcpClient.GetStream(); diff --git a/TLSharp.Core/TelegramClient.cs b/TLSharp.Core/TelegramClient.cs index 5a83bd8..d180680 100644 --- a/TLSharp.Core/TelegramClient.cs +++ b/TLSharp.Core/TelegramClient.cs @@ -65,22 +65,22 @@ namespace TLSharp.Core this.apiId = apiId; this.handler = handler; this.dcIpVersion = dcIpVersion; - - session = Session.TryLoadOrCreateNew(this.store, sessionUserId); - transport = new TcpTransport (session.DataCenter.Address, session.DataCenter.Port, this.handler); } public async Task ConnectAsync(bool reconnect = false, CancellationToken token = default(CancellationToken)) { token.ThrowIfCancellationRequested(); - if (!transport.IsConnected) - { - // we must recreate the session because it might track dirty information - // of a connection that maybe was disconnected, reusing that session will cause errors - session = Session.TryLoadOrCreateNew(store, sessionUserId); - await transport.ConnectAsync(); - } + //if (!transport.IsConnected) + //{ + // // we must recreate the session because it might track dirty information + // // of a connection that maybe was disconnected, reusing that session will cause errors + // session = Session.TryLoadOrCreateNew(store, sessionUserId); + // await transport.ConnectAsync(); + //} + + session = Session.TryLoadOrCreateNew(store, sessionUserId); + transport = new TcpTransport(session.DataCenter.Address, session.DataCenter.Port, this.handler); if (session.AuthKey == null || reconnect) { @@ -131,6 +131,8 @@ namespace TLSharp.Core else dcs = dcOptions.Where(d => d.Id == dcId); // any + dcs = dcs.Where(d => !d.MediaOnly);//.AsEnumerable(); + TLDcOption dc; if (dcIpVersion != DataCenterIPVersion.Default) { @@ -144,8 +146,9 @@ namespace TLSharp.Core var dataCenter = new DataCenter (dcId, dc.IpAddress, dc.Port); - transport = new TcpTransport(dc.IpAddress, dc.Port, handler); + //transport = new TcpTransport(dc.IpAddress, dc.Port, handler); session.DataCenter = dataCenter; + session.Save(); await ConnectAsync(true, token).ConfigureAwait(false); From e674cf1f497f7a351791ae3fa3e7084e6608b06e Mon Sep 17 00:00:00 2001 From: solarin Date: Sat, 11 Apr 2020 19:00:38 +0400 Subject: [PATCH 8/8] removed comment --- 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 d180680..d217ca4 100644 --- a/TLSharp.Core/TelegramClient.cs +++ b/TLSharp.Core/TelegramClient.cs @@ -131,7 +131,7 @@ namespace TLSharp.Core else dcs = dcOptions.Where(d => d.Id == dcId); // any - dcs = dcs.Where(d => !d.MediaOnly);//.AsEnumerable(); + dcs = dcs.Where(d => !d.MediaOnly); TLDcOption dc; if (dcIpVersion != DataCenterIPVersion.Default)