fixed not to conflict

! IsConnected is restored as it was before
+ TcpTransport has got a connect method which properly initializes the underlying tcpclient
This commit is contained in:
solarin 2020-04-08 23:20:54 +04:00
parent c67801a6c9
commit 2bcae5bd9b
2 changed files with 26 additions and 8 deletions

View file

@ -12,23 +12,36 @@ namespace TLSharp.Core.Network
public class TcpTransport : IDisposable public class TcpTransport : IDisposable
{ {
private readonly TcpClient tcpClient; private TcpClient tcpClient;
private readonly NetworkStream stream; private NetworkStream stream;
private int sendCounter = 0; private int sendCounter = 0;
TcpClientConnectionHandler handler;
string address;
int port;
IPAddress ipAddress;
public TcpTransport(string address, int port, TcpClientConnectionHandler handler = null) 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()
{ {
if (handler == null) if (handler == null)
{ {
var ipAddress = IPAddress.Parse(address); if (tcpClient != null)
var endpoint = new IPEndPoint(ipAddress, port); {
tcpClient.Close();
}
tcpClient = new TcpClient(ipAddress.AddressFamily); tcpClient = new TcpClient(ipAddress.AddressFamily);
try { try {
tcpClient.Connect (endpoint); await tcpClient.ConnectAsync(ipAddress, port);
} catch (Exception ex) { } 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); ex);
} }
} }
@ -102,7 +115,7 @@ namespace TLSharp.Core.Network
{ {
get get
{ {
return this.tcpClient.Connected; return this.tcpClient != null && this.tcpClient.Connected;
} }
} }

View file

@ -72,6 +72,11 @@ namespace TLSharp.Core
{ {
token.ThrowIfCancellationRequested(); token.ThrowIfCancellationRequested();
if (!transport.IsConnected)
await transport.Connect();
if (!transport.IsConnected)
throw new Exception("Connection to Telegram failed");
if (session.AuthKey == null || reconnect) if (session.AuthKey == null || reconnect)
{ {
var result = await Authenticator.DoAuthentication(transport, token).ConfigureAwait(false); var result = await Authenticator.DoAuthentication(transport, token).ConfigureAwait(false);