mirror-ac/service/Worker.cs

110 lines
3.5 KiB
C#
Raw Normal View History

2023-08-18 07:33:13 +02:00
using System.IO.Pipes;
2023-08-20 16:12:04 +02:00
using System.Runtime.CompilerServices;
2023-08-18 09:18:00 +02:00
using System.Runtime.InteropServices;
2023-08-24 09:29:16 +02:00
using System;
2023-09-02 15:47:15 +02:00
using System.Reflection.PortableExecutable;
2023-09-02 17:56:46 +02:00
using System.Net.Sockets;
using System.Net;
using System.Net.Http;
2023-09-10 18:30:46 +02:00
using Serilog;
2023-08-18 10:39:21 +02:00
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
#pragma warning disable CS8600
#pragma warning disable CS8603
2023-08-18 07:33:13 +02:00
namespace service
{
public class Worker : BackgroundService
{
2023-09-10 18:30:46 +02:00
private readonly Serilog.ILogger _logger;
2023-08-18 07:33:13 +02:00
private NamedPipeServerStream _pipeServer;
2023-09-02 17:56:46 +02:00
private byte[] _buffer;
private int _bufferSize;
2023-09-10 18:30:46 +02:00
private static int MAX_BUFFER_SIZE = 8192;
2023-08-18 16:34:15 +02:00
2023-09-10 19:32:12 +02:00
private static int OK_RESPONSE_SIZE = 4;
2023-09-10 18:30:46 +02:00
public Worker(Serilog.ILogger logger)
2023-08-18 07:33:13 +02:00
{
_logger = logger;
2023-09-23 09:22:43 +02:00
_pipeServer = new NamedPipeServerStream(
"DonnaACPipe",
PipeDirection.InOut,
1,
0,
PipeOptions.Asynchronous,
MAX_BUFFER_SIZE,
MAX_BUFFER_SIZE);
2023-09-02 17:56:46 +02:00
_bufferSize = MAX_BUFFER_SIZE;
_buffer = new byte[_bufferSize];
2023-08-18 07:33:13 +02:00
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
2023-09-10 18:30:46 +02:00
_logger.Information("Windows service starting, waiting for client to connect");
2023-08-18 07:33:13 +02:00
2023-08-18 16:34:15 +02:00
// to do: verify whos connecting
2023-08-18 09:18:00 +02:00
_pipeServer.WaitForConnection();
2023-08-18 07:33:13 +02:00
2023-09-10 18:30:46 +02:00
_logger.Information("Client connected to the pipe server");
2023-08-18 07:33:13 +02:00
2023-08-18 09:18:00 +02:00
while (!stoppingToken.IsCancellationRequested)
2023-09-02 17:56:46 +02:00
{
2023-08-18 07:33:13 +02:00
try
{
2023-09-02 17:56:46 +02:00
int numBytesRead = _pipeServer.Read(_buffer, 0, _bufferSize);
if (numBytesRead > 0)
2023-08-18 07:33:13 +02:00
{
2023-09-10 18:30:46 +02:00
_logger.Information("Message received at pipe server with size: {0}", numBytesRead);
2023-09-02 15:47:15 +02:00
2023-09-10 18:30:46 +02:00
Client message = new Client(ref _buffer, numBytesRead, _logger);
2023-09-10 18:30:46 +02:00
message.SendMessageToServer();
2023-09-10 18:30:46 +02:00
ThreadPool.QueueUserWorkItem(state => RelayResponseMessage(ref message));
2023-08-18 07:33:13 +02:00
}
}
catch (Exception ex)
{
2023-09-10 18:30:46 +02:00
_logger.Error("Reading buffer from pipe failed with message: {0}", ex.Message);
2023-08-18 07:33:13 +02:00
}
2023-08-18 15:22:53 +02:00
2023-09-02 17:56:46 +02:00
Array.Clear(_buffer, 0, _bufferSize);
2023-08-18 09:18:00 +02:00
}
}
2023-08-20 16:12:04 +02:00
2023-09-10 18:30:46 +02:00
private void RelayResponseMessage(ref Client message)
{
byte[] responseMessage = message.GetResponseFromServer();
2023-09-24 13:13:20 +02:00
if (responseMessage == null)
{
_logger.Warning("Response message is null");
2023-09-10 19:32:12 +02:00
return;
2023-09-24 13:13:20 +02:00
}
2023-09-10 19:32:12 +02:00
2023-09-10 18:30:46 +02:00
_logger.Information("Sending response message to client with size: {0}", responseMessage.Length);
_pipeServer.Write(responseMessage, 0, responseMessage.Length);
}
2023-08-20 16:12:04 +02:00
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern bool GetNamedPipeClientProcessId(IntPtr Pipe, out uint ClientProcessId);
public static uint GetNamedPipeClientProcId(NamedPipeServerStream PipeServer)
{
UInt32 procId;
IntPtr pipeHandle = PipeServer.SafePipeHandle.DangerousGetHandle();
if (GetNamedPipeClientProcessId(pipeHandle, out procId))
return procId;
return 0;
}
2023-08-18 07:33:13 +02:00
}
}
2023-08-18 10:39:21 +02:00
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
#pragma warning restore CS8600
#pragma warning restore CS8603