Create basic web server to receive commands

This commit is contained in:
Norbi Peti 2021-04-19 23:53:41 +02:00
commit ef662f1d1e
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
5 changed files with 126 additions and 0 deletions

8
.gitignore vendored Normal file
View file

@ -0,0 +1,8 @@
bin/
obj/
/packages/
riderModule.iml
/_ReSharper.Caches/
.idea
TBConsole.sln.DotSettings.user

16
TBConsole.sln Normal file
View file

@ -0,0 +1,16 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TBConsole", "TBConsole\TBConsole.csproj", "{9B1B4559-5C22-4F75-B29B-2E1E1E761B1A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{9B1B4559-5C22-4F75-B29B-2E1E1E761B1A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9B1B4559-5C22-4F75-B29B-2E1E1E761B1A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9B1B4559-5C22-4F75-B29B-2E1E1E761B1A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9B1B4559-5C22-4F75-B29B-2E1E1E761B1A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

View file

@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<OutputType>Exe</OutputType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Lib.Harmony" Version="2.0.4" />
</ItemGroup>
<ItemGroup>
<Reference Include="GamecraftModdingAPI">
<HintPath>..\..\GamecraftModdingAPI\GamecraftModdingAPI\bin\Debug\net472\GamecraftModdingAPI.dll</HintPath>
</Reference>
<Reference Include="IllusionPlugin">
<HintPath>..\..\GamecraftModdingAPI\GamecraftModdingAPI\bin\Debug\net472\IllusionPlugin.dll</HintPath>
</Reference>
</ItemGroup>
</Project>

41
TBConsole/TBConsoleMod.cs Normal file
View file

@ -0,0 +1,41 @@
using System;
using System.IO;
using System.Net;
using System.Reflection;
using IllusionPlugin;
namespace TBConsole
{
public class TBConsoleMod : IEnhancedPlugin
{
public override string Name => "TBConsole";
public override string Version { get; } = Assembly.GetExecutingAssembly().GetName().Version.ToString();
private WebServer _server;
public override void OnApplicationStart()
{
GamecraftModdingAPI.Main.Init();
_server = new WebServer(CommandReceived);
_server.Start();
}
public string CommandReceived(string command)
{
return $"Got it: {command}";
}
public override void OnApplicationQuit()
{
_server.Stop();
GamecraftModdingAPI.Main.Shutdown();
}
public static void Main(string[] args)
{
var mod = new TBConsoleMod();
mod._server = new WebServer(mod.CommandReceived);
mod._server.Start();
Console.ReadLine();
}
}
}

40
TBConsole/WebServer.cs Normal file
View file

@ -0,0 +1,40 @@
using System;
using System.IO;
using System.Net;
namespace TBConsole
{
public class WebServer
{
private bool _running;
private readonly HttpListener _listener = new HttpListener();
private Func<string, string> _receiver;
public WebServer(Func<string, string> receiver) => _receiver = receiver;
public void Start()
{
_running = true;
KeepListening();
}
public void Stop()
{
_running = false;
_listener.Stop();
}
private async void KeepListening()
{
_listener.Prefixes.Add("http://localhost:8019/");
_listener.Start();
while (_running)
{
var context = await _listener.GetContextAsync();
string resp = _receiver(await new StreamReader(context.Request.InputStream).ReadToEndAsync());
var sw = new StreamWriter(context.Response.OutputStream);
await sw.WriteLineAsync(resp);
sw.Close();
}
}
}
}