Files
TEngine/Assets/GameScripts/DotNet/Hotfix/Server/Module/Message/NetServerComponentSystem.cs
ALEXTANG 336d4b2eb9 [+] 接入ET8服务端
[+] 接入ET8服务端
2023-07-13 12:23:48 +08:00

72 lines
2.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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