LRM VS Solution

This commit is contained in:
Derek S 2021-04-05 11:11:38 -05:00
parent f60d76db9a
commit 77cd0ca4eb
12 changed files with 214 additions and 161 deletions

View file

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31129.286
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LRM", "LRM\LRM.csproj", "{BA0E55C8-6B24-4690-AC55-1DDDB4F7C05F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BA0E55C8-6B24-4690-AC55-1DDDB4F7C05F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BA0E55C8-6B24-4690-AC55-1DDDB4F7C05F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BA0E55C8-6B24-4690-AC55-1DDDB4F7C05F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BA0E55C8-6B24-4690-AC55-1DDDB4F7C05F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3B4C70D2-0DA3-4638-9D5E-03F35729DC2B}
EndGlobalSection
EndGlobal

View file

@ -1,60 +1,60 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Text;
namespace LightReflectiveMirror.Compression
{
internal static class StringCompressor
{
/// <summary>
/// Compresses the string.
/// </summary>
/// <param name="text">The text.</param>
/// <returns></returns>
public static string Compress(this string text)
{
byte[] buffer = Encoding.UTF8.GetBytes(text);
var memoryStream = new MemoryStream();
using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
{
gZipStream.Write(buffer, 0, buffer.Length);
}
memoryStream.Position = 0;
var compressedData = new byte[memoryStream.Length];
memoryStream.Read(compressedData, 0, compressedData.Length);
var gZipBuffer = new byte[compressedData.Length + 4];
Buffer.BlockCopy(compressedData, 0, gZipBuffer, 4, compressedData.Length);
Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gZipBuffer, 0, 4);
return Convert.ToBase64String(gZipBuffer);
}
/// <summary>
/// Decompresses the string.
/// </summary>
/// <param name="compressedText">The compressed text.</param>
/// <returns></returns>
public static string Decompress(this string compressedText)
{
byte[] gZipBuffer = Convert.FromBase64String(compressedText);
using (var memoryStream = new MemoryStream())
{
int dataLength = BitConverter.ToInt32(gZipBuffer, 0);
memoryStream.Write(gZipBuffer, 4, gZipBuffer.Length - 4);
var buffer = new byte[dataLength];
memoryStream.Position = 0;
using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
{
gZipStream.Read(buffer, 0, buffer.Length);
}
return Encoding.UTF8.GetString(buffer);
}
}
}
}
using System;
using System.IO;
using System.IO.Compression;
using System.Text;
namespace LightReflectiveMirror.Compression
{
internal static class StringCompressor
{
/// <summary>
/// Compresses the string.
/// </summary>
/// <param name="text">The text.</param>
/// <returns></returns>
public static string Compress(this string text)
{
byte[] buffer = Encoding.UTF8.GetBytes(text);
var memoryStream = new MemoryStream();
using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
{
gZipStream.Write(buffer, 0, buffer.Length);
}
memoryStream.Position = 0;
var compressedData = new byte[memoryStream.Length];
memoryStream.Read(compressedData, 0, compressedData.Length);
var gZipBuffer = new byte[compressedData.Length + 4];
Buffer.BlockCopy(compressedData, 0, gZipBuffer, 4, compressedData.Length);
Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gZipBuffer, 0, 4);
return Convert.ToBase64String(gZipBuffer);
}
/// <summary>
/// Decompresses the string.
/// </summary>
/// <param name="compressedText">The compressed text.</param>
/// <returns></returns>
public static string Decompress(this string compressedText)
{
byte[] gZipBuffer = Convert.FromBase64String(compressedText);
using (var memoryStream = new MemoryStream())
{
int dataLength = BitConverter.ToInt32(gZipBuffer, 0);
memoryStream.Write(gZipBuffer, 4, gZipBuffer.Length - 4);
var buffer = new byte[dataLength];
memoryStream.Position = 0;
using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
{
gZipStream.Read(buffer, 0, buffer.Length);
}
return Encoding.UTF8.GetString(buffer);
}
}
}
}

View file

@ -1,101 +1,101 @@
using Grapevine;
using LightReflectiveMirror.Compression;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace LightReflectiveMirror.Endpoints
{
[Serializable]
struct RelayStats
{
public int ConnectedClients;
public int RoomCount;
public int PublicRoomCount;
public TimeSpan Uptime;
}
[RestResource]
public class Endpoint
{
private List<Room> _rooms { get => Program.instance.GetRooms(); }
private RelayStats _stats { get => new RelayStats
{
ConnectedClients = Program.instance.GetConnections(),
RoomCount = Program.instance.GetRooms().Count,
PublicRoomCount = Program.instance.GetPublicRoomCount(),
Uptime = Program.instance.GetUptime()
}; }
[RestRoute("Get", "/api/stats")]
public async Task Stats(IHttpContext context)
{
string json = JsonConvert.SerializeObject(_stats, Formatting.Indented);
await context.Response.SendResponseAsync(json);
}
[RestRoute("Get", "/api/servers")]
public async Task ServerList(IHttpContext context)
{
if (Program.conf.EndpointServerList)
{
string json = JsonConvert.SerializeObject(_rooms, Formatting.Indented);
await context.Response.SendResponseAsync(json);
}
else
await context.Response.SendResponseAsync(HttpStatusCode.Forbidden);
}
[RestRoute("Get", "/api/compressed/servers")]
public async Task ServerListCompressed(IHttpContext context)
{
if (Program.conf.EndpointServerList)
{
string json = JsonConvert.SerializeObject(_rooms);
await context.Response.SendResponseAsync(json.Compress());
}
else
await context.Response.SendResponseAsync(HttpStatusCode.Forbidden);
}
}
public class EndpointServer
{
public bool Start(ushort port = 8080)
{
try
{
var config = new ConfigurationBuilder()
.SetBasePath(System.IO.Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();
var server = new RestServerBuilder(new ServiceCollection(), config,
(services) =>
{
services.AddLogging(configure => configure.AddConsole());
services.Configure<LoggerFilterOptions>(options => options.MinLevel = LogLevel.None);
}, (server) =>
{
server.Prefixes.Add($"http://*:{port}/");
}).Build();
server.Router.Options.SendExceptionMessages = false;
server.Start();
return true;
}
catch
{
return false;
}
}
}
}
using Grapevine;
using LightReflectiveMirror.Compression;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace LightReflectiveMirror.Endpoints
{
[Serializable]
struct RelayStats
{
public int ConnectedClients;
public int RoomCount;
public int PublicRoomCount;
public TimeSpan Uptime;
}
[RestResource]
public class Endpoint
{
private List<Room> _rooms { get => Program.instance.GetRooms(); }
private RelayStats _stats { get => new RelayStats
{
ConnectedClients = Program.instance.GetConnections(),
RoomCount = Program.instance.GetRooms().Count,
PublicRoomCount = Program.instance.GetPublicRoomCount(),
Uptime = Program.instance.GetUptime()
}; }
[RestRoute("Get", "/api/stats")]
public async Task Stats(IHttpContext context)
{
string json = JsonConvert.SerializeObject(_stats, Formatting.Indented);
await context.Response.SendResponseAsync(json);
}
[RestRoute("Get", "/api/servers")]
public async Task ServerList(IHttpContext context)
{
if (Program.conf.EndpointServerList)
{
string json = JsonConvert.SerializeObject(_rooms, Formatting.Indented);
await context.Response.SendResponseAsync(json);
}
else
await context.Response.SendResponseAsync(HttpStatusCode.Forbidden);
}
[RestRoute("Get", "/api/compressed/servers")]
public async Task ServerListCompressed(IHttpContext context)
{
if (Program.conf.EndpointServerList)
{
string json = JsonConvert.SerializeObject(_rooms);
await context.Response.SendResponseAsync(json.Compress());
}
else
await context.Response.SendResponseAsync(HttpStatusCode.Forbidden);
}
}
public class EndpointServer
{
public bool Start(ushort port = 8080)
{
try
{
var config = new ConfigurationBuilder()
.SetBasePath(System.IO.Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();
var server = new RestServerBuilder(new ServiceCollection(), config,
(services) =>
{
services.AddLogging(configure => configure.AddConsole());
services.Configure<LoggerFilterOptions>(options => options.MinLevel = LogLevel.None);
}, (server) =>
{
server.Prefixes.Add($"http://*:{port}/");
}).Build();
server.Router.Options.SendExceptionMessages = false;
server.Start();
return true;
}
catch
{
return false;
}
}
}
}

View file

@ -0,0 +1,3 @@
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<Costura />
</Weavers>

View file

@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Costura.Fody" Version="5.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Grapevine" Version="5.0.0-rc.5" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
</Project>