NetworkComponent.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. protected virtual 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. Session session;
  63. if (!this.sessions.TryGetValue(id, out session))
  64. {
  65. return;
  66. }
  67. this.sessions.Remove(id);
  68. session.Dispose();
  69. }
  70. public Session Get(long id)
  71. {
  72. Session session;
  73. this.sessions.TryGetValue(id, out session);
  74. return session;
  75. }
  76. /// <summary>
  77. /// 创建一个新Session
  78. /// </summary>
  79. public Session Create(string address)
  80. {
  81. string[] ss = address.Split(':');
  82. int port = int.Parse(ss[1]);
  83. string host = ss[0];
  84. AChannel channel = this.Service.ConnectChannel(host, port);
  85. Session session = new Session(this, channel);
  86. channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
  87. this.sessions.Add(session.Id, session);
  88. return session;
  89. }
  90. public void Update()
  91. {
  92. if (this.Service == null)
  93. {
  94. return;
  95. }
  96. this.Service.Update();
  97. }
  98. public override void Dispose()
  99. {
  100. if (this.Id == 0)
  101. {
  102. return;
  103. }
  104. base.Dispose();
  105. foreach (Session session in this.sessions.Values.ToArray())
  106. {
  107. session.Dispose();
  108. }
  109. this.Service.Dispose();
  110. }
  111. }
  112. }