NetworkComponent.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. namespace ETModel
  6. {
  7. public abstract class NetworkComponent : Component
  8. {
  9. public AppType AppType;
  10. private AService Service;
  11. private readonly Dictionary<long, Session> sessions = new Dictionary<long, Session>();
  12. public IMessagePacker MessagePacker { get; set; }
  13. public IMessageDispatcher MessageDispatcher { get; set; }
  14. public void Awake(NetworkProtocol protocol)
  15. {
  16. try
  17. {
  18. switch (protocol)
  19. {
  20. case NetworkProtocol.KCP:
  21. this.Service = new KService();
  22. break;
  23. case NetworkProtocol.TCP:
  24. this.Service = new TService();
  25. break;
  26. default:
  27. throw new ArgumentOutOfRangeException();
  28. }
  29. this.Service.AcceptCallback += this.OnAccept;
  30. this.Start();
  31. }
  32. catch (Exception e)
  33. {
  34. throw new Exception($"{e}");
  35. }
  36. }
  37. public void Awake(NetworkProtocol protocol, IPEndPoint ipEndPoint)
  38. {
  39. try
  40. {
  41. switch (protocol)
  42. {
  43. case NetworkProtocol.KCP:
  44. this.Service = new KService(ipEndPoint);
  45. break;
  46. case NetworkProtocol.TCP:
  47. this.Service = new TService(ipEndPoint);
  48. break;
  49. default:
  50. throw new ArgumentOutOfRangeException();
  51. }
  52. this.Service.AcceptCallback += this.OnAccept;
  53. this.Start();
  54. }
  55. catch (Exception e)
  56. {
  57. throw new Exception($"{ipEndPoint}", e);
  58. }
  59. }
  60. public void Start()
  61. {
  62. this.Service.Start();
  63. }
  64. public int Count
  65. {
  66. get { return this.sessions.Count; }
  67. }
  68. public void OnAccept(AChannel channel)
  69. {
  70. Session session = ComponentFactory.CreateWithParent<Session, AChannel>(this, channel);
  71. this.sessions.Add(session.Id, session);
  72. }
  73. public virtual void Remove(long id)
  74. {
  75. Session session;
  76. if (!this.sessions.TryGetValue(id, out session))
  77. {
  78. return;
  79. }
  80. this.sessions.Remove(id);
  81. session.Dispose();
  82. }
  83. public Session Get(long id)
  84. {
  85. Session session;
  86. this.sessions.TryGetValue(id, out session);
  87. return session;
  88. }
  89. /// <summary>
  90. /// 创建一个新Session
  91. /// </summary>
  92. public Session Create(IPEndPoint ipEndPoint)
  93. {
  94. AChannel channel = this.Service.ConnectChannel(ipEndPoint);
  95. Session session = ComponentFactory.CreateWithParent<Session, AChannel>(this, channel);
  96. this.sessions.Add(session.Id, session);
  97. return session;
  98. }
  99. public void Update()
  100. {
  101. if (this.Service == null)
  102. {
  103. return;
  104. }
  105. this.Service.Update();
  106. }
  107. public override void Dispose()
  108. {
  109. if (this.IsDisposed)
  110. {
  111. return;
  112. }
  113. base.Dispose();
  114. foreach (Session session in this.sessions.Values.ToArray())
  115. {
  116. session.Dispose();
  117. }
  118. this.Service.Dispose();
  119. }
  120. }
  121. }