NetInnerComponent.cs 1.3 KB

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