NetInnerComponent.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. this.MessageDispatcher = new InnerMessageDispatcher();
  28. }
  29. public void Awake(string host, int port)
  30. {
  31. this.Awake(NetworkProtocol.TCP, host, port);
  32. this.MessagePacker = new MongoPacker();
  33. this.MessageDispatcher = new InnerMessageDispatcher();
  34. }
  35. public new void Update()
  36. {
  37. base.Update();
  38. }
  39. public override void Remove(long id)
  40. {
  41. Session session = this.Get(id);
  42. if (session == null)
  43. {
  44. return;
  45. }
  46. this.adressSessions.Remove(session.RemoteAddress);
  47. base.Remove(id);
  48. }
  49. /// <summary>
  50. /// 从地址缓存中取Session,如果没有则创建一个新的Session,并且保存到地址缓存中
  51. /// </summary>
  52. public Session Get(string address)
  53. {
  54. if (this.adressSessions.TryGetValue(address, out Session session))
  55. {
  56. return session;
  57. }
  58. session = this.Create(address);
  59. this.adressSessions.Add(address, session);
  60. return session;
  61. }
  62. }
  63. }