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 2 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;
string address;
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.

address is not reused, so you don't need to create a field for it

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.

my bad, seems it is needed, howevr these 3 new fields can be marked as readonly

int port;
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 Connect()
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.

if this method is async its name should have the Async suffix

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.

we can change, but also send and receive in the same class are async and are not named as such. i suggest to keep the same style.

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.

this is a convention by Microsoft, the creators of C#; if you see any current API that doesn't fit this pattern we should rename it

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.

ok then
guys, i think we should push this fix asap, i have been testing on a software which has been running for half a day now, perfectly handling the reconnection.

before this fix, it could last only 30 minutes.

{
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
18 changes: 15 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,32 @@ 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.Connect();
}
if (!transport.IsConnected)
Comment thread
solarin marked this conversation as resolved.
Outdated
throw new Exception("Connection to Telegram failed");

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