NetworkComponent.cs 2.9 KB

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