mirror of
https://github.com/sochix/TLSharp.git
synced 2026-04-06 23:15:16 +00:00
TLSharp and Tests
This commit is contained in:
parent
9cf850e9b8
commit
3bb487a194
39 changed files with 21943 additions and 0 deletions
51
TLSharp.Core/Network/TcpTransport.cs
Normal file
51
TLSharp.Core/Network/TcpTransport.cs
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TLSharp.Core.Network
|
||||
{
|
||||
public class TcpTransport : IDisposable
|
||||
{
|
||||
private const string defaultConnectionAddress = "91.108.56.165";
|
||||
private const int defaultConnectionPort = 443;
|
||||
private readonly TcpClient _tcpClient;
|
||||
private int sendCounter = 0;
|
||||
|
||||
public TcpTransport(string address = defaultConnectionAddress, int port = defaultConnectionPort)
|
||||
{
|
||||
_tcpClient = new TcpClient();
|
||||
|
||||
var ipAddress = IPAddress.Parse(address);
|
||||
_tcpClient.Connect(ipAddress, port);
|
||||
}
|
||||
|
||||
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 buffer = new byte[_tcpClient.ReceiveBufferSize];
|
||||
var availableBytes = await _tcpClient.GetStream().ReadAsync(buffer, 0, buffer.Length);
|
||||
|
||||
var result = buffer.Take(availableBytes).ToArray();
|
||||
|
||||
return TcpMessage.Decode(result);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_tcpClient.Connected)
|
||||
_tcpClient.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue