NetInnerComponent.cs 1.0 KB

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