NetworkComponent.cs 3.6 KB

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