NetworkComponent.cs 2.8 KB

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