mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
[+] 接入ET8服务端
[+] 接入ET8服务端
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
|
||||
namespace ET.Server
|
||||
{
|
||||
[EntitySystemOf(typeof(HttpComponent))]
|
||||
[FriendOf(typeof(HttpComponent))]
|
||||
public static partial class HttpComponentSystem
|
||||
{
|
||||
[EntitySystem]
|
||||
private static void Awake(this HttpComponent self, string address)
|
||||
{
|
||||
try
|
||||
{
|
||||
self.Listener = new HttpListener();
|
||||
|
||||
foreach (string s in address.Split(';'))
|
||||
{
|
||||
if (s.Trim() == "")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
self.Listener.Prefixes.Add(s);
|
||||
}
|
||||
|
||||
self.Listener.Start();
|
||||
|
||||
self.Accept().Coroutine();
|
||||
}
|
||||
catch (HttpListenerException e)
|
||||
{
|
||||
throw new Exception($"请先在cmd中运行: netsh http add urlacl url=http://*:你的address中的端口/ user=Everyone, address: {address}", e);
|
||||
}
|
||||
}
|
||||
|
||||
[EntitySystem]
|
||||
private static void Destroy(this HttpComponent self)
|
||||
{
|
||||
self.Listener.Stop();
|
||||
self.Listener.Close();
|
||||
}
|
||||
|
||||
private static async ETTask Accept(this HttpComponent self)
|
||||
{
|
||||
long instanceId = self.InstanceId;
|
||||
while (self.InstanceId == instanceId)
|
||||
{
|
||||
try
|
||||
{
|
||||
HttpListenerContext context = await self.Listener.GetContextAsync();
|
||||
self.Handle(context).Coroutine();
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
{
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static async ETTask Handle(this HttpComponent self, HttpListenerContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
IHttpHandler handler = HttpDispatcher.Instance.Get(self.IScene.SceneType, context.Request.Url.AbsolutePath);
|
||||
await handler.Handle(self.Scene(), context);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e);
|
||||
}
|
||||
context.Request.InputStream.Dispose();
|
||||
context.Response.OutputStream.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user