NetInnerComponent.cs 1.3 KB

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