コード例 #1
0
 public override DecodeResult Decode(ByteArraySegment data, int size, out int bytesUsed)
 {
     m_inProgress.InitPool(size);
     data.CopyTo(0, m_inProgress.Data, 0, size);
     bytesUsed = size;
     return(DecodeResult.MessageReady);
 }
コード例 #2
0
ファイル: DecoderBase.cs プロジェクト: zad15c/netmq
        /// <summary>
        /// Processes the data in the buffer previously allocated using
        /// GetBuffer function. size argument specifies the number of bytes
        /// actually filled into the buffer.
        /// </summary>
        public virtual DecodeResult Decode(ByteArraySegment data, int size, out int bytesUsed)
        {
            bytesUsed = 0;

            // In case of zero-copy simply adjust the pointers, no copying
            // is required. Also, run the state machine in case all the data
            // were processed.
            if (data != null && data.Equals(m_readPos))
            {
                m_readPos.AdvanceOffset(size);
                m_toRead -= size;
                bytesUsed = size;

                while (m_toRead == 0)
                {
                    var result = Next();
                    if (result != DecodeResult.Processing)
                    {
                        return(result);
                    }
                }

                return(DecodeResult.Processing);
            }

            while (bytesUsed < size)
            {
                // Copy the data from buffer to the message.
                int toCopy = Math.Min(m_toRead, size - bytesUsed);

                // Only copy when destination address is different from the
                // current address in the buffer.
                data.CopyTo(bytesUsed, m_readPos, 0, toCopy);
                m_readPos.AdvanceOffset(toCopy);
                m_toRead  -= toCopy;
                bytesUsed += toCopy;

                // Try to get more space in the message to fill in.
                // If none is available, return.
                while (m_toRead == 0)
                {
                    var result = Next();
                    if (result != DecodeResult.Processing)
                    {
                        return(result);
                    }
                }
            }

            return(DecodeResult.Processing);
        }
コード例 #3
0
ファイル: EncoderBase.cs プロジェクト: wangkai2014/netmq
        public void GetData(ref ByteArraySegment data, ref int size, ref int offset)
        {
            ByteArraySegment buffer = data ?? new ByteArraySegment(m_buffer);
            int bufferSize          = data == null ? m_bufferSize : size;

            int pos = 0;

            while (pos < bufferSize)
            {
                // If there are no more data to return, run the state machine.
                // If there are still no data, return what we already have
                // in the buffer.
                if (m_toWrite == 0)
                {
                    // If we are to encode the beginning of a new message,
                    // adjust the message offset.

                    if (m_beginning)
                    {
                        if (offset == -1)
                        {
                            offset = pos;
                        }
                    }

                    if (!Next())
                    {
                        break;
                    }
                }

                // If there are no data in the buffer yet and we are able to
                // fill whole buffer in a single go, let's use zero-copy.
                // There's no disadvantage to it as we cannot stuck multiple
                // messages into the buffer anyway. Note that subsequent
                // write(s) are non-blocking, thus each single write writes
                // at most SO_SNDBUF bytes at once not depending on how large
                // is the chunk returned from here.
                // As a consequence, large messages being sent won't block
                // other engines running in the same I/O thread for excessive
                // amounts of time.
                if (pos == 0 && data == null && m_toWrite >= bufferSize)
                {
                    data = m_writePos;
                    size = m_toWrite;

                    m_writePos = null;
                    m_toWrite  = 0;
                    return;
                }

                // Copy data to the buffer. If the buffer is full, return.
                int toCopy = Math.Min(m_toWrite, bufferSize - pos);

                if (toCopy != 0)
                {
                    m_writePos.CopyTo(0, buffer, pos, toCopy);
                    pos += toCopy;
                    m_writePos.AdvanceOffset(toCopy);
                    m_toWrite -= toCopy;
                }
            }

            data = buffer;
            size = pos;
        }
コード例 #4
0
        /// <summary>
        /// Processes the data in the buffer previously allocated using
        /// get_buffer function. size argument specifies the number of bytes
        /// actually filled into the buffer. Function returns number of
        /// bytes actually processed.
        /// </summary>
        public int ProcessBuffer(ByteArraySegment data, int size)
        {
            // Check if we had an error in previous attempt.
            if (State < 0)
            {
                return(-1);
            }

            // In case of zero-copy simply adjust the pointers, no copying
            // is required. Also, run the state machine in case all the data
            // were processed.
            if (data != null && data.Equals(m_readPos))
            {
                m_readPos.AdvanceOffset(size);
                m_toRead -= size;

                while (m_toRead == 0)
                {
                    if (!Next())
                    {
                        if (State < 0)
                        {
                            return(-1);
                        }
                        return(size);
                    }
                }
                return(size);
            }

            int pos = 0;

            while (true)
            {
                // Try to get more space in the message to fill in.
                // If none is available, return.
                while (m_toRead == 0)
                {
                    if (!Next())
                    {
                        if (State < 0)
                        {
                            return(-1);
                        }

                        return(pos);
                    }
                }

                // If there are no more data in the buffer, return.
                if (pos == size)
                {
                    return(pos);
                }

                // Copy the data from buffer to the message.
                int toCopy = Math.Min(m_toRead, size - pos);
                data.CopyTo(pos, m_readPos, 0, toCopy);
                m_readPos.AdvanceOffset(toCopy);
                pos      += toCopy;
                m_toRead -= toCopy;
            }
        }
コード例 #5
0
ファイル: DecoderBase.cs プロジェクト: hdxhan/netmq
        /// <summary>
        /// Processes the data in the buffer previously allocated using
        /// get_buffer function. size argument specifies the number of bytes
        /// actually filled into the buffer. Function returns number of
        /// bytes actually processed.
        /// </summary>
        public int ProcessBuffer(ByteArraySegment data, int size)
        {
            // Check if we had an error in previous attempt.
            if (State < 0)
            {
                return -1;
            }

            // In case of zero-copy simply adjust the pointers, no copying
            // is required. Also, run the state machine in case all the data
            // were processed.
            if (data != null && data.Equals(m_readPos))
            {
                m_readPos.AdvanceOffset(size);
                m_toRead -= size;

                while (m_toRead == 0)
                {
                    if (!Next())
                    {
                        if (State < 0)
                        {
                            return -1;
                        }
                        return size;
                    }
                }
                return size;
            }

            int pos = 0;
            while (true)
            {

                // Try to get more space in the message to fill in.
                // If none is available, return.
                while (m_toRead == 0)
                {
                    if (!Next())
                    {
                        if (State < 0)
                        {
                            return -1;
                        }

                        return pos;
                    }
                }

                // If there are no more data in the buffer, return.
                if (pos == size)
                    return pos;

                // Copy the data from buffer to the message.
                int toCopy = Math.Min(m_toRead, size - pos);
                data.CopyTo(pos, m_readPos, 0, toCopy);
                m_readPos.AdvanceOffset(toCopy);
                pos += toCopy;
                m_toRead -= toCopy;
            }
        }