NetworkComponent.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. case NetworkProtocol.WebSocket:
  27. this.Service = new WService();
  28. break;
  29. }
  30. }
  31. catch (Exception e)
  32. {
  33. throw new Exception($"{e}");
  34. }
  35. }
  36. public void Awake(NetworkProtocol protocol, string address)
  37. {
  38. try
  39. {
  40. IPEndPoint ipEndPoint;
  41. switch (protocol)
  42. {
  43. case NetworkProtocol.KCP:
  44. ipEndPoint = NetworkHelper.ToIPEndPoint(address);
  45. this.Service = new KService(ipEndPoint, this.OnAccept);
  46. break;
  47. case NetworkProtocol.TCP:
  48. ipEndPoint = NetworkHelper.ToIPEndPoint(address);
  49. this.Service = new TService(ipEndPoint, this.OnAccept);
  50. break;
  51. case NetworkProtocol.WebSocket:
  52. string[] prefixs = address.Split(';');
  53. this.Service = new WService(prefixs, this.OnAccept);
  54. break;
  55. }
  56. }
  57. catch (Exception e)
  58. {
  59. throw new Exception($"NetworkComponent Awake Error {address}", e);
  60. }
  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. /// <summary>
  98. /// 创建一个新Session
  99. /// </summary>
  100. public Session Create(string address)
  101. {
  102. AChannel channel = this.Service.ConnectChannel(address);
  103. Session session = ComponentFactory.CreateWithParent<Session, AChannel>(this, channel);
  104. this.sessions.Add(session.Id, session);
  105. return session;
  106. }
  107. public void Update()
  108. {
  109. if (this.Service == null)
  110. {
  111. return;
  112. }
  113. this.Service.Update();
  114. }
  115. public override void Dispose()
  116. {
  117. if (this.IsDisposed)
  118. {
  119. return;
  120. }
  121. base.Dispose();
  122. foreach (Session session in this.sessions.Values.ToArray())
  123. {
  124. session.Dispose();
  125. }
  126. this.Service.Dispose();
  127. }
  128. }
  129. }