NetworkComponent.cs 3.3 KB

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