| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading.Tasks;
- namespace ET
- {
- [ObjectSystem]
- public class NetWSComponentAwakeSystem : AwakeSystem<NetWSComponent, int>
- {
- public override void Awake(NetWSComponent self, int sessionStreamDispatcherType)
- {
- self.SessionStreamDispatcherType = sessionStreamDispatcherType;
- self.AService = new WService();
- self.AService.ReadCallback = self.OnRead;
- self.AService.ErrorCallback = self.OnError;
- }
- }
- [ObjectSystem]
- public class NetWSComponentAwake1System : AwakeSystem<NetWSComponent, IEnumerable<string>>
- {
- public override void Awake(NetWSComponent self, IEnumerable<string> prefixs)
- {
- self.AService = new WService(prefixs);
- self.AService.ErrorCallback += (channelId, error) => self.OnError(channelId, error);
- self.AService.ReadCallback += (channelId, Memory) => self.OnRead(channelId, Memory);
- self.AService.AcceptCallback += (channelId, IPAddress) => self.OnAccept(channelId, IPAddress);
- NetThreadComponent.Instance.Add(self.AService);
- }
- }
- [ObjectSystem]
- public class NetWSComponentDestroySystem : DestroySystem<NetWSComponent>
- {
- public override void Destroy(NetWSComponent self)
- {
- self.AService.Dispose();
- }
- }
- public static class NetWSComponentSystem
- {
- public static void OnRead(this NetWSComponent self, long channelId, MemoryStream memoryStream)
- {
- Session session = self.GetChild<Session>(channelId);
- if (session == null)
- {
- return;
- }
- session.LastRecvTime = TimeHelper.ClientNow();
- SessionStreamDispatcher.Instance.Dispatch(self.SessionStreamDispatcherType, session, memoryStream);
- }
- public static void OnError(this NetWSComponent self, long channelId, int error)
- {
- Session session = self.GetChild<Session>(channelId);
- if (session == null)
- {
- return;
- }
- session.Error = error;
- session.Dispose();
- }
- // 这个channelId是由CreateAcceptChannelId生成的
- public static void OnAccept(this NetWSComponent self, long channelId, IPEndPoint ipEndPoint)
- {
- Session session = self.AddChildWithId<Session, AService>(channelId, self.AService);
- session.RemoteAddress = ipEndPoint;
- // 挂上这个组件,5秒就会删除session,所以客户端验证完成要删除这个组件。该组件的作用就是防止外挂一直连接不发消息也不进行权限验证
- session.AddComponent<SessionAcceptTimeoutComponent>();
- // 客户端连接,2秒检查一次recv消息,10秒没有消息则断开
- session.AddComponent<SessionIdleCheckerComponent, int>(NetThreadComponent.checkInteral);
- }
- public static Session Get(this NetWSComponent self, long id)
- {
- Session session = self.GetChild<Session>(id);
- return session;
- }
- public static Session Create(this NetWSComponent self, IPEndPoint realIPEndPoint, string address)
- {
- long channelId = RandomHelper.RandInt64();
- Session session = self.AddChildWithId<Session, AService>(channelId, self.AService);
- session.RemoteAddress = new IPEndPoint(IPAddress.Parse("0.0.0.0"), 0);
- session.AddComponent<SessionIdleCheckerComponent, int>(NetThreadComponent.checkInteral);
- self.AService.Create(session.Id, realIPEndPoint, address);
- return session;
- }
- }
- }
|