/// <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) { Assumes.NotNull(m_readPos); 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); }
/// <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; } }
/// <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; } }