Core: allow re-connecting from same TelegramClient

If the connection dropped, there was no way to reconnect from the same
instance of TelegramClient.
This commit is contained in:
Andres G. Aragoneses 2020-04-10 15:36:35 +08:00
parent b961897062
commit f25058d813

View file

@ -29,6 +29,8 @@ namespace TLSharp.Core
private string apiHash = String.Empty; private string apiHash = String.Empty;
private int apiId = 0; private int apiId = 0;
private Session session; private Session session;
private string sessionUserId;
private ISessionStore store;
private List<TLDcOption> dcOptions; private List<TLDcOption> dcOptions;
private TcpClientConnectionHandler handler; private TcpClientConnectionHandler handler;
private DataCenterIPVersion dcIpVersion; private DataCenterIPVersion dcIpVersion;
@ -58,20 +60,22 @@ namespace TLSharp.Core
if (store == null) if (store == null)
store = new FileSessionStore(); store = new FileSessionStore();
this.store = store;
this.apiHash = apiHash; this.apiHash = apiHash;
this.apiId = apiId; this.apiId = apiId;
this.handler = handler; this.handler = handler;
this.dcIpVersion = dcIpVersion; this.dcIpVersion = dcIpVersion;
this.sessionUserId = sessionUserId;
session = Session.TryLoadOrCreateNew(store, sessionUserId);
transport = new TcpTransport (session.DataCenter.Address, session.DataCenter.Port, this.handler);
} }
public async Task ConnectAsync(bool reconnect = false, CancellationToken token = default(CancellationToken)) public async Task ConnectAsync(bool reconnect = false, CancellationToken token = default(CancellationToken))
{ {
token.ThrowIfCancellationRequested(); token.ThrowIfCancellationRequested();
session = Session.TryLoadOrCreateNew (store, sessionUserId);
transport = new TcpTransport (session.DataCenter.Address, session.DataCenter.Port, this.handler);
if (session.AuthKey == null || reconnect) if (session.AuthKey == null || reconnect)
{ {
var result = await Authenticator.DoAuthentication(transport, token).ConfigureAwait(false); var result = await Authenticator.DoAuthentication(transport, token).ConfigureAwait(false);