Skip to content
This repository has been archived by the owner on Dec 5, 2021. It is now read-only.

Core: allow re-connecting from same TelegramClient #937

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions TLSharp.Core/TelegramClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class TelegramClient : IDisposable
private string apiHash = String.Empty;
private int apiId = 0;
private Session session;
private string sessionUserId;
private ISessionStore store;
private List<TLDcOption> dcOptions;
private TcpClientConnectionHandler handler;
private DataCenterIPVersion dcIpVersion;
Expand Down Expand Up @@ -58,20 +60,22 @@ public TelegramClient(int apiId, string apiHash,

if (store == null)
store = new FileSessionStore();
this.store = store;

this.apiHash = apiHash;
this.apiId = apiId;
this.handler = handler;
this.dcIpVersion = dcIpVersion;

session = Session.TryLoadOrCreateNew(store, sessionUserId);
transport = new TcpTransport (session.DataCenter.Address, session.DataCenter.Port, this.handler);
this.sessionUserId = sessionUserId;
}

public async Task ConnectAsync(bool reconnect = false, CancellationToken token = default(CancellationToken))
{
token.ThrowIfCancellationRequested();

session = Session.TryLoadOrCreateNew (store, sessionUserId);
transport = new TcpTransport (session.DataCenter.Address, session.DataCenter.Port, this.handler);

if (session.AuthKey == null || reconnect)
{
var result = await Authenticator.DoAuthentication(transport, token).ConfigureAwait(false);
Expand Down