NetworkComponent.cs 2.8 KB

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