Browse Source

Create basic web server to receive commands

tags/v1.0.0
NorbiPeti 3 years ago
commit
ef662f1d1e
Signed by: NorbiPeti <szatmari.norbert.peter@gmail.com> GPG Key ID: DBA4C4549A927E56
5 changed files with 126 additions and 0 deletions
  1. +8
    -0
      .gitignore
  2. +16
    -0
      TBConsole.sln
  3. +21
    -0
      TBConsole/TBConsole.csproj
  4. +41
    -0
      TBConsole/TBConsoleMod.cs
  5. +40
    -0
      TBConsole/WebServer.cs

+ 8
- 0
.gitignore View File

@@ -0,0 +1,8 @@
bin/
obj/
/packages/
riderModule.iml
/_ReSharper.Caches/

.idea
TBConsole.sln.DotSettings.user

+ 16
- 0
TBConsole.sln 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

+ 21
- 0
TBConsole/TBConsole.csproj 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
- 0
TBConsole/TBConsoleMod.cs 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
- 0
TBConsole/WebServer.cs 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();
}
}
}
}

Loading…
Cancel
Save