NetworkComponent.cs 3.1 KB

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