NetworkComponent.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Sockets;
  5. using Base;
  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. private readonly Dictionary<string, Session> adressSessions = new Dictionary<string, Session>();
  13. private event Action<Session> removeCallback;
  14. public event Action<Session> RemoveCallback
  15. {
  16. add
  17. {
  18. this.removeCallback += value;
  19. }
  20. remove
  21. {
  22. this.removeCallback -= value;
  23. }
  24. }
  25. protected void Awake(NetworkProtocol protocol)
  26. {
  27. switch (protocol)
  28. {
  29. case NetworkProtocol.TCP:
  30. this.Service = new TService();
  31. break;
  32. case NetworkProtocol.UDP:
  33. this.Service = new UService();
  34. break;
  35. default:
  36. throw new ArgumentOutOfRangeException();
  37. }
  38. }
  39. protected void Awake(NetworkProtocol protocol, string host, int port)
  40. {
  41. switch (protocol)
  42. {
  43. case NetworkProtocol.TCP:
  44. this.Service = new TService(host, port);
  45. break;
  46. case NetworkProtocol.UDP:
  47. this.Service = new UService(host, port);
  48. break;
  49. default:
  50. throw new ArgumentOutOfRangeException();
  51. }
  52. this.StartAccept();
  53. }
  54. private async void StartAccept()
  55. {
  56. while (true)
  57. {
  58. if (this.Id == 0)
  59. {
  60. return;
  61. }
  62. AChannel channel = await this.Service.AcceptChannel();
  63. Session session = new Session(channel);
  64. channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
  65. this.Add(session);
  66. }
  67. }
  68. private void Add(Session session)
  69. {
  70. this.sessions.Add(session.Id, session);
  71. this.AddToAddressDict(session);
  72. }
  73. private void AddToAddressDict(Session session)
  74. {
  75. Session s;
  76. if (this.adressSessions.TryGetValue(session.RemoteAddress, out s))
  77. {
  78. this.Remove(s.Id);
  79. Log.Warning($"session 地址冲突, 可能是客户端断开, 服务器还没检测到!: {session.RemoteAddress}");
  80. }
  81. this.adressSessions.Add(session.RemoteAddress, session);
  82. }
  83. public void Remove(long id)
  84. {
  85. Session session;
  86. if (!this.sessions.TryGetValue(id, out session))
  87. {
  88. return;
  89. }
  90. removeCallback.Invoke(session);
  91. this.sessions.Remove(id);
  92. this.adressSessions.Remove(session.RemoteAddress);
  93. session.Dispose();
  94. }
  95. public Session Get(long id)
  96. {
  97. Session session;
  98. this.sessions.TryGetValue(id, out session);
  99. return session;
  100. }
  101. /// <summary>
  102. /// 从地址缓存中取Session,如果没有则创建一个新的Session,并且保存到地址缓存中
  103. /// </summary>
  104. public Session Get(string address)
  105. {
  106. Session session;
  107. if (this.adressSessions.TryGetValue(address, out session))
  108. {
  109. return session;
  110. }
  111. session = this.GetNew(address);
  112. this.AddToAddressDict(session);
  113. return session;
  114. }
  115. /// <summary>
  116. /// 创建一个新Session,不保存到地址缓存中
  117. /// </summary>
  118. public Session GetNew(string address)
  119. {
  120. string[] ss = address.Split(':');
  121. int port = int.Parse(ss[1]);
  122. string host = ss[0];
  123. AChannel channel = this.Service.ConnectChannel(host, port);
  124. Session session = new Session(channel);
  125. channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
  126. return session;
  127. }
  128. public override void Dispose()
  129. {
  130. if (this.Id == 0)
  131. {
  132. return;
  133. }
  134. base.Dispose();
  135. foreach (Session session in this.sessions.Values.ToArray())
  136. {
  137. session.Dispose();
  138. }
  139. this.Service.Dispose();
  140. }
  141. public void Update()
  142. {
  143. if (this.Service == null)
  144. {
  145. return;
  146. }
  147. this.Service.Update();
  148. }
  149. }
  150. }