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 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
21 changes: 14 additions & 7 deletions TLSharp.Core/Network/TcpTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ 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)
{
Expand All @@ -25,10 +29,13 @@ public TcpTransport(string address, int port, TcpClientConnectionHandler handler

tcpClient = new TcpClient(ipAddress.AddressFamily);

try {
tcpClient.Connect (endpoint);
} 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)",
try
{
tcpClient.Connect(endpoint);
}
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)",
ex);
}
}
Expand Down Expand Up @@ -102,7 +109,7 @@ public bool IsConnected
{
get
{
return this.tcpClient.Connected;
return this.tcpClient != null && this.tcpClient.Connected;
}
}

Expand Down
25 changes: 19 additions & 6 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);
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();
//}

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 Expand Up @@ -121,6 +131,8 @@ public TelegramClient(int apiId, string apiHash,
else
dcs = dcOptions.Where(d => d.Id == dcId); // any

dcs = dcs.Where(d => !d.MediaOnly);

TLDcOption dc;
if (dcIpVersion != DataCenterIPVersion.Default)
{
Expand All @@ -134,8 +146,9 @@ public TelegramClient(int apiId, string apiHash,

var dataCenter = new DataCenter (dcId, dc.IpAddress, dc.Port);

transport = new TcpTransport(dc.IpAddress, dc.Port, handler);
//transport = new TcpTransport(dc.IpAddress, dc.Port, handler);
session.DataCenter = dataCenter;
session.Save();

await ConnectAsync(true, token).ConfigureAwait(false);

Expand Down