mirror of
https://github.com/donnaskiez/ac.git
synced 2024-11-21 22:24:08 +01:00
got report working YES
This commit is contained in:
parent
50240f4033
commit
5afcec8926
6 changed files with 70 additions and 27 deletions
|
@ -1,21 +1,29 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO.Pipes;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Serilog;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace service
|
||||
{
|
||||
public struct TestReport
|
||||
{
|
||||
public UInt64 Num1;
|
||||
public UInt64 Num2;
|
||||
}
|
||||
public class Worker : BackgroundService
|
||||
{
|
||||
private readonly ILogger<Worker> _logger;
|
||||
private NamedPipeServerStream _pipeServer;
|
||||
private int _threadId;
|
||||
private byte[] _buffer;
|
||||
private Mutex _mutex;
|
||||
|
||||
public Worker(ILogger<Worker> logger)
|
||||
{
|
||||
|
@ -28,30 +36,52 @@ namespace service
|
|||
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
int bytesRead = 0;
|
||||
int offset = 0;
|
||||
|
||||
_logger.LogInformation("Windows service starting, waiting for client to connect");
|
||||
|
||||
_pipeServer.WaitForConnection();
|
||||
|
||||
_logger.LogInformation("Client connected to the pipe server");
|
||||
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
_pipeServer.WaitForConnection();
|
||||
|
||||
_logger.LogInformation("Client connected to the pipe server");
|
||||
|
||||
{
|
||||
try
|
||||
{
|
||||
while ((bytesRead = _pipeServer.Read(_buffer, offset, unchecked((int)_pipeServer.Length))) > 0)
|
||||
if (_pipeServer.Read(_buffer, 0, 1024) > 0)
|
||||
{
|
||||
offset += bytesRead;
|
||||
_logger.LogInformation("Report received, decoding buffer");
|
||||
await TranslatePipeBuffer();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError("Reading buffer from pipe failed with status: {1}", ex.Message);
|
||||
_logger.LogError("Reading buffer from pipe failed with message: {0}", ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task TranslatePipeBuffer()
|
||||
{
|
||||
var packet = BytesToStructure<TestReport>();
|
||||
Array.Clear(_buffer, 0, _buffer.Length);
|
||||
_logger.LogInformation("Num1: {0}, Num2: {1}", packet.Num1, packet.Num2);
|
||||
}
|
||||
|
||||
private T BytesToStructure<T>()
|
||||
{
|
||||
int size = Marshal.SizeOf(typeof(T));
|
||||
|
||||
IntPtr ptr = Marshal.AllocHGlobal(size);
|
||||
|
||||
try
|
||||
{
|
||||
Marshal.Copy(_buffer, 0, ptr, size);
|
||||
return (T)Marshal.PtrToStructure(ptr, typeof(T));
|
||||
}
|
||||
finally
|
||||
{
|
||||
Marshal.FreeHGlobal(ptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
|
|
@ -22,14 +22,14 @@ global::Client::Client( LPTSTR PipeName )
|
|||
}
|
||||
}
|
||||
|
||||
void global::Client::WriteToPipe( TestReport* Report )
|
||||
void global::Client::WriteToPipe( PVOID Buffer, SIZE_T Size )
|
||||
{
|
||||
DWORD bytes_written;
|
||||
|
||||
WriteFile(
|
||||
this->pipe_handle,
|
||||
Report,
|
||||
sizeof( TestReport ),
|
||||
Buffer,
|
||||
Size,
|
||||
&bytes_written,
|
||||
NULL
|
||||
);
|
||||
|
@ -39,4 +39,6 @@ void global::Client::WriteToPipe( TestReport* Report )
|
|||
LOG_ERROR( "WriteFile failed with status code 0x%x", GetLastError() );
|
||||
return;
|
||||
}
|
||||
|
||||
LOG_INFO( "Sent bytes over pipe" );
|
||||
}
|
||||
|
|
|
@ -3,8 +3,6 @@
|
|||
|
||||
#include <Windows.h>
|
||||
|
||||
#include "report.h"
|
||||
|
||||
namespace global
|
||||
{
|
||||
class Client
|
||||
|
@ -14,7 +12,7 @@ namespace global
|
|||
|
||||
public:
|
||||
Client(LPTSTR PipeName);
|
||||
void WriteToPipe( TestReport* Report );
|
||||
void WriteToPipe( PVOID Buffer, SIZE_T Size );
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@ DWORD WINAPI Init(HINSTANCE hinstDLL)
|
|||
|
||||
std::this_thread::sleep_for( std::chrono::seconds( 1 ) );
|
||||
|
||||
LPTSTR pipe_name = (LPTSTR)L"DonnaACPipe";
|
||||
LPTSTR pipe_name = (LPTSTR)L"\\\\.\\pipe\\DonnaACPipe";
|
||||
|
||||
std::shared_ptr<global::ThreadPool> thread_pool = std::make_shared<global::ThreadPool>( 4 );
|
||||
std::shared_ptr<global::Report> report_interface = std::make_shared<global::Report>( thread_pool, pipe_name );
|
||||
|
|
|
@ -1,12 +1,25 @@
|
|||
#include "report.h"
|
||||
|
||||
#include "common.h"
|
||||
|
||||
global::Report::Report( std::shared_ptr<global::ThreadPool> ThreadPool, LPTSTR PipeName )
|
||||
{
|
||||
this->thread_pool = ThreadPool;
|
||||
this->client = std::make_unique<global::Client>( PipeName );
|
||||
this->client = std::make_shared<global::Client>( PipeName );
|
||||
|
||||
//test report
|
||||
TestReport report;
|
||||
report.value1 = 10;
|
||||
report.value2 = 1337;
|
||||
this->ReportViolation( &report );
|
||||
}
|
||||
|
||||
void global::Report::ReportViolation( TestReport* Report )
|
||||
{
|
||||
this->thread_pool->QueueJob( [ this, Report ]() {this->client->WriteToPipe( Report ); } );
|
||||
byte buffer[ 1024 ];
|
||||
int size = sizeof( TestReport );
|
||||
memcpy( buffer, Report, size );
|
||||
LOG_INFO( "sending report over pipe" );
|
||||
|
||||
this->thread_pool->QueueJob( [ this, buffer, size ]() {this->client->WriteToPipe( (PVOID)buffer, size ); } );
|
||||
}
|
||||
|
|
|
@ -6,18 +6,18 @@
|
|||
#include "threadpool.h"
|
||||
#include "client.h"
|
||||
|
||||
struct TestReport
|
||||
{
|
||||
UINT64 value1;
|
||||
UINT64 value2;
|
||||
};
|
||||
|
||||
namespace global
|
||||
{
|
||||
struct TestReport
|
||||
{
|
||||
UINT64 value1;
|
||||
UINT64 value2;
|
||||
};
|
||||
|
||||
class Report
|
||||
{
|
||||
std::shared_ptr<global::ThreadPool> thread_pool;
|
||||
std::unique_ptr<global::Client> client;
|
||||
std::shared_ptr<global::Client> client;
|
||||
public:
|
||||
Report( std::shared_ptr<global::ThreadPool> ThreadPool, LPTSTR PipeName );
|
||||
void ReportViolation( TestReport* Report );
|
||||
|
|
Loading…
Reference in a new issue