From 5778366b297d543194e870b2b8fb31bfb446d37c Mon Sep 17 00:00:00 2001 From: solarin Date: Thu, 9 Apr 2020 04:47:41 +0400 Subject: [PATCH] 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");