public async Task SendNextAsync(int bytesLimit) { if (m_headerBytes != null) { if (!await m_context.SendAsync(m_headerBytes, 0, m_headerBytes.Length).ConfigureAwait(false)) { if (m_context.CanSend()) { m_context.ContinueSendResponse(true); return; } if (m_body != null) { m_body.Dispose(); } RawBuffer = null; Sent = true; return; } bytesLimit -= m_headerBytes.Length; m_headerBytes = null; if (bytesLimit <= 0) { m_context.ContinueSendResponse(true); return; } } if (RawBuffer != null) { if (RawBufferLen > 0) { if (BandWitdhEvent != null) { bytesLimit = CheckBandwidth(RawBufferLen, bytesLimit); } bool sendRes; if (RawBufferLen > bytesLimit) { sendRes = (await m_context.SendAsync(RawBuffer, RawBufferStart, bytesLimit).ConfigureAwait(false)); if (sendRes) { RawBufferLen -= bytesLimit; RawBufferStart += bytesLimit; } } else { sendRes = await m_context.SendAsync(RawBuffer, RawBufferStart, RawBufferLen).ConfigureAwait(false); if (sendRes) { RawBufferLen = 0; } } if (!sendRes) { if (m_context.CanSend()) { m_context.ContinueSendResponse(true); return; } RawBuffer = null; if (m_body != null) { Body.Dispose(); } Sent = true; return; } } if (RawBufferLen <= 0) { RawBuffer = null; } else { m_context.ContinueSendResponse(true); return; } } if (m_body != null && m_body.Length != 0) { MemoryStream mb = m_body as MemoryStream; RawBuffer = mb.GetBuffer(); RawBufferStart = 0; // must be a internal buffer, or starting at 0 RawBufferLen = (int)mb.Length; mb.Dispose(); m_body = null; if (RawBufferLen > 0) { bool sendRes; if (RawBufferLen > bytesLimit) { sendRes = await m_context.SendAsync(RawBuffer, RawBufferStart, bytesLimit).ConfigureAwait(false); if (sendRes) { RawBufferLen -= bytesLimit; RawBufferStart += bytesLimit; } } else { sendRes = await m_context.SendAsync(RawBuffer, RawBufferStart, RawBufferLen).ConfigureAwait(false); if (sendRes) { RawBufferLen = 0; } } if (!sendRes) { if (m_context.CanSend()) { m_context.ContinueSendResponse(true); return; } RawBuffer = null; Sent = true; return; } } if (RawBufferLen > 0) { m_context.ContinueSendResponse(false); return; } } if (m_body != null) { m_body.Dispose(); } Sent = true; m_context.EndSendResponse(requestID, Connection); }