NetworkComponent.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. protected void Awake(NetworkProtocol protocol)
  13. {
  14. switch (protocol)
  15. {
  16. case NetworkProtocol.TCP:
  17. this.Service = new TService();
  18. break;
  19. case NetworkProtocol.UDP:
  20. this.Service = new UService();
  21. break;
  22. default:
  23. throw new ArgumentOutOfRangeException();
  24. }
  25. }
  26. protected void Awake(NetworkProtocol protocol, string host, int port)
  27. {
  28. switch (protocol)
  29. {
  30. case NetworkProtocol.TCP:
  31. this.Service = new TService(host, port);
  32. break;
  33. case NetworkProtocol.UDP:
  34. this.Service = new UService(host, port);
  35. break;
  36. default:
  37. throw new ArgumentOutOfRangeException();
  38. }
  39. this.StartAccept();
  40. }
  41. private async void StartAccept()
  42. {
  43. while (true)
  44. {
  45. if (this.Id == 0)
  46. {
  47. return;
  48. }
  49. AChannel channel = await this.Service.AcceptChannel();
  50. Session session = new Session(channel);
  51. channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
  52. this.Add(session);
  53. }
  54. }
  55. private void Add(Session session)
  56. {
  57. Session s;
  58. if (this.adressSessions.TryGetValue(session.RemoteAddress, out s))
  59. {
  60. this.Remove(s.Id);
  61. Log.Warning($"session 地址冲突, 可能是客户端断开, 服务器还没检测到!: {session.RemoteAddress}");
  62. }
  63. this.sessions.Add(session.Id, session);
  64. this.adressSessions.Add(session.RemoteAddress, session);
  65. }
  66. public void Remove(long id)
  67. {
  68. Session session;
  69. if (!this.sessions.TryGetValue(id, out session))
  70. {
  71. return;
  72. }
  73. this.sessions.Remove(id);
  74. this.adressSessions.Remove(session.RemoteAddress);
  75. session.Dispose();
  76. }
  77. public Session Get(long id)
  78. {
  79. Session session;
  80. this.sessions.TryGetValue(id, out session);
  81. return session;
  82. }
  83. public Session Get(string address)
  84. {
  85. Session session;
  86. if (this.adressSessions.TryGetValue(address, out session))
  87. {
  88. return session;
  89. }
  90. string[] ss = address.Split(':');
  91. int port = int.Parse(ss[1]);
  92. string host = ss[0];
  93. AChannel channel = this.Service.ConnectChannel(host, port);
  94. session = new Session(channel);
  95. channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
  96. this.Add(session);
  97. return session;
  98. }
  99. public override void Dispose()
  100. {
  101. if (this.Id == 0)
  102. {
  103. return;
  104. }
  105. base.Dispose();
  106. foreach (Session session in this.sessions.Values.ToArray())
  107. {
  108. session.Dispose();
  109. }
  110. this.Service.Dispose();
  111. }
  112. public void Update()
  113. {
  114. if (this.Service == null)
  115. {
  116. return;
  117. }
  118. this.Service.Update();
  119. }
  120. }
  121. }