/// <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> /// 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); }