NetworkComponent.cs 3.3 KB

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