mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-05-07 12:16:04 +00:00
154 lines
4.7 KiB
C#
154 lines
4.7 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace WelsonJS.Launcher
|
|
{
|
|
public class ResourceServer
|
|
{
|
|
private readonly HttpListener _listener;
|
|
private CancellationTokenSource _cts;
|
|
private Task _serverTask;
|
|
private bool _isRunning;
|
|
private string _prefix;
|
|
private string _resourceName;
|
|
|
|
public ResourceServer(string prefix, string resourceName)
|
|
{
|
|
_prefix = prefix;
|
|
_listener = new HttpListener();
|
|
_listener.Prefixes.Add(prefix);
|
|
_resourceName = resourceName;
|
|
}
|
|
|
|
public void Start()
|
|
{
|
|
if (_isRunning) return;
|
|
|
|
_isRunning = true;
|
|
_cts = new CancellationTokenSource();
|
|
_listener.Start();
|
|
|
|
// Open the web browser
|
|
Process.Start(_prefix);
|
|
|
|
// Run a task with cancellation token
|
|
_serverTask = Task.Run(() => ListenLoop(_cts.Token));
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
_isRunning = false;
|
|
_cts.Cancel();
|
|
_listener.Stop();
|
|
|
|
MessageBox.Show("Server stopped.");
|
|
}
|
|
|
|
public bool IsRunning()
|
|
{
|
|
return _isRunning;
|
|
}
|
|
|
|
private async Task ListenLoop(CancellationToken token)
|
|
{
|
|
while (!token.IsCancellationRequested && _isRunning)
|
|
{
|
|
try
|
|
{
|
|
ProcessRequest(await _listener.GetContextAsync());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (token.IsCancellationRequested || !_isRunning) break;
|
|
MessageBox.Show($"Error: {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ProcessRequest(HttpListenerContext context)
|
|
{
|
|
string path = context.Request.Url.AbsolutePath.TrimStart('/');
|
|
|
|
if ("favicon.ico".Equals(path, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
ServeResource(context, GetResource("favicon"), "image/x-icon");
|
|
return;
|
|
}
|
|
|
|
ServeResource(context, GetResource(_resourceName), "text/html");
|
|
}
|
|
|
|
private void ServeResource(HttpListenerContext context, byte[] data, string mimeType = "text/html")
|
|
{
|
|
if (data == null) {
|
|
data = "text/html".Equals(mimeType, StringComparison.OrdinalIgnoreCase) ?
|
|
Encoding.UTF8.GetBytes("<html><body><h1>Could not find the resource.</h1></body></html>") :
|
|
Encoding.UTF8.GetBytes("Could not find the resource.")
|
|
;
|
|
}
|
|
|
|
context.Response.ContentType = mimeType;
|
|
context.Response.ContentLength64 = data.Length;
|
|
context.Response.OutputStream.Write(data, 0, data.Length);
|
|
context.Response.OutputStream.Close();
|
|
}
|
|
|
|
private byte[] GetResource(string resourceName)
|
|
{
|
|
// Try to fetch embedded resource.
|
|
byte[] data = GetEmbeddedResource(typeof(ResourceServer).Namespace + "." + resourceName);
|
|
if (data != null) return data;
|
|
|
|
// Fallback: Try to fetch resource from ResourceManager.
|
|
return GetResourceFromManager(resourceName);
|
|
}
|
|
|
|
private byte[] GetEmbeddedResource(string fullResourceName)
|
|
{
|
|
Assembly assembly = Assembly.GetExecutingAssembly();
|
|
using (Stream stream = assembly.GetManifestResourceStream(fullResourceName))
|
|
{
|
|
if (stream != null)
|
|
{
|
|
using (MemoryStream memoryStream = new MemoryStream())
|
|
{
|
|
stream.CopyTo(memoryStream);
|
|
return memoryStream.ToArray();
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private byte[] GetResourceFromManager(string resourceName)
|
|
{
|
|
object resourceObject = Properties.Resources.ResourceManager.GetObject(resourceName);
|
|
switch (resourceObject)
|
|
{
|
|
case byte[] resourceBytes:
|
|
return resourceBytes;
|
|
case System.Drawing.Icon icon:
|
|
return ConvertIconToBytes(icon);
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private byte[] ConvertIconToBytes(System.Drawing.Icon icon)
|
|
{
|
|
using (MemoryStream memoryStream = new MemoryStream())
|
|
{
|
|
icon.Save(memoryStream);
|
|
return memoryStream.ToArray();
|
|
}
|
|
}
|
|
}
|
|
}
|