[+] 接入ET8服务端

[+] 接入ET8服务端
This commit is contained in:
ALEXTANG
2023-07-13 12:23:48 +08:00
parent e0be062006
commit 336d4b2eb9
1316 changed files with 130657 additions and 626 deletions

View File

@@ -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();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 70f2ab9b9fd4f114795f68e7e8311ac5
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: