Skip to content
This repository was archived by the owner on Dec 5, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 4 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
33 changes: 24 additions & 9 deletions TLSharp.Core/Network/TcpTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,38 @@ namespace TLSharp.Core.Network

public class TcpTransport : IDisposable
{
private readonly TcpClient tcpClient;
private readonly NetworkStream stream;
private TcpClient tcpClient;
private NetworkStream stream;
private int sendCounter = 0;
TcpClientConnectionHandler handler;
readonly string address;
readonly int port;
readonly IPAddress ipAddress;

public TcpTransport(string address, int port, TcpClientConnectionHandler handler = null)
{
this.handler = handler;
this.address = address;
this.port = port;
ipAddress = IPAddress.Parse(address);
}

public async Task ConnectAsync()
{
if (handler == null)
{
var ipAddress = IPAddress.Parse(address);
var endpoint = new IPEndPoint(ipAddress, port);

if (tcpClient != null)
{
tcpClient.Close();
}
tcpClient = new TcpClient(ipAddress.AddressFamily);
sendCounter = 0;

try {
tcpClient.Connect (endpoint);
try
{
await tcpClient.ConnectAsync(ipAddress, port);
} catch (Exception ex) {
throw new Exception ($"Problem when trying to connect to {endpoint}; 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)",
ex);
}
}
Expand Down Expand Up @@ -102,7 +117,7 @@ public bool IsConnected
{
get
{
return this.tcpClient.Connected;
return this.tcpClient != null && this.tcpClient.Connected;
}
}

Expand Down
16 changes: 13 additions & 3 deletions TLSharp.Core/TelegramClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class TelegramClient : IDisposable
private List<TLDcOption> dcOptions;
private TcpClientConnectionHandler handler;
private DataCenterIPVersion dcIpVersion;
private ISessionStore store;
string sessionUserId;

public Session Session
{
Expand All @@ -56,22 +58,30 @@ public TelegramClient(int apiId, string apiHash,
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);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't we just remove this line above? session will be initialized again anyway in ConnectAsync

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately not, because the following like needs it.

to save one initialization of session we must add extra code in the Connect method

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

public async Task ConnectAsync(bool reconnect = false, CancellationToken token = default(CancellationToken))
{
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.ConnectAsync();
}

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