Exemplo n.º 1
0
        internal WebSocketMessage ToMessage(WebSocketMessageType messageType, WebSocketBase socket)
        {
            if (m_current.Position > 0)
            {
                if (m_deflater != null)
                {
                    Deflate();
                    m_current.Reset();
                }
                else
                {
                    // No compression, just append the block for sending
                    m_current.AppendTo(ref m_completedLast);

                    if (m_completedFirst == null)
                    {
                        m_completedFirst = m_completedLast;
                    }
                }
            }

            if (m_deflater != null && m_completedLast != null)
            {
                FinishDeflater();
            }

            var offset     = 0;
            var compressed = m_completedLast != null && m_deflater != null;

            if (socket.Native)
            {
                if (m_completedLast == null)
                {
                    // The send procedure requires at least the message header
                    m_completedLast  = new BufferSequenceSegment(WebSocketBase.Memory.Rent(WebSocketBase.MaxHeaderSize), WebSocketBase.MaxHeaderSize);
                    m_completedFirst = m_completedLast;
                }

                offset = WriteHeader(messageType, compressed, socket.IsServer());

                if (!socket.IsServer())
                {
                    ApplyMask();
                }
            }

            var message = m_completedFirst == null ? (WebSocketMessage) new WebSocketControlMessage(messageType, Array.Empty <Byte>()) :
                          new WebSocketDataMessage(messageType, m_completedFirst, offset, compressed);

            m_completedFirst = null;
            m_completedLast  = null;

            return(message);
        }
Exemplo n.º 2
0
        public Memory <Byte> GetMemory(Int32 sizeHint)
        {
Retry:
            if (m_current.Available == 0 || m_current.Available < sizeHint)
            {
                // If nothing is written, discard the current memory and replace it with
                // new one that is big enough
                if (m_current.Position == 0)
                {
                    m_current.Reset(WebSocketBase.Memory.Rent(WebSocketBase.AdjustSegmentSize(sizeHint)));

                    if (m_completedFirst == null && m_deflater == null)
                    {
                        // Reserve space for the header only if compression is not enabled.
                        // When compression is enabled, the header is reserved when we compress the memory.
                        m_current.Advance(WebSocketBase.MaxHeaderSize);
                    }
                }
                else
                {
                    if (m_deflater != null)
                    {
                        Deflate();

                        // Go back from the beginning now that we've freed up space
                        goto Retry;
                    }

                    // The socket doesn't support compression, just appent the segment
                    m_current.AppendTo(ref m_completedLast);
                    m_current = new BufferSegment(WebSocketBase.Memory, WebSocketBase.AdjustSegmentSize(sizeHint));

                    if (m_completedFirst == null)
                    {
                        m_completedFirst = m_completedLast;
                    }
                }
            }

            return(m_current.AvailableMemory);
        }