NetworkComponent.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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; protected set; }
  12. public IMessageDispatcher MessageDispatcher { get; protected set; }
  13. protected 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. protected void Awake(NetworkProtocol protocol, string host, int port)
  28. {
  29. switch (protocol)
  30. {
  31. case NetworkProtocol.TCP:
  32. this.Service = new TService(host, port);
  33. break;
  34. case NetworkProtocol.UDP:
  35. this.Service = new UService(host, port);
  36. break;
  37. default:
  38. throw new ArgumentOutOfRangeException();
  39. }
  40. this.StartAccept();
  41. }
  42. private async void StartAccept()
  43. {
  44. while (true)
  45. {
  46. if (this.Id == 0)
  47. {
  48. return;
  49. }
  50. await this.Accept();
  51. }
  52. }
  53. public virtual async Task<Session> Accept()
  54. {
  55. AChannel channel = await this.Service.AcceptChannel();
  56. Session session = new Session(this, channel);
  57. channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
  58. this.sessions.Add(session.Id, session);
  59. return session;
  60. }
  61. public virtual void Remove(long id)
  62. {
  63. Session session;
  64. if (!this.sessions.TryGetValue(id, out session))
  65. {
  66. return;
  67. }
  68. this.sessions.Remove(id);
  69. session.Dispose();
  70. }
  71. public Session Get(long id)
  72. {
  73. Session session;
  74. this.sessions.TryGetValue(id, out session);
  75. return session;
  76. }
  77. /// <summary>
  78. /// 创建一个新Session
  79. /// </summary>
  80. public virtual Session Create(string address)
  81. {
  82. string[] ss = address.Split(':');
  83. int port = int.Parse(ss[1]);
  84. string host = ss[0];
  85. AChannel channel = this.Service.ConnectChannel(host, port);
  86. Session session = new Session(this, channel);
  87. channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
  88. this.sessions.Add(session.Id, session);
  89. return session;
  90. }
  91. protected void Update()
  92. {
  93. if (this.Service == null)
  94. {
  95. return;
  96. }
  97. this.Service.Update();
  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. }
  113. }