NetworkComponent.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. channel.ReadCallback += (packet) => { session.OnRead(packet); };
  80. this.sessions.Add(session.Id, session);
  81. return session;
  82. }
  83. public virtual void Remove(long id)
  84. {
  85. Session session;
  86. if (!this.sessions.TryGetValue(id, out session))
  87. {
  88. return;
  89. }
  90. this.sessions.Remove(id);
  91. session.Dispose();
  92. }
  93. public Session Get(long id)
  94. {
  95. Session session;
  96. this.sessions.TryGetValue(id, out session);
  97. return session;
  98. }
  99. /// <summary>
  100. /// 创建一个新Session
  101. /// </summary>
  102. public virtual Session Create(IPEndPoint ipEndPoint)
  103. {
  104. try
  105. {
  106. AChannel channel = this.Service.ConnectChannel(ipEndPoint);
  107. Session session = ComponentFactory.CreateWithId<Session, NetworkComponent, AChannel>(IdGenerater.GenerateId(), this, channel);
  108. session.Parent = this;
  109. channel.ErrorCallback += (c, e) =>
  110. {
  111. session.Error = e;
  112. this.Remove(session.Id);
  113. };
  114. channel.ReadCallback += (packet) => { session.OnRead(packet); };
  115. this.sessions.Add(session.Id, session);
  116. return session;
  117. }
  118. catch (Exception e)
  119. {
  120. Log.Error(e);
  121. return null;
  122. }
  123. }
  124. public void Update()
  125. {
  126. if (this.Service == null)
  127. {
  128. return;
  129. }
  130. this.Service.Update();
  131. }
  132. public override void Dispose()
  133. {
  134. if (this.IsDisposed)
  135. {
  136. return;
  137. }
  138. base.Dispose();
  139. foreach (Session session in this.sessions.Values.ToArray())
  140. {
  141. session.Dispose();
  142. }
  143. this.Service.Dispose();
  144. }
  145. }
  146. }