mirror of
https://github.com/sochix/TLSharp.git
synced 2026-01-07 17:29:57 +01:00
Merge e674cf1f49 into dab442f45b
This commit is contained in:
commit
663696f313
|
|
@ -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)
|
||||
{
|
||||
|
|
@ -25,10 +29,13 @@ namespace TLSharp.Core.Network
|
|||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
@ -102,7 +109,7 @@ namespace TLSharp.Core.Network
|
|||
{
|
||||
get
|
||||
{
|
||||
return this.tcpClient.Connected;
|
||||
return this.tcpClient != null && this.tcpClient.Connected;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -32,6 +32,8 @@ namespace TLSharp.Core
|
|||
private List<TLDcOption> dcOptions;
|
||||
private TcpClientConnectionHandler handler;
|
||||
private DataCenterIPVersion dcIpVersion;
|
||||
private ISessionStore store;
|
||||
string sessionUserId;
|
||||
|
||||
public Session Session
|
||||
{
|
||||
|
|
@ -56,22 +58,30 @@ namespace TLSharp.Core
|
|||
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);
|
||||
|
|
@ -136,8 +146,9 @@ namespace TLSharp.Core
|
|||
|
||||
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);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue