NetworkComponent.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  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. protected void Awake(NetworkProtocol protocol)
  12. {
  13. switch (protocol)
  14. {
  15. case NetworkProtocol.TCP:
  16. this.Service = new TService();
  17. break;
  18. case NetworkProtocol.UDP:
  19. this.Service = new UService();
  20. break;
  21. default:
  22. throw new ArgumentOutOfRangeException();
  23. }
  24. }
  25. protected void Awake(NetworkProtocol protocol, string host, int port)
  26. {
  27. switch (protocol)
  28. {
  29. case NetworkProtocol.TCP:
  30. this.Service = new TService(host, port);
  31. break;
  32. case NetworkProtocol.UDP:
  33. this.Service = new UService(host, port);
  34. break;
  35. default:
  36. throw new ArgumentOutOfRangeException();
  37. }
  38. this.StartAccept();
  39. }
  40. private async void StartAccept()
  41. {
  42. while (true)
  43. {
  44. if (this.Id == 0)
  45. {
  46. return;
  47. }
  48. await this.Accept();
  49. }
  50. }
  51. private async Task<Session> Accept()
  52. {
  53. AChannel channel = await this.Service.AcceptChannel();
  54. Session session = new Session(this, channel);
  55. channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
  56. this.sessions.Add(session.Id, session);
  57. return session;
  58. }
  59. public virtual void Remove(long id)
  60. {
  61. Session session;
  62. if (!this.sessions.TryGetValue(id, out session))
  63. {
  64. return;
  65. }
  66. this.sessions.Remove(id);
  67. session.Dispose();
  68. }
  69. public Session Get(long id)
  70. {
  71. Session session;
  72. this.sessions.TryGetValue(id, out session);
  73. return session;
  74. }
  75. /// <summary>
  76. /// 创建一个新Session
  77. /// </summary>
  78. public Session Create(string address)
  79. {
  80. string[] ss = address.Split(':');
  81. int port = int.Parse(ss[1]);
  82. string host = ss[0];
  83. AChannel channel = this.Service.ConnectChannel(host, port);
  84. Session session = new Session(this, channel);
  85. channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
  86. this.sessions.Add(session.Id, session);
  87. return session;
  88. }
  89. protected void Update()
  90. {
  91. if (this.Service == null)
  92. {
  93. return;
  94. }
  95. this.Service.Update();
  96. }
  97. public override void Dispose()
  98. {
  99. if (this.Id == 0)
  100. {
  101. return;
  102. }
  103. base.Dispose();
  104. foreach (Session session in this.sessions.Values.ToArray())
  105. {
  106. session.Dispose();
  107. }
  108. this.Service.Dispose();
  109. }
  110. }
  111. }