NetInnerComponent.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System.Collections.Generic;
  2. namespace ETModel
  3. {
  4. public class NetInnerComponent: NetworkComponent
  5. {
  6. public static NetInnerComponent Instance;
  7. public readonly Dictionary<string, Session> adressSessions = new Dictionary<string, Session>();
  8. public override Session OnAccept(AChannel channel)
  9. {
  10. Session session = base.OnAccept(channel);
  11. // 内网accept连接,一分钟检查一次,20分钟没有收到消息则断开, 主要是防止连接过来的机器宕机,导致无法超时主动断开,这里检测时间是连接方的两倍
  12. session.AddComponent<SessionIdleCheckerComponent, int, int, int>(60 * 1000, 1000 * 60 * 20, int.MaxValue);
  13. return session;
  14. }
  15. public override void Remove(long id)
  16. {
  17. Session session = this.Get(id);
  18. if (session == null)
  19. {
  20. return;
  21. }
  22. this.adressSessions.Remove(session.RemoteAddress);
  23. base.Remove(id);
  24. }
  25. /// <summary>
  26. /// 从地址缓存中取Session,如果没有则创建一个新的Session,并且保存到地址缓存中
  27. /// </summary>
  28. public Session Get(string addr)
  29. {
  30. if (this.adressSessions.TryGetValue(addr, out Session session))
  31. {
  32. return session;
  33. }
  34. session = this.Create(addr);
  35. this.adressSessions.Add(addr, session);
  36. // 内网connect连接,一分钟检查一次,10分钟没有收到发送消息则断开
  37. session.AddComponent<SessionIdleCheckerComponent, int, int, int>(60 * 1000, int.MaxValue, 60 * 1000);
  38. return session;
  39. }
  40. }
  41. }