123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- using System.Net;
- namespace ET.Server
- {
- [FriendOf(typeof(NetServerComponent))]
- public static class NetServerComponentSystem
- {
- [ObjectSystem]
- public class AwakeSystem: AwakeSystem<NetServerComponent, IPEndPoint>
- {
- protected override void Awake(NetServerComponent self, IPEndPoint address)
- {
- self.ServiceId = NetServices.Instance.AddService(new KService(address, ServiceType.Outer));
- NetServices.Instance.RegisterAcceptCallback(self.ServiceId, self.OnAccept);
- NetServices.Instance.RegisterReadCallback(self.ServiceId, self.OnRead);
- NetServices.Instance.RegisterErrorCallback(self.ServiceId, self.OnError);
- }
- }
- [ObjectSystem]
- public class NetKcpComponentDestroySystem: DestroySystem<NetServerComponent>
- {
- protected override void Destroy(NetServerComponent self)
- {
- NetServices.Instance.RemoveService(self.ServiceId);
- }
- }
- 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();
- }
-
- private static void OnAccept(this NetServerComponent self, long channelId, IPEndPoint ipEndPoint)
- {
- Session session = self.AddChildWithId<Session, int>(channelId, self.ServiceId);
- session.RemoteAddress = ipEndPoint;
- if (self.DomainScene().SceneType == SceneType.BenchmarkServer)
- {
- return;
- }
-
- session.AddComponent<SessionAcceptTimeoutComponent>();
-
- if (ConstValue.SessionTimeoutTime > 0)
- {
- #pragma warning disable CS0162 // 检测到无法访问的代码
- session.AddComponent<SessionIdleCheckerComponent>();
- #pragma warning restore CS0162 // 检测到无法访问的代码
- }
- }
- private static void OnRead(this NetServerComponent self, long channelId, long actorId, object message)
- {
- Session session = self.GetChild<Session>(channelId);
- if (session == null)
- {
- return;
- }
- session.LastRecvTime = TimeHelper.ClientNow();
- OpcodeHelper.LogMsg(self.DomainZone(), message);
- EventSystem.Instance.Publish(NetServerComponentOnRead.Clone(session, message));
- }
- }
- }
|