NetworkComponent.cs 2.9 KB

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