NATSocketClient.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.Threading.Tasks;
  15. using TouchSocket.Core;
  16. namespace TouchSocket.Sockets
  17. {
  18. /// <summary>
  19. /// 端口转发辅助
  20. /// </summary>
  21. public class NATSocketClient : SocketClient
  22. {
  23. internal Action<NATSocketClient, ITcpClient, DisconnectEventArgs> m_internalDis;
  24. internal Func<NATSocketClient, ITcpClient, ByteBlock, IRequestInfo, byte[]> m_internalTargetClientRev;
  25. private readonly ConcurrentList<ITcpClient> m_targetClients = new ConcurrentList<ITcpClient>();
  26. /// <summary>
  27. /// 添加转发客户端。
  28. /// </summary>
  29. /// <param name="config">配置文件</param>
  30. /// <param name="setupAction">当完成配置,但是还未连接时回调。</param>
  31. /// <returns></returns>
  32. public ITcpClient AddTargetClient(TouchSocketConfig config, Action<ITcpClient> setupAction = default)
  33. {
  34. TcpClient tcpClient = new TcpClient();
  35. tcpClient.Disconnected += TcpClient_Disconnected;
  36. tcpClient.Received += TcpClient_Received;
  37. tcpClient.Setup(config);
  38. setupAction?.Invoke(tcpClient);
  39. tcpClient.Connect();
  40. m_targetClients.Add(tcpClient);
  41. return tcpClient;
  42. }
  43. /// <summary>
  44. /// 添加转发客户端。
  45. /// </summary>
  46. /// <param name="config">配置文件</param>
  47. /// <param name="setupAction">当完成配置,但是还未连接时回调。</param>
  48. /// <returns></returns>
  49. public Task<ITcpClient> AddTargetClientAsync(TouchSocketConfig config, Action<ITcpClient> setupAction = default)
  50. {
  51. return EasyTask.Run(() =>
  52. {
  53. return AddTargetClient(config, setupAction);
  54. });
  55. }
  56. /// <summary>
  57. /// 获取所有目标客户端
  58. /// </summary>
  59. /// <returns></returns>
  60. public ITcpClient[] GetTargetClients()
  61. {
  62. return m_targetClients.ToArray();
  63. }
  64. /// <summary>
  65. /// 发送数据到全部转发端。
  66. /// </summary>
  67. /// <param name="buffer"></param>
  68. /// <param name="offset"></param>
  69. /// <param name="length"></param>
  70. public void SendToTargetClient(byte[] buffer, int offset, int length)
  71. {
  72. foreach (var socket in m_targetClients)
  73. {
  74. try
  75. {
  76. socket.Send(buffer, offset, length);
  77. }
  78. catch
  79. {
  80. }
  81. }
  82. }
  83. /// <summary>
  84. /// <inheritdoc/>
  85. /// </summary>
  86. /// <param name="e"></param>
  87. protected override void OnDisconnected(DisconnectEventArgs e)
  88. {
  89. foreach (var client in m_targetClients)
  90. {
  91. client.TryShutdown();
  92. client.SafeDispose();
  93. }
  94. base.OnDisconnected(e);
  95. }
  96. private void TcpClient_Disconnected(ITcpClientBase client, DisconnectEventArgs e)
  97. {
  98. client.Dispose();
  99. m_targetClients.Remove((ITcpClient)client);
  100. m_internalDis?.Invoke(this, (ITcpClient)client, e);
  101. }
  102. private void TcpClient_Received(TcpClient client, ByteBlock byteBlock, IRequestInfo requestInfo)
  103. {
  104. if (DisposedValue)
  105. {
  106. return;
  107. }
  108. try
  109. {
  110. var data = m_internalTargetClientRev?.Invoke(this, client, byteBlock, requestInfo);
  111. if (data != null)
  112. {
  113. if (Online)
  114. {
  115. this.Send(data);
  116. }
  117. }
  118. }
  119. catch
  120. {
  121. }
  122. }
  123. }
  124. }