NetworkComponent.cs 3.5 KB

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