/// <summary> /// Read the first message contained in the buffer. /// </summary> /// <param name="buffer">The buffer used for the receiving</param> /// <param name="offset">The offset for start read the buffer</param> /// <param name="len">The real size of the buffer</param> /// <returns>0 if the message in the buffer is not complete. Otherwise the len of the message read</returns> private int ReadFirstMessage(byte[] buffer, int offset, int len) { // Get a Message Header instance header = GetMessageHeaderInstance(); // Read the Message Header from the buffer int idx = header.Read(buffer, offset); // Check if the message is complete if (!header.IsComplete()) { // If it's not, save the buffer in the previous buffer (pending message) and return 0. previousBuffer = new byte[len - offset]; Buffer.BlockCopy(buffer, offset, previousBuffer, 0, previousBuffer.Length); bufferIndex = 0; return(0); } // Get the message body body = new byte[header.MessageLength - header.HeaderLength]; int max = (header.MessageLength >= len) ? (len - header.HeaderLength) : (header.MessageLength - header.HeaderLength); Array.Copy(buffer, idx, body, 0, max); // Check if the message is complete. if (body.Length > len - header.HeaderLength) { // If it's not, return 0. bufferIndex = (len - header.HeaderLength); return(0); } bufferIndex = -1; return((header.MessageLength >= len) ? 0 : (header.MessageLength)); }
/// <summary> /// Add a buffer to the pending message and try to read the first message. /// </summary> /// <param name="buffer">The buffer to add</param> /// <param name="len">The real size of the buffer</param> /// <returns>0 if the message in the buffer is still not complete. Otherwise the len of the message read</returns> private int AppendBuffer(byte[] buffer, int len) { if (!header.IsComplete()) { int prevLen = previousBuffer.Length; byte[] fullBuffer = new byte[buffer.Length + prevLen]; Buffer.BlockCopy(previousBuffer, 0, fullBuffer, 0, previousBuffer.Length); Buffer.BlockCopy(buffer, 0, fullBuffer, previousBuffer.Length, buffer.Length); previousBuffer = null; int moremsg = ReadFirstMessage(fullBuffer, fullBuffer.Length); if (moremsg != 0) { moremsg -= prevLen; } return(moremsg); } int max = ((bufferIndex + len) > body.Length) ? (body.Length - bufferIndex) : len; Array.Copy(buffer, 0, body, bufferIndex, max); bufferIndex += max; if (bufferIndex == (header.MessageLength - header.HeaderLength)) { bufferIndex = -1; return((max == len) ? 0 : max); } return(0); }
/// <summary> /// Read the first message contained in the buffer. /// </summary> /// <param name="buffer">The buffer used for the receiving</param> /// <param name="offset">The offset for start read the buffer</param> /// <param name="len">The real size of the buffer</param> /// <returns>0 if the message in the buffer is not complete. Otherwise the len of the message read</returns> private int ReadFirstMessage(byte[] buffer, int offset, int len) { // Get a Message Header instance header = GetMessageHeaderInstance(); // Read the Message Header from the buffer int idx = header.Read(buffer, offset); // Check if the message is complete if (!header.IsComplete()) { // If it's not, save the buffer in the previous buffer (pending message) and return 0. previousBuffer = new byte[len - offset]; Buffer.BlockCopy(buffer, offset, previousBuffer, 0, previousBuffer.Length); bufferIndex = 0; return 0; } // Get the message body body = new byte[header.MessageLength - header.HeaderLength]; int max = (header.MessageLength >= len) ? (len - header.HeaderLength) : (header.MessageLength - header.HeaderLength); Array.Copy(buffer, idx, body, 0, max); // Check if the message is complete. if (body.Length > len - header.HeaderLength) { // If it's not, return 0. bufferIndex = (len - header.HeaderLength); return 0; } bufferIndex = -1; return (header.MessageLength >= len) ? 0 : (header.MessageLength); }