NetworkComponent.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. }
  31. catch (Exception e)
  32. {
  33. throw new Exception($"{e}");
  34. }
  35. }
  36. public void Awake(NetworkProtocol protocol, IPEndPoint ipEndPoint)
  37. {
  38. try
  39. {
  40. switch (protocol)
  41. {
  42. case NetworkProtocol.KCP:
  43. this.Service = new KService(ipEndPoint);
  44. break;
  45. case NetworkProtocol.TCP:
  46. this.Service = new TService(ipEndPoint);
  47. break;
  48. default:
  49. throw new ArgumentOutOfRangeException();
  50. }
  51. this.Service.AcceptCallback += this.OnAccept;
  52. }
  53. catch (Exception e)
  54. {
  55. throw new Exception($"{ipEndPoint}", e);
  56. }
  57. }
  58. public void Start()
  59. {
  60. this.Service.Start();
  61. }
  62. public int Count
  63. {
  64. get { return this.sessions.Count; }
  65. }
  66. public void OnAccept(AChannel channel)
  67. {
  68. Session session = ComponentFactory.CreateWithParent<Session, AChannel>(this, channel);
  69. this.sessions.Add(session.Id, session);
  70. }
  71. public virtual void Remove(long id)
  72. {
  73. Session session;
  74. if (!this.sessions.TryGetValue(id, out session))
  75. {
  76. return;
  77. }
  78. this.sessions.Remove(id);
  79. session.Dispose();
  80. }
  81. public Session Get(long id)
  82. {
  83. Session session;
  84. this.sessions.TryGetValue(id, out session);
  85. return session;
  86. }
  87. /// <summary>
  88. /// 创建一个新Session
  89. /// </summary>
  90. public Session Create(IPEndPoint ipEndPoint)
  91. {
  92. AChannel channel = this.Service.ConnectChannel(ipEndPoint);
  93. Session session = ComponentFactory.CreateWithParent<Session, AChannel>(this, channel);
  94. this.sessions.Add(session.Id, session);
  95. return session;
  96. }
  97. public void Update()
  98. {
  99. if (this.Service == null)
  100. {
  101. return;
  102. }
  103. this.Service.Update();
  104. }
  105. public override void Dispose()
  106. {
  107. if (this.IsDisposed)
  108. {
  109. return;
  110. }
  111. base.Dispose();
  112. foreach (Session session in this.sessions.Values.ToArray())
  113. {
  114. session.Dispose();
  115. }
  116. this.Service.Dispose();
  117. }
  118. }
  119. }