2023-08-29 19:36:58 +02:00
|
|
|
|
using Microsoft.AspNetCore.Authentication.OAuth.Claims;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using Serilog;
|
2023-09-02 15:47:15 +02:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2023-09-24 13:13:20 +02:00
|
|
|
|
using System.Linq.Expressions;
|
2023-08-29 19:36:58 +02:00
|
|
|
|
|
|
|
|
|
namespace service
|
|
|
|
|
{
|
|
|
|
|
public class Client
|
|
|
|
|
{
|
2023-09-10 18:30:46 +02:00
|
|
|
|
private readonly Serilog.ILogger _logger;
|
2023-09-02 15:47:15 +02:00
|
|
|
|
private IPEndPoint _ipEndPoint;
|
|
|
|
|
private TcpClient _tcpClient;
|
2023-09-10 18:30:46 +02:00
|
|
|
|
private NetworkStream _stream;
|
2023-09-02 15:47:15 +02:00
|
|
|
|
private byte[] _buffer;
|
|
|
|
|
private int _bufferSize;
|
2023-08-29 19:36:58 +02:00
|
|
|
|
|
2023-09-10 18:30:46 +02:00
|
|
|
|
public Client(ref byte[] buffer, int bufferSize, Serilog.ILogger logger)
|
2023-09-02 15:47:15 +02:00
|
|
|
|
{
|
2023-09-10 18:30:46 +02:00
|
|
|
|
_logger = logger;
|
2023-09-02 15:47:15 +02:00
|
|
|
|
_ipEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888);
|
|
|
|
|
_tcpClient = new TcpClient();
|
2023-09-10 18:30:46 +02:00
|
|
|
|
_tcpClient.Connect(_ipEndPoint);
|
|
|
|
|
_stream = _tcpClient.GetStream();
|
2023-09-02 15:47:15 +02:00
|
|
|
|
_buffer = buffer;
|
|
|
|
|
_bufferSize = bufferSize;
|
2023-08-29 19:36:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-02 15:47:15 +02:00
|
|
|
|
public void SendMessageToServer()
|
2023-08-29 19:36:58 +02:00
|
|
|
|
{
|
2023-09-24 13:13:20 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_stream.Write(_buffer, 0, _bufferSize);
|
|
|
|
|
}
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.Error("{0}", ex.Message);
|
|
|
|
|
}
|
2023-08-29 19:36:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-24 13:13:20 +02:00
|
|
|
|
public byte[]? GetResponseFromServer()
|
2023-09-10 12:20:35 +02:00
|
|
|
|
{
|
|
|
|
|
byte[] buffer = new byte[1024];
|
|
|
|
|
|
2023-09-24 13:13:20 +02:00
|
|
|
|
try
|
2023-09-10 12:20:35 +02:00
|
|
|
|
{
|
2023-09-24 13:13:20 +02:00
|
|
|
|
using (MemoryStream memoryStream = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
int bytesRead = _stream.Read(buffer, 0, 1024);
|
2023-09-10 18:30:46 +02:00
|
|
|
|
|
2023-09-24 13:13:20 +02:00
|
|
|
|
memoryStream.Write(buffer, 0, bytesRead);
|
2023-09-10 12:20:35 +02:00
|
|
|
|
|
2023-09-24 13:13:20 +02:00
|
|
|
|
return memoryStream.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.Error("{0}", ex.Message);
|
|
|
|
|
return null;
|
2023-09-10 12:20:35 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-29 19:36:58 +02:00
|
|
|
|
}
|
|
|
|
|
}
|