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.
This commit is contained in:
solarin 2020-04-09 04:47:41 +04:00
parent 2bcae5bd9b
commit 5778366b29
2 changed files with 13 additions and 4 deletions

View file

@ -37,8 +37,10 @@ namespace TLSharp.Core.Network
tcpClient.Close(); tcpClient.Close();
} }
tcpClient = new TcpClient(ipAddress.AddressFamily); tcpClient = new TcpClient(ipAddress.AddressFamily);
sendCounter = 0;
try { try
{
await tcpClient.ConnectAsync(ipAddress, port); await tcpClient.ConnectAsync(ipAddress, port);
} catch (Exception ex) { } 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)", 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)",

View file

@ -32,6 +32,8 @@ namespace TLSharp.Core
private List<TLDcOption> dcOptions; private List<TLDcOption> dcOptions;
private TcpClientConnectionHandler handler; private TcpClientConnectionHandler handler;
private DataCenterIPVersion dcIpVersion; private DataCenterIPVersion dcIpVersion;
private ISessionStore store;
string sessionUserId;
public Session Session public Session Session
{ {
@ -56,15 +58,15 @@ namespace TLSharp.Core
if (string.IsNullOrEmpty(apiHash)) if (string.IsNullOrEmpty(apiHash))
throw new MissingApiConfigurationException("API_HASH"); throw new MissingApiConfigurationException("API_HASH");
if (store == null) this.store = store ?? new FileSessionStore();
store = new FileSessionStore(); this.sessionUserId = sessionUserId;
this.apiHash = apiHash; this.apiHash = apiHash;
this.apiId = apiId; this.apiId = apiId;
this.handler = handler; this.handler = handler;
this.dcIpVersion = dcIpVersion; 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); transport = new TcpTransport (session.DataCenter.Address, session.DataCenter.Port, this.handler);
} }
@ -73,7 +75,12 @@ namespace TLSharp.Core
token.ThrowIfCancellationRequested(); token.ThrowIfCancellationRequested();
if (!transport.IsConnected) 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(); await transport.Connect();
}
if (!transport.IsConnected) if (!transport.IsConnected)
throw new Exception("Connection to Telegram failed"); throw new Exception("Connection to Telegram failed");