mirror of
https://github.com/sochix/TLSharp.git
synced 2025-12-06 08:02:00 +01:00
! IsConnected is restored as it was before
+ TcpTransport has got a connect method which properly initializes the underlying tcpclient
This commit is contained in:
parent
e8ba470357
commit
47cc3405cf
|
|
@ -10,19 +10,28 @@ 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);
|
||||||
|
tcpClient = new TcpClient(ipAddress.AddressFamily);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Connect()
|
||||||
{
|
{
|
||||||
if (handler == null)
|
if (handler == null)
|
||||||
{
|
{
|
||||||
var ipAddress = IPAddress.Parse(address);
|
await tcpClient.ConnectAsync(ipAddress, port);
|
||||||
var endpoint = new IPEndPoint(ipAddress, port);
|
|
||||||
|
|
||||||
tcpClient = new TcpClient(ipAddress.AddressFamily);
|
|
||||||
tcpClient.Connect(endpoint);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
tcpClient = handler(address, port);
|
tcpClient = handler(address, port);
|
||||||
|
|
@ -31,8 +40,9 @@ namespace TLSharp.Core.Network
|
||||||
{
|
{
|
||||||
stream = tcpClient.GetStream();
|
stream = tcpClient.GetStream();
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
stream = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Send(byte[] packet, CancellationToken token = default(CancellationToken))
|
public async Task Send(byte[] packet, CancellationToken token = default(CancellationToken))
|
||||||
{
|
{
|
||||||
if (!tcpClient.Connected)
|
if (!tcpClient.Connected)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue