NetworkComponent.cs 3.1 KB

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