[+] 接入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,72 @@
using System.Net;
using MongoDB.Bson;
namespace ET.Server
{
[EntitySystemOf(typeof(NetServerComponent))]
[FriendOf(typeof(NetServerComponent))]
public static partial class NetServerComponentSystem
{
[EntitySystem]
private static void Awake(this NetServerComponent self, IPEndPoint address)
{
self.AService = new KService(address, ServiceType.Outer);
self.AService.AcceptCallback = self.OnAccept;
self.AService.ReadCallback = self.OnRead;
self.AService.ErrorCallback = self.OnError;
}
[EntitySystem]
private static void Update(this NetServerComponent self)
{
self.AService.Update();
}
[EntitySystem]
private static void Destroy(this NetServerComponent self)
{
self.AService.Dispose();
}
private static void OnError(this NetServerComponent self, long channelId, int error)
{
Session session = self.GetChild<Session>(channelId);
if (session == null)
{
return;
}
session.Error = error;
session.Dispose();
}
// 这个channelId是由CreateAcceptChannelId生成的
private static void OnAccept(this NetServerComponent self, long channelId, IPEndPoint ipEndPoint)
{
Session session = self.AddChildWithId<Session, AService>(channelId, self.AService);
session.RemoteAddress = ipEndPoint;
if (self.IScene.SceneType != SceneType.BenchmarkServer)
{
// 挂上这个组件5秒就会删除session所以客户端验证完成要删除这个组件。该组件的作用就是防止外挂一直连接不发消息也不进行权限验证
session.AddComponent<SessionAcceptTimeoutComponent>();
// 客户端连接2秒检查一次recv消息10秒没有消息则断开
session.AddComponent<SessionIdleCheckerComponent>();
}
}
private static void OnRead(this NetServerComponent self, long channelId, ActorId actorId, object message)
{
Session session = self.GetChild<Session>(channelId);
if (session == null)
{
return;
}
session.LastRecvTime = self.Fiber().TimeInfo.ClientNow();
Log.Debug(message.ToJson());
EventSystem.Instance.Publish(self.Scene(), new NetServerComponentOnRead() {Session = session, Message = message});
}
}
}