Skip to content
This repository was archived by the owner on Dec 5, 2021. It is now read-only.
Closed
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
26 changes: 18 additions & 8 deletions TLSharp.Core/Network/TcpTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,28 @@ 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;
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);
tcpClient = new TcpClient(ipAddress.AddressFamily);
}

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

tcpClient = new TcpClient(ipAddress.AddressFamily);
tcpClient.Connect(endpoint);
await tcpClient.ConnectAsync(ipAddress, port);
}
else
tcpClient = handler(address, port);
Expand All @@ -31,8 +40,9 @@ public TcpTransport(string address, int port, TcpClientConnectionHandler handler
{
stream = tcpClient.GetStream();
}
else
stream = null;
}

public async Task Send(byte[] packet, CancellationToken token = default(CancellationToken))
{
if (!tcpClient.Connected)
Expand Down
2 changes: 1 addition & 1 deletion TLSharp.Core/TelegramClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ public bool IsConnected
{
get
{
if (transport == null)
if (transport == null || sender == null)
return false;
return transport.IsConnected;
}
Expand Down