NetworkComponent.cs 2.9 KB

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