From 3dabf95bb0ab85c19f9f2418c4db1362f2e97cd2 Mon Sep 17 00:00:00 2001 From: glax Date: Sat, 13 Jan 2024 22:31:48 +0100 Subject: [PATCH] GSI Server --- OpenCS2hock/GSIServer.cs | 28 +++++++++++++--------------- OpenCS2hock/OpenCS2hock.cs | 20 +++++++++++++++++--- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/OpenCS2hock/GSIServer.cs b/OpenCS2hock/GSIServer.cs index 2c9eb64..70e30ce 100644 --- a/OpenCS2hock/GSIServer.cs +++ b/OpenCS2hock/GSIServer.cs @@ -6,12 +6,11 @@ namespace OpenCS2hock; public class GSIServer { private HttpListener HttpListener { get; init; } - public delegate void OnMessageEventHandler(string content); - public event OnMessageEventHandler? OnMessage; - private bool keepRunning = true; + private bool _keepRunning = true; + public bool IsRunning { get; private set; } public GSIServer(int port) { @@ -19,15 +18,13 @@ public class GSIServer HttpListener.Prefixes.Add($"http://127.0.0.1:{port}/"); HttpListener.Start(); - Task connectionListener = HandleConnection(); - connectionListener.GetAwaiter().GetResult(); - - HttpListener.Close(); + Thread connectionListener = new (HandleConnection); + connectionListener.Start(); } - private async Task HandleConnection() + private async void HandleConnection() { - while (keepRunning) + while (_keepRunning) { HttpListenerContext context = await HttpListener.GetContextAsync(); HttpListenerRequest request = context.Request; @@ -36,17 +33,18 @@ public class GSIServer HttpResponseMessage responseMessage = new HttpResponseMessage(HttpStatusCode.Accepted); context.Response.OutputStream.Write(Encoding.UTF8.GetBytes(responseMessage.ToString())); - await context.Response.OutputStream.DisposeAsync(); - StreamReader reader = new StreamReader(request.InputStream); - OnMessage?.Invoke(await reader.ReadToEndAsync()); - reader.Close(); - await request.InputStream.DisposeAsync(); + StreamReader reader = new StreamReader(request.InputStream, request.ContentEncoding); + string content = await reader.ReadToEndAsync(); + Console.WriteLine(content); + OnMessage?.Invoke(content); } + HttpListener.Close(); + IsRunning = false; } internal void Dispose() { - keepRunning = false; + _keepRunning = false; } } \ No newline at end of file diff --git a/OpenCS2hock/OpenCS2hock.cs b/OpenCS2hock/OpenCS2hock.cs index e8eb0e8..37dce8a 100644 --- a/OpenCS2hock/OpenCS2hock.cs +++ b/OpenCS2hock/OpenCS2hock.cs @@ -3,12 +3,26 @@ public class OpenCS2hock { private GSIServer GSIServer { get; init; } + private List _shockers = new(); + public OpenCS2hock() { - GSIServer gsiServer; Installer.InstallGsi(); this.GSIServer = new GSIServer(3000); - - this.GSIServer.Dispose(); + this.GSIServer.OnMessage += OnGSIMessage; + + Thread runningThread = new(() => + { + while (GSIServer.IsRunning) + Thread.Sleep(10); + }); + runningThread.Start(); + } + + private void OnGSIMessage(string content) + { + string fileName = Path.Combine(Environment.CurrentDirectory, $"{DateTime.Now.ToLongTimeString().Replace(':','.')}.json"); + File.WriteAllText(fileName, content); + Console.WriteLine(fileName); } } \ No newline at end of file