GSI Server
This commit is contained in:
parent
24821b84e9
commit
3dabf95bb0
@ -6,12 +6,11 @@ namespace OpenCS2hock;
|
|||||||
public class GSIServer
|
public class GSIServer
|
||||||
{
|
{
|
||||||
private HttpListener HttpListener { get; init; }
|
private HttpListener HttpListener { get; init; }
|
||||||
|
|
||||||
public delegate void OnMessageEventHandler(string content);
|
public delegate void OnMessageEventHandler(string content);
|
||||||
|
|
||||||
public event OnMessageEventHandler? OnMessage;
|
public event OnMessageEventHandler? OnMessage;
|
||||||
|
|
||||||
private bool keepRunning = true;
|
private bool _keepRunning = true;
|
||||||
|
public bool IsRunning { get; private set; }
|
||||||
|
|
||||||
public GSIServer(int port)
|
public GSIServer(int port)
|
||||||
{
|
{
|
||||||
@ -19,15 +18,13 @@ public class GSIServer
|
|||||||
HttpListener.Prefixes.Add($"http://127.0.0.1:{port}/");
|
HttpListener.Prefixes.Add($"http://127.0.0.1:{port}/");
|
||||||
HttpListener.Start();
|
HttpListener.Start();
|
||||||
|
|
||||||
Task connectionListener = HandleConnection();
|
Thread connectionListener = new (HandleConnection);
|
||||||
connectionListener.GetAwaiter().GetResult();
|
connectionListener.Start();
|
||||||
|
|
||||||
HttpListener.Close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task HandleConnection()
|
private async void HandleConnection()
|
||||||
{
|
{
|
||||||
while (keepRunning)
|
while (_keepRunning)
|
||||||
{
|
{
|
||||||
HttpListenerContext context = await HttpListener.GetContextAsync();
|
HttpListenerContext context = await HttpListener.GetContextAsync();
|
||||||
HttpListenerRequest request = context.Request;
|
HttpListenerRequest request = context.Request;
|
||||||
@ -36,17 +33,18 @@ public class GSIServer
|
|||||||
|
|
||||||
HttpResponseMessage responseMessage = new HttpResponseMessage(HttpStatusCode.Accepted);
|
HttpResponseMessage responseMessage = new HttpResponseMessage(HttpStatusCode.Accepted);
|
||||||
context.Response.OutputStream.Write(Encoding.UTF8.GetBytes(responseMessage.ToString()));
|
context.Response.OutputStream.Write(Encoding.UTF8.GetBytes(responseMessage.ToString()));
|
||||||
await context.Response.OutputStream.DisposeAsync();
|
|
||||||
|
|
||||||
StreamReader reader = new StreamReader(request.InputStream);
|
StreamReader reader = new StreamReader(request.InputStream, request.ContentEncoding);
|
||||||
OnMessage?.Invoke(await reader.ReadToEndAsync());
|
string content = await reader.ReadToEndAsync();
|
||||||
reader.Close();
|
Console.WriteLine(content);
|
||||||
await request.InputStream.DisposeAsync();
|
OnMessage?.Invoke(content);
|
||||||
}
|
}
|
||||||
|
HttpListener.Close();
|
||||||
|
IsRunning = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void Dispose()
|
internal void Dispose()
|
||||||
{
|
{
|
||||||
keepRunning = false;
|
_keepRunning = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -3,12 +3,26 @@
|
|||||||
public class OpenCS2hock
|
public class OpenCS2hock
|
||||||
{
|
{
|
||||||
private GSIServer GSIServer { get; init; }
|
private GSIServer GSIServer { get; init; }
|
||||||
|
private List<Shocker> _shockers = new();
|
||||||
|
|
||||||
public OpenCS2hock()
|
public OpenCS2hock()
|
||||||
{
|
{
|
||||||
GSIServer gsiServer;
|
|
||||||
Installer.InstallGsi();
|
Installer.InstallGsi();
|
||||||
this.GSIServer = new GSIServer(3000);
|
this.GSIServer = new GSIServer(3000);
|
||||||
|
this.GSIServer.OnMessage += OnGSIMessage;
|
||||||
|
|
||||||
this.GSIServer.Dispose();
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user