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");