NetworkComponent.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  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. 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. await this.Accept();
  50. }
  51. }
  52. private async Task<Session> Accept()
  53. {
  54. AChannel channel = await this.Service.AcceptChannel();
  55. Session session = new Session(this, channel);
  56. channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
  57. this.sessions.Add(session.Id, session);
  58. return session;
  59. }
  60. public virtual void Remove(long id)
  61. {
  62. if (!this.sessions.TryGetValue(id, out Session session))
  63. {
  64. return;
  65. }
  66. this.sessions.Remove(id);
  67. session.Dispose();
  68. }
  69. public Session Get(long id)
  70. {
  71. this.sessions.TryGetValue(id, out Session session);
  72. return session;
  73. }
  74. /// <summary>
  75. /// 创建一个新Session
  76. /// </summary>
  77. public Session Create(string address)
  78. {
  79. string[] ss = address.Split(':');
  80. int port = int.Parse(ss[1]);
  81. string host = ss[0];
  82. AChannel channel = this.Service.ConnectChannel(host, port);
  83. Session session = new Session(this, channel);
  84. channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
  85. this.sessions.Add(session.Id, session);
  86. return session;
  87. }
  88. protected void Update()
  89. {
  90. if (this.Service == null)
  91. {
  92. return;
  93. }
  94. this.Service.Update();
  95. }
  96. public override void Dispose()
  97. {
  98. if (this.Id == 0)
  99. {
  100. return;
  101. }
  102. base.Dispose();
  103. foreach (Session session in this.sessions.Values.ToArray())
  104. {
  105. session.Dispose();
  106. }
  107. this.Service.Dispose();
  108. }
  109. }
  110. }