Добавлен magic-header
This commit is contained in:
@@ -9,6 +9,7 @@ namespace Client;
|
|||||||
|
|
||||||
public class TcpClientWrapper : IClient
|
public class TcpClientWrapper : IClient
|
||||||
{
|
{
|
||||||
|
private const uint MagicHeader = 0xDEADBEEF;
|
||||||
private readonly string _host;
|
private readonly string _host;
|
||||||
private readonly int _port;
|
private readonly int _port;
|
||||||
private CancellationTokenSource _cts = new CancellationTokenSource();
|
private CancellationTokenSource _cts = new CancellationTokenSource();
|
||||||
@@ -16,7 +17,7 @@ public class TcpClientWrapper : IClient
|
|||||||
private Func<byte[], Task<Data?>>? _responseConverter;
|
private Func<byte[], Task<Data?>>? _responseConverter;
|
||||||
private TcpClient? _tcpClient;
|
private TcpClient? _tcpClient;
|
||||||
|
|
||||||
public TcpClientWrapper(Func<byte[], Task<Data?>> responseConverter, string host = "192.168.1.117", int port = 5555)
|
public TcpClientWrapper(Func<byte[], Task<Data?>> responseConverter, string host = "localhost", int port = 5555)
|
||||||
{
|
{
|
||||||
_host = host;
|
_host = host;
|
||||||
_port = port;
|
_port = port;
|
||||||
@@ -51,9 +52,26 @@ public class TcpClientWrapper : IClient
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
// Читаем magic header полностью
|
||||||
|
var magicBytes = new byte[4];
|
||||||
|
int totalRead = 0;
|
||||||
|
while (totalRead < 4)
|
||||||
|
{
|
||||||
|
var read = await stream.ReadAsync(magicBytes, totalRead, 4 - totalRead, token);
|
||||||
|
if (read == 0) break; // соединение закрыто
|
||||||
|
totalRead += read;
|
||||||
|
}
|
||||||
|
if (totalRead < 4) break;
|
||||||
|
var magic = BitConverter.ToUInt32(magicBytes, 0);
|
||||||
|
if (magic != MagicHeader)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Invalid magic header: {magic:X8}, expected {MagicHeader:X8}. Skipping packet.");
|
||||||
|
continue; // or break, depending on policy
|
||||||
|
}
|
||||||
|
|
||||||
// Читаем длину полностью
|
// Читаем длину полностью
|
||||||
var lengthBytes = new byte[4];
|
var lengthBytes = new byte[4];
|
||||||
int totalRead = 0;
|
totalRead = 0;
|
||||||
while (totalRead < 4)
|
while (totalRead < 4)
|
||||||
{
|
{
|
||||||
var read = await stream.ReadAsync(lengthBytes, totalRead, 4 - totalRead, token);
|
var read = await stream.ReadAsync(lengthBytes, totalRead, 4 - totalRead, token);
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ namespace NetworkTest;
|
|||||||
|
|
||||||
public class TcpServer : IServer
|
public class TcpServer : IServer
|
||||||
{
|
{
|
||||||
|
private const uint MagicHeader = 0xDEADBEEF;
|
||||||
private readonly TcpListener _listener;
|
private readonly TcpListener _listener;
|
||||||
private readonly Func<long, Data?> _getData;
|
private readonly Func<long, Data?> _getData;
|
||||||
private readonly Func<Data, byte[]> _prepareBytes;
|
private readonly Func<Data, byte[]> _prepareBytes;
|
||||||
@@ -69,7 +70,9 @@ public class TcpServer : IServer
|
|||||||
if (data != null)
|
if (data != null)
|
||||||
{
|
{
|
||||||
var bytes = _prepareBytes(data);
|
var bytes = _prepareBytes(data);
|
||||||
|
var magicBytes = BitConverter.GetBytes(MagicHeader);
|
||||||
var lengthBytes = BitConverter.GetBytes(bytes.Length);
|
var lengthBytes = BitConverter.GetBytes(bytes.Length);
|
||||||
|
await stream.WriteAsync(magicBytes, 0, magicBytes.Length, token);
|
||||||
await stream.WriteAsync(lengthBytes, 0, lengthBytes.Length, token);
|
await stream.WriteAsync(lengthBytes, 0, lengthBytes.Length, token);
|
||||||
await stream.WriteAsync(bytes, 0, bytes.Length, token);
|
await stream.WriteAsync(bytes, 0, bytes.Length, token);
|
||||||
index++;
|
index++;
|
||||||
|
|||||||
Reference in New Issue
Block a user