HttpServerDataHandlingAdapter.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. using TouchSocket.Sockets;
  17. namespace TouchSocket.Http
  18. {
  19. /// <summary>
  20. /// Http服务器数据处理适配器
  21. /// </summary>
  22. public class HttpServerDataHandlingAdapter : NormalDataHandlingAdapter
  23. {
  24. /// <summary>
  25. /// 缓存数据,如果需要手动释放,请先判断,然后到调用<see cref="IDisposable.Dispose"/>后,再置空;
  26. /// </summary>
  27. protected ByteBlock tempByteBlock;
  28. /// <summary>
  29. /// <inheritdoc/>
  30. /// </summary>
  31. public override bool CanSplicingSend => false;
  32. /// <summary>
  33. /// <inheritdoc/>
  34. /// </summary>
  35. /// <param name="byteBlock"></param>
  36. protected override void PreviewReceived(ByteBlock byteBlock)
  37. {
  38. if (tempByteBlock == null)
  39. {
  40. byteBlock.Pos = 0;
  41. Single(byteBlock, false);
  42. }
  43. else
  44. {
  45. tempByteBlock.Write(byteBlock.Buffer, 0, byteBlock.Len);
  46. ByteBlock block = tempByteBlock;
  47. tempByteBlock = null;
  48. block.Pos = 0;
  49. Single(block, true);
  50. }
  51. }
  52. private HttpRequest m_httpRequest;
  53. private Task m_task;
  54. private long m_surLen;
  55. private void Single(ByteBlock byteBlock, bool dis)
  56. {
  57. try
  58. {
  59. while (byteBlock.CanReadLen > 0)
  60. {
  61. if (m_httpRequest == null)
  62. {
  63. m_httpRequest = new HttpRequest(Client, true);
  64. if (m_httpRequest.ParsingHeader(byteBlock, byteBlock.CanReadLen))
  65. {
  66. byteBlock.Pos++;
  67. if (m_httpRequest.ContentLength > byteBlock.CanReadLength)
  68. {
  69. m_surLen = m_httpRequest.ContentLength;
  70. m_task = EasyTask.Run(m_httpRequest, (res) =>
  71. {
  72. GoReceived(null, res);
  73. });
  74. }
  75. else
  76. {
  77. byteBlock.Read(out byte[] buffer, (int)m_httpRequest.ContentLength);
  78. m_httpRequest.SetContent(buffer);
  79. GoReceived(null, m_httpRequest);
  80. m_httpRequest = null;
  81. }
  82. }
  83. else
  84. {
  85. Cache(byteBlock);
  86. m_httpRequest = null;
  87. m_task?.Wait();
  88. m_task = null;
  89. return;
  90. }
  91. }
  92. if (m_surLen > 0)
  93. {
  94. if (byteBlock.CanRead)
  95. {
  96. int len = (int)Math.Min(m_surLen, byteBlock.CanReadLength);
  97. m_httpRequest.InternalInput(byteBlock.Buffer, byteBlock.Pos, len);
  98. m_surLen -= len;
  99. byteBlock.Pos += len;
  100. if (m_surLen == 0)
  101. {
  102. m_httpRequest.InternalInput(null, 0, 0);
  103. m_httpRequest = null;
  104. m_task?.Wait();
  105. m_task = null;
  106. }
  107. }
  108. }
  109. else
  110. {
  111. m_httpRequest = null;
  112. m_task?.Wait();
  113. m_task = null;
  114. }
  115. }
  116. }
  117. finally
  118. {
  119. if (dis)
  120. {
  121. byteBlock.Dispose();
  122. }
  123. }
  124. }
  125. private void Cache(ByteBlock byteBlock)
  126. {
  127. if (byteBlock.CanReadLen > 0)
  128. {
  129. tempByteBlock = new ByteBlock();
  130. tempByteBlock.Write(byteBlock.Buffer, byteBlock.Pos, byteBlock.CanReadLen);
  131. if (tempByteBlock.Len > MaxPackageSize)
  132. {
  133. OnError("缓存的数据长度大于设定值的情况下未收到解析信号");
  134. }
  135. }
  136. }
  137. }
  138. }