NetInnerComponent.cs 957 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System.Collections.Generic;
  2. using System.Net;
  3. namespace Model
  4. {
  5. public class NetInnerComponent: NetworkComponent
  6. {
  7. public readonly Dictionary<string, Session> adressSessions = new Dictionary<string, 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.ToString());
  16. base.Remove(id);
  17. }
  18. /// <summary>
  19. /// 从地址缓存中取Session,如果没有则创建一个新的Session,并且保存到地址缓存中
  20. /// </summary>
  21. public Session Get(string address)
  22. {
  23. if (this.adressSessions.TryGetValue(address, out Session session))
  24. {
  25. return session;
  26. }
  27. string[] ss = address.Split(':');
  28. IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse(ss[0]), int.Parse(ss[1]));
  29. session = this.Create(ipEndPoint);
  30. this.adressSessions.Add(address, session);
  31. return session;
  32. }
  33. }
  34. }