NetInnerComponent.cs 1.1 KB

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