NetworkComponent.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. namespace ETModel
  6. {
  7. public abstract class NetworkComponent : Component
  8. {
  9. public AppType AppType;
  10. private AService Service;
  11. private readonly Dictionary<long, Session> sessions = new Dictionary<long, Session>();
  12. public IMessagePacker MessagePacker { get; set; }
  13. public IMessageDispatcher MessageDispatcher { get; set; }
  14. public void Awake(NetworkProtocol protocol)
  15. {
  16. try
  17. {
  18. switch (protocol)
  19. {
  20. case NetworkProtocol.KCP:
  21. this.Service = new KService();
  22. break;
  23. case NetworkProtocol.TCP:
  24. this.Service = new TService();
  25. break;
  26. default:
  27. throw new ArgumentOutOfRangeException();
  28. }
  29. this.Service.AcceptCallback += this.OnAccept;
  30. }
  31. catch (Exception e)
  32. {
  33. throw new Exception($"{e}");
  34. }
  35. }
  36. public void Awake(NetworkProtocol protocol, IPEndPoint ipEndPoint)
  37. {
  38. try
  39. {
  40. switch (protocol)
  41. {
  42. case NetworkProtocol.KCP:
  43. this.Service = new KService(ipEndPoint);
  44. break;
  45. case NetworkProtocol.TCP:
  46. this.Service = new TService(ipEndPoint);
  47. break;
  48. default:
  49. throw new ArgumentOutOfRangeException();
  50. }
  51. this.Service.AcceptCallback += this.OnAccept;
  52. }
  53. catch (Exception e)
  54. {
  55. throw new Exception($"{ipEndPoint}", e);
  56. }
  57. }
  58. public void Start()
  59. {
  60. this.Service.Start();
  61. }
  62. public void OnAccept(AChannel channel)
  63. {
  64. Session session = ComponentFactory.CreateWithParent<Session, NetworkComponent, AChannel>(this, this, channel);
  65. this.sessions.Add(session.Id, session);
  66. }
  67. public virtual void Remove(long id)
  68. {
  69. Session session;
  70. if (!this.sessions.TryGetValue(id, out session))
  71. {
  72. return;
  73. }
  74. this.sessions.Remove(id);
  75. session.Dispose();
  76. }
  77. public Session Get(long id)
  78. {
  79. Session session;
  80. this.sessions.TryGetValue(id, out session);
  81. return session;
  82. }
  83. /// <summary>
  84. /// 创建一个新Session
  85. /// </summary>
  86. public Session Create(IPEndPoint ipEndPoint)
  87. {
  88. AChannel channel = this.Service.ConnectChannel(ipEndPoint);
  89. Session session = ComponentFactory.CreateWithParent<Session, NetworkComponent, AChannel>(this, this, channel);
  90. this.sessions.Add(session.Id, session);
  91. return session;
  92. }
  93. public void Update()
  94. {
  95. if (this.Service == null)
  96. {
  97. return;
  98. }
  99. this.Service.Update();
  100. }
  101. public override void Dispose()
  102. {
  103. if (this.IsDisposed)
  104. {
  105. return;
  106. }
  107. base.Dispose();
  108. foreach (Session session in this.sessions.Values.ToArray())
  109. {
  110. session.Dispose();
  111. }
  112. this.Service.Dispose();
  113. }
  114. }
  115. }