NetworkComponent.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 = session => { };
  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.sessions.Add(session.Id, session);
  65. this.AddToAddressDict(session);
  66. }
  67. }
  68. private void AddToAddressDict(Session session)
  69. {
  70. Session s;
  71. if (this.adressSessions.TryGetValue(session.RemoteAddress, out s))
  72. {
  73. this.Remove(s.Id);
  74. Log.Warning($"session 地址冲突, 可能是客户端断开, 服务器还没检测到!: {session.RemoteAddress}");
  75. }
  76. this.adressSessions.Add(session.RemoteAddress, session);
  77. }
  78. public void Remove(long id)
  79. {
  80. Session session;
  81. if (!this.sessions.TryGetValue(id, out session))
  82. {
  83. return;
  84. }
  85. removeCallback.Invoke(session);
  86. this.sessions.Remove(id);
  87. this.adressSessions.Remove(session.RemoteAddress);
  88. session.Dispose();
  89. }
  90. public Session Get(long id)
  91. {
  92. Session session;
  93. this.sessions.TryGetValue(id, out session);
  94. return session;
  95. }
  96. /// <summary>
  97. /// 从地址缓存中取Session,如果没有则创建一个新的Session,并且保存到地址缓存中
  98. /// </summary>
  99. public Session Get(string address)
  100. {
  101. Session session;
  102. if (this.adressSessions.TryGetValue(address, out session))
  103. {
  104. return session;
  105. }
  106. session = this.Create(address);
  107. this.AddToAddressDict(session);
  108. return session;
  109. }
  110. /// <summary>
  111. /// 创建一个新Session
  112. /// </summary>
  113. public Session Create(string address)
  114. {
  115. string[] ss = address.Split(':');
  116. int port = int.Parse(ss[1]);
  117. string host = ss[0];
  118. AChannel channel = this.Service.ConnectChannel(host, port);
  119. Session session = new Session(this.GetOwner<Scene>(), channel);
  120. channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
  121. this.sessions.Add(session.Id, session);
  122. return session;
  123. }
  124. public void Update()
  125. {
  126. if (this.Service == null)
  127. {
  128. return;
  129. }
  130. this.Service.Update();
  131. }
  132. public override void Dispose()
  133. {
  134. if (this.Id == 0)
  135. {
  136. return;
  137. }
  138. base.Dispose();
  139. foreach (Session session in this.sessions.Values.ToArray())
  140. {
  141. session.Dispose();
  142. }
  143. this.Service.Dispose();
  144. }
  145. }
  146. }