コード例 #1
0
ファイル: HttpResponse.cs プロジェクト: mdickson/opensim-libs
        public async Task SendNextAsync(int bytesLimit)
        {
            if (m_headerBytes != null)
            {
                if (!await m_context.SendAsync(m_headerBytes, 0, m_headerBytes.Length).ConfigureAwait(false))
                {
                    if (_body != null)
                    {
                        _body.Dispose();
                    }
                    RawBuffer = null;
                    Sent      = true;
                    return;
                }
                bytesLimit   -= m_headerBytes.Length;
                m_headerBytes = null;
                if (bytesLimit <= 0)
                {
                    m_context.ContinueSendResponse();
                    return;
                }
            }

            if (RawBuffer != null)
            {
                if (RawBufferLen > 0)
                {
                    bool sendRes;
                    if (RawBufferLen > bytesLimit)
                    {
                        sendRes = await m_context.SendAsync(RawBuffer, RawBufferStart, bytesLimit).ConfigureAwait(false);

                        RawBufferLen   -= bytesLimit;
                        RawBufferStart += bytesLimit;
                    }
                    else
                    {
                        sendRes = await m_context.SendAsync(RawBuffer, RawBufferStart, RawBufferLen).ConfigureAwait(false);

                        RawBufferLen = 0;
                    }

                    if (!sendRes)
                    {
                        RawBuffer = null;
                        if (_body != null)
                        {
                            Body.Dispose();
                        }
                        Sent = true;
                        return;
                    }
                }
                if (RawBufferLen <= 0)
                {
                    RawBuffer = null;
                }
                else
                {
                    m_context.ContinueSendResponse();
                    return;
                }
            }

            if (_body != null && _body.Length != 0)
            {
                _body.Flush();
                _body.Seek(0, SeekOrigin.Begin);

                RawBuffer    = new byte[_body.Length];
                RawBufferLen = _body.Read(RawBuffer, 0, (int)_body.Length);
                _body.Dispose();

                if (RawBufferLen > 0)
                {
                    bool sendRes;
                    if (RawBufferLen > bytesLimit)
                    {
                        sendRes = await m_context.SendAsync(RawBuffer, RawBufferStart, bytesLimit).ConfigureAwait(false);

                        RawBufferLen   -= bytesLimit;
                        RawBufferStart += bytesLimit;
                    }
                    else
                    {
                        sendRes = await m_context.SendAsync(RawBuffer, RawBufferStart, RawBufferLen).ConfigureAwait(false);

                        RawBufferLen = 0;
                    }

                    if (!sendRes)
                    {
                        RawBuffer = null;
                        Sent      = true;
                        return;
                    }
                }
                if (RawBufferLen > 0)
                {
                    m_context.ContinueSendResponse();
                    return;
                }
            }

            if (_body != null)
            {
                _body.Dispose();
            }
            Sent = true;
            m_context.ReqResponseSent(requestID, Connection);
        }
コード例 #2
0
ファイル: HttpResponse.cs プロジェクト: Teslos-Dev/Teslos-Sim
        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);
        }