mirror of
https://github.com/sochix/TLSharp.git
synced 2025-12-06 08:02:00 +01:00
94 lines
3.3 KiB
C#
94 lines
3.3 KiB
C#
using System;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace TLSharp.Core.Network
|
|
{
|
|
public delegate TcpClient TcpClientConnectionHandler(string address, int port);
|
|
|
|
public class TcpTransport : IDisposable
|
|
{
|
|
private readonly TcpClient _tcpClient;
|
|
private int sendCounter;
|
|
|
|
public TcpTransport(string address, int port, TcpClientConnectionHandler handler = null)
|
|
{
|
|
if (handler == null)
|
|
{
|
|
_tcpClient = new TcpClient();
|
|
|
|
var ipAddress = IPAddress.Parse(address);
|
|
_tcpClient.Connect(ipAddress, port);
|
|
}
|
|
else
|
|
{
|
|
_tcpClient = handler(address, port);
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (_tcpClient.Connected)
|
|
_tcpClient.Close();
|
|
}
|
|
|
|
public async Task Send(byte[] packet)
|
|
{
|
|
if (!_tcpClient.Connected)
|
|
throw new InvalidOperationException("Client not connected to server.");
|
|
|
|
var tcpMessage = new TcpMessage(sendCounter, packet);
|
|
|
|
await _tcpClient.GetStream().WriteAsync(tcpMessage.Encode(), 0, tcpMessage.Encode().Length);
|
|
sendCounter++;
|
|
}
|
|
|
|
public async Task<TcpMessage> Receieve()
|
|
{
|
|
var stream = _tcpClient.GetStream();
|
|
|
|
var packetLengthBytes = new byte[4];
|
|
if (await stream.ReadAsync(packetLengthBytes, 0, 4) != 4)
|
|
throw new InvalidOperationException("Couldn't read the packet length");
|
|
var packetLength = BitConverter.ToInt32(packetLengthBytes, 0);
|
|
|
|
var seqBytes = new byte[4];
|
|
if (await stream.ReadAsync(seqBytes, 0, 4) != 4)
|
|
throw new InvalidOperationException("Couldn't read the sequence");
|
|
var seq = BitConverter.ToInt32(seqBytes, 0);
|
|
|
|
var readBytes = 0;
|
|
var body = new byte[packetLength - 12];
|
|
var neededToRead = packetLength - 12;
|
|
|
|
do
|
|
{
|
|
var bodyByte = new byte[packetLength - 12];
|
|
var availableBytes = await stream.ReadAsync(bodyByte, 0, neededToRead);
|
|
neededToRead -= availableBytes;
|
|
Buffer.BlockCopy(bodyByte, 0, body, readBytes, availableBytes);
|
|
readBytes += availableBytes;
|
|
} while (readBytes != packetLength - 12);
|
|
|
|
var crcBytes = new byte[4];
|
|
if (await stream.ReadAsync(crcBytes, 0, 4) != 4)
|
|
throw new InvalidOperationException("Couldn't read the crc");
|
|
var checksum = BitConverter.ToInt32(crcBytes, 0);
|
|
|
|
var rv = new byte[packetLengthBytes.Length + seqBytes.Length + body.Length];
|
|
|
|
Buffer.BlockCopy(packetLengthBytes, 0, rv, 0, packetLengthBytes.Length);
|
|
Buffer.BlockCopy(seqBytes, 0, rv, packetLengthBytes.Length, seqBytes.Length);
|
|
Buffer.BlockCopy(body, 0, rv, packetLengthBytes.Length + seqBytes.Length, body.Length);
|
|
var crc32 = new Ionic.Crc.CRC32();
|
|
crc32.SlurpBlock(rv, 0, rv.Length);
|
|
var validChecksum = crc32.Crc32Result;
|
|
|
|
if (checksum != validChecksum)
|
|
throw new InvalidOperationException("invalid checksum! skip");
|
|
|
|
return new TcpMessage(seq, body);
|
|
}
|
|
}
|
|
} |