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