NetInnerComponent.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System.Collections.Generic;
  2. using Base;
  3. namespace Model
  4. {
  5. [EntityEvent(EntityEventId.NetInnerComponent)]
  6. public class NetInnerComponent: NetworkComponent
  7. {
  8. private readonly Dictionary<string, Session> adressSessions = new Dictionary<string, Session>();
  9. private void Awake()
  10. {
  11. this.Awake(NetworkProtocol.TCP);
  12. }
  13. private void Awake(string host, int port)
  14. {
  15. this.Awake(NetworkProtocol.TCP, host, port);
  16. }
  17. private new void Update()
  18. {
  19. base.Update();
  20. }
  21. public override void Remove(long id)
  22. {
  23. Session session = this.Get(id);
  24. if (session == null)
  25. {
  26. return;
  27. }
  28. this.adressSessions.Remove(session.RemoteAddress);
  29. base.Remove(id);
  30. }
  31. /// <summary>
  32. /// 从地址缓存中取Session,如果没有则创建一个新的Session,并且保存到地址缓存中
  33. /// </summary>
  34. public Session Get(string address)
  35. {
  36. Session session;
  37. if (this.adressSessions.TryGetValue(address, out session))
  38. {
  39. return session;
  40. }
  41. session = this.Create(address);
  42. this.adressSessions.Add(address, session);
  43. return session;
  44. }
  45. }
  46. }