NetworkComponent.cs 4.3 KB

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