TcpServiceBase.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //------------------------------------------------------------------------------
  2. // 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有
  3. // 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权
  4. // CSDN博客:https://blog.csdn.net/qq_40374647
  5. // 哔哩哔哩视频:https://space.bilibili.com/94253567
  6. // Gitee源代码仓库:https://gitee.com/RRQM_Home
  7. // Github源代码仓库:https://github.com/RRQM
  8. // API首页:https://www.yuque.com/rrqm/touchsocket/index
  9. // 交流QQ群:234762506
  10. // 感谢您的下载和使用
  11. //------------------------------------------------------------------------------
  12. //------------------------------------------------------------------------------
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Linq;
  16. using System.Threading.Tasks;
  17. using TouchSocket.Core;
  18. using TouchSocket.Resources;
  19. namespace TouchSocket.Sockets
  20. {
  21. /// <summary>
  22. /// Tcp服务器基类
  23. /// </summary>
  24. public abstract class TcpServiceBase : BaseSocket, ITcpService
  25. {
  26. /// <summary>
  27. /// <inheritdoc/>
  28. /// </summary>
  29. public abstract TouchSocketConfig Config { get; }
  30. /// <summary>
  31. /// <inheritdoc/>
  32. /// </summary>
  33. public abstract IContainer Container { get; }
  34. /// <summary>
  35. /// <inheritdoc/>
  36. /// </summary>
  37. public int Count => SocketClients.Count;
  38. /// <summary>
  39. /// <inheritdoc/>
  40. /// </summary>
  41. public abstract NetworkMonitor[] Monitors { get; }
  42. /// <summary>
  43. /// 插件管理器
  44. /// </summary>
  45. public abstract IPluginsManager PluginsManager { get; }
  46. /// <summary>
  47. /// <inheritdoc/>
  48. /// </summary>
  49. public abstract string ServerName { get; }
  50. /// <summary>
  51. /// <inheritdoc/>
  52. /// </summary>
  53. public abstract ServerState ServerState { get; }
  54. /// <summary>
  55. /// <inheritdoc/>
  56. /// </summary>
  57. public abstract SocketClientCollection SocketClients { get; }
  58. /// <summary>
  59. /// <inheritdoc/>
  60. /// </summary>
  61. public abstract bool UseSsl { get; }
  62. /// <summary>
  63. /// <inheritdoc/>
  64. /// </summary>
  65. public abstract Func<string> GetDefaultNewID { get; }
  66. /// <summary>
  67. /// <inheritdoc/>
  68. /// </summary>
  69. public abstract int MaxCount { get; }
  70. /// <summary>
  71. /// <inheritdoc/>
  72. /// </summary>
  73. public abstract bool UsePlugin { get; }
  74. /// <summary>
  75. /// <inheritdoc/>
  76. /// </summary>
  77. public abstract void Clear();
  78. /// <summary>
  79. /// <inheritdoc/>
  80. /// </summary>
  81. /// <returns></returns>
  82. public string[] GetIDs()
  83. {
  84. return SocketClients.GetIDs().ToArray();
  85. }
  86. /// <summary>
  87. /// <inheritdoc/>
  88. /// </summary>
  89. /// <param name="oldID"></param>
  90. /// <param name="newID"></param>
  91. public abstract void ResetID(string oldID, string newID);
  92. /// <summary>
  93. /// <inheritdoc/>
  94. /// </summary>
  95. /// <param name="serverConfig"></param>
  96. /// <returns></returns>
  97. public abstract IService Setup(TouchSocketConfig serverConfig);
  98. /// <summary>
  99. /// <inheritdoc/>
  100. /// </summary>
  101. /// <param name="port"></param>
  102. /// <returns></returns>
  103. public abstract IService Setup(int port);
  104. /// <summary>
  105. /// <inheritdoc/>
  106. /// </summary>
  107. /// <returns></returns>
  108. public abstract IService Start();
  109. /// <summary>
  110. /// <inheritdoc/>
  111. /// </summary>
  112. /// <returns></returns>
  113. public abstract IService Stop();
  114. internal void OnInternalConnected(ISocketClient socketClient, TouchSocketEventArgs e)
  115. {
  116. OnClientConnected(socketClient, e);
  117. }
  118. internal void OnInternalConnecting(ISocketClient socketClient, OperationEventArgs e)
  119. {
  120. OnClientConnecting(socketClient, e);
  121. }
  122. internal void OnInternalDisconnected(ISocketClient socketClient, DisconnectEventArgs e)
  123. {
  124. OnClientDisconnected(socketClient, e);
  125. }
  126. internal void OnInternalDisconnecting(ISocketClient socketClient, DisconnectEventArgs e)
  127. {
  128. OnClientDisconnecting(socketClient, e);
  129. }
  130. internal void OnInternalReceivedData(ISocketClient socketClient, ByteBlock byteBlock, IRequestInfo requestInfo)
  131. {
  132. OnClientReceivedData(socketClient, byteBlock, requestInfo);
  133. }
  134. /// <summary>
  135. /// 客户端连接完成
  136. /// </summary>
  137. /// <param name="socketClient"></param>
  138. /// <param name="e"></param>
  139. protected abstract void OnClientConnected(ISocketClient socketClient, TouchSocketEventArgs e);
  140. /// <summary>
  141. /// 客户端请求连接
  142. /// </summary>
  143. /// <param name="socketClient"></param>
  144. /// <param name="e"></param>
  145. protected abstract void OnClientConnecting(ISocketClient socketClient, OperationEventArgs e);
  146. /// <summary>
  147. /// 客户端断开连接
  148. /// </summary>
  149. /// <param name="socketClient"></param>
  150. /// <param name="e"></param>
  151. protected abstract void OnClientDisconnected(ISocketClient socketClient, DisconnectEventArgs e);
  152. /// <summary>
  153. /// 即将断开连接(仅主动断开时有效)。
  154. /// <para>
  155. /// 当主动调用Close断开时,可通过<see cref="TouchSocketEventArgs.IsPermitOperation"/>终止断开行为。
  156. /// </para>
  157. /// </summary>
  158. /// <param name="socketClient"></param>
  159. /// <param name="e"></param>
  160. protected abstract void OnClientDisconnecting(ISocketClient socketClient, DisconnectEventArgs e);
  161. /// <summary>
  162. /// 收到数据时
  163. /// </summary>
  164. /// <param name="socketClient"></param>
  165. /// <param name="byteBlock"></param>
  166. /// <param name="requestInfo"></param>
  167. protected abstract void OnClientReceivedData(ISocketClient socketClient, ByteBlock byteBlock, IRequestInfo requestInfo);
  168. #region ID发送
  169. /// <summary>
  170. /// 发送字节流
  171. /// </summary>
  172. /// <param name="id">用于检索TcpSocketClient</param>
  173. /// <param name="buffer"></param>
  174. /// <param name="offset"></param>
  175. /// <param name="length"></param>
  176. /// <exception cref="KeyNotFoundException"></exception>
  177. /// <exception cref="NotConnectedException"></exception>
  178. /// <exception cref="OverlengthException"></exception>
  179. /// <exception cref="Exception"></exception>
  180. public void Send(string id, byte[] buffer, int offset, int length)
  181. {
  182. if (SocketClients.TryGetSocketClient(id, out ISocketClient client))
  183. {
  184. client.Send(buffer, offset, length);
  185. }
  186. else
  187. {
  188. throw new ClientNotFindException(TouchSocketStatus.ClientNotFind.GetDescription(id));
  189. }
  190. }
  191. /// <summary>
  192. /// <inheritdoc/>
  193. /// </summary>
  194. /// <param name="id"></param>
  195. /// <param name="requestInfo"></param>
  196. public void Send(string id, IRequestInfo requestInfo)
  197. {
  198. if (SocketClients.TryGetSocketClient(id, out ISocketClient client))
  199. {
  200. client.Send(requestInfo);
  201. }
  202. else
  203. {
  204. throw new ClientNotFindException(TouchSocketStatus.ClientNotFind.GetDescription(id));
  205. }
  206. }
  207. /// <summary>
  208. /// 发送字节流
  209. /// </summary>
  210. /// <param name="id">用于检索TcpSocketClient</param>
  211. /// <param name="buffer"></param>
  212. /// <param name="offset"></param>
  213. /// <param name="length"></param>
  214. /// <exception cref="KeyNotFoundException"></exception>
  215. /// <exception cref="NotConnectedException"></exception>
  216. /// <exception cref="OverlengthException"></exception>
  217. /// <exception cref="Exception"></exception>
  218. public Task SendAsync(string id, byte[] buffer, int offset, int length)
  219. {
  220. if (SocketClients.TryGetSocketClient(id, out ISocketClient client))
  221. {
  222. return client.SendAsync(buffer, offset, length);
  223. }
  224. else
  225. {
  226. throw new ClientNotFindException(TouchSocketStatus.ClientNotFind.GetDescription(id));
  227. }
  228. }
  229. /// <summary>
  230. /// <inheritdoc/>
  231. /// </summary>
  232. /// <param name="id"></param>
  233. /// <param name="requestInfo"></param>
  234. public Task SendAsync(string id, IRequestInfo requestInfo)
  235. {
  236. if (SocketClients.TryGetSocketClient(id, out ISocketClient client))
  237. {
  238. return client.SendAsync(requestInfo);
  239. }
  240. else
  241. {
  242. throw new ClientNotFindException(TouchSocketStatus.ClientNotFind.GetDescription(id));
  243. }
  244. }
  245. /// <summary>
  246. /// <inheritdoc/>
  247. /// </summary>
  248. /// <param name="id"></param>
  249. /// <returns></returns>
  250. public abstract bool SocketClientExist(string id);
  251. #endregion ID发送
  252. }
  253. }