NetInnerComponent.cs 848 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System.Collections.Generic;
  2. using System.Net;
  3. namespace Model
  4. {
  5. public class NetInnerComponent: NetworkComponent
  6. {
  7. public readonly Dictionary<IPEndPoint, Session> adressSessions = new Dictionary<IPEndPoint, Session>();
  8. public override void Remove(long id)
  9. {
  10. Session session = this.Get(id);
  11. if (session == null)
  12. {
  13. return;
  14. }
  15. this.adressSessions.Remove(session.RemoteAddress);
  16. base.Remove(id);
  17. }
  18. /// <summary>
  19. /// 从地址缓存中取Session,如果没有则创建一个新的Session,并且保存到地址缓存中
  20. /// </summary>
  21. public Session Get(IPEndPoint ipEndPoint)
  22. {
  23. if (this.adressSessions.TryGetValue(ipEndPoint, out Session session))
  24. {
  25. return session;
  26. }
  27. session = this.Create(ipEndPoint);
  28. this.adressSessions.Add(ipEndPoint, session);
  29. return session;
  30. }
  31. }
  32. }