NetworkComponent.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.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.Id == 0)
  56. {
  57. return;
  58. }
  59. await this.Accept();
  60. }
  61. }
  62. public virtual async Task<Session> Accept()
  63. {
  64. AChannel channel = await this.Service.AcceptChannel();
  65. Session session = EntityFactory.Create<Session, NetworkComponent, AChannel>(this, channel);
  66. session.Parent = this;
  67. channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
  68. this.sessions.Add(session.Id, session);
  69. return session;
  70. }
  71. public virtual void Remove(long id)
  72. {
  73. Session session;
  74. if (!this.sessions.TryGetValue(id, out session))
  75. {
  76. return;
  77. }
  78. this.sessions.Remove(id);
  79. session.Dispose();
  80. }
  81. public Session Get(long id)
  82. {
  83. Session session;
  84. this.sessions.TryGetValue(id, out session);
  85. return session;
  86. }
  87. /// <summary>
  88. /// 创建一个新Session
  89. /// </summary>
  90. public virtual Session Create(IPEndPoint ipEndPoint)
  91. {
  92. try
  93. {
  94. AChannel channel = this.Service.ConnectChannel(ipEndPoint);
  95. Session session = EntityFactory.Create<Session, NetworkComponent, AChannel>(this, channel);
  96. session.Parent = this;
  97. channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
  98. this.sessions.Add(session.Id, session);
  99. return session;
  100. }
  101. catch (Exception e)
  102. {
  103. Log.Error(e.ToString());
  104. return null;
  105. }
  106. }
  107. public void Update()
  108. {
  109. if (this.Service == null)
  110. {
  111. return;
  112. }
  113. this.Service.Update();
  114. }
  115. public override void Dispose()
  116. {
  117. if (this.Id == 0)
  118. {
  119. return;
  120. }
  121. base.Dispose();
  122. foreach (Session session in this.sessions.Values.ToArray())
  123. {
  124. session.Dispose();
  125. }
  126. this.Service.Dispose();
  127. }
  128. }
  129. }