NATService.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 TouchSocket.Core;
  14. namespace TouchSocket.Sockets
  15. {
  16. /// <summary>
  17. /// TCP端口转发服务器
  18. /// </summary>
  19. public class NATService : TcpService<NATSocketClient>
  20. {
  21. /// <summary>
  22. /// <inheritdoc/>
  23. /// </summary>
  24. /// <returns></returns>
  25. protected override NATSocketClient GetClientInstence()
  26. {
  27. var client = base.GetClientInstence();
  28. client.m_internalDis = OnTargetClientDisconnected;
  29. client.m_internalTargetClientRev = OnTargetClientReceived;
  30. return client;
  31. }
  32. /// <summary>
  33. /// 在NAT服务器收到数据时。
  34. /// </summary>
  35. /// <param name="socketClient"></param>
  36. /// <param name="byteBlock"></param>
  37. /// <param name="requestInfo"></param>
  38. /// <returns>需要转发的数据。</returns>
  39. protected virtual byte[] OnNATReceived(NATSocketClient socketClient, ByteBlock byteBlock, IRequestInfo requestInfo)
  40. {
  41. return byteBlock?.ToArray();
  42. }
  43. /// <summary>
  44. /// <inheritdoc/>
  45. /// </summary>
  46. /// <param name="socketClient"></param>
  47. /// <param name="byteBlock"></param>
  48. /// <param name="requestInfo"></param>
  49. protected override sealed void OnReceived(NATSocketClient socketClient, ByteBlock byteBlock, IRequestInfo requestInfo)
  50. {
  51. var data = OnNATReceived(socketClient, byteBlock, requestInfo);
  52. if (data != null)
  53. {
  54. socketClient.SendToTargetClient(data, 0, data.Length);
  55. }
  56. }
  57. /// <summary>
  58. /// 当目标客户端断开。
  59. /// </summary>
  60. /// <param name="socketClient"></param>
  61. /// <param name="tcpClient"></param>
  62. /// <param name="e"></param>
  63. protected virtual void OnTargetClientDisconnected(NATSocketClient socketClient, ITcpClient tcpClient, DisconnectEventArgs e)
  64. {
  65. }
  66. /// <summary>
  67. /// 在目标客户端收到数据时。
  68. /// </summary>
  69. /// <param name="socketClient"></param>
  70. /// <param name="tcpClient"></param>
  71. /// <param name="byteBlock"></param>
  72. /// <param name="requestInfo"></param>
  73. /// <returns></returns>
  74. protected virtual byte[] OnTargetClientReceived(NATSocketClient socketClient, ITcpClient tcpClient, ByteBlock byteBlock, IRequestInfo requestInfo)
  75. {
  76. return byteBlock?.ToArray();
  77. }
  78. }
  79. }