NetInnerComponent.cs 1.4 KB

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