public static byte[] GetPacketBuffer(BaseSocketConnection connection, byte[] buffer, ref int bufferSize) { byte[] result = null; buffer = CryptUtils.EncryptData(connection, buffer); switch (connection.DelimiterType) { case DelimiterType.dtNone: //----- No Delimiter! bufferSize = buffer.Length; result = connection.BaseHost.BufferManager.TakeBuffer(bufferSize); Buffer.BlockCopy(buffer, 0, result, 0, buffer.Length); break; case DelimiterType.dtMessageTailExcludeOnReceive: case DelimiterType.dtMessageTailIncludeOnReceive: if (connection.Delimiter?.Length > 0) { //----- Need delimiter! bufferSize = buffer.Length + connection.Delimiter.Length; result = connection.BaseHost.BufferManager.TakeBuffer(bufferSize); Buffer.BlockCopy(buffer, 0, result, 0, buffer.Length); Buffer.BlockCopy(connection.Delimiter, 0, result, buffer.Length, connection.Delimiter.Length); } else { bufferSize = buffer.Length; result = connection.BaseHost.BufferManager.TakeBuffer(bufferSize); Buffer.BlockCopy(buffer, 0, result, 0, buffer.Length); } break; } return(result); }
/// <summary> /// Gets a packet message! /// </summary> /// <param name="connection"> /// Socket connection. /// </param> /// <param name="buffer"> /// Data. /// </param> public static MessageBuffer GetPacketMessage(BaseSocketConnection connection, byte[] buffer) { byte[] workBuffer = null; MessageBuffer messageBuffer = null; workBuffer = CryptUtils.EncryptData(connection, buffer); switch (connection.DelimiterType) { case DelimiterType.dtNone: //----- No Delimiter! messageBuffer = new MessageBuffer(buffer, workBuffer); break; case DelimiterType.dtPacketHeader: if (connection.Delimiter != null && connection.Delimiter.Length >= 0) { //----- Need delimiter! int delimiterSize = connection.Delimiter.Length + 3; byte[] result = new byte[workBuffer.Length + delimiterSize]; int messageLength = result.Length; //----- Delimiter! for (int i = 0; i < connection.Delimiter.Length; i++) { result[i] = connection.Delimiter[i]; } //----- Length! result[connection.Delimiter.Length] = Convert.ToByte((messageLength & 0xFF0000) >> 16); result[connection.Delimiter.Length + 1] = Convert.ToByte((messageLength & 0xFF00) >> 8); result[connection.Delimiter.Length + 2] = Convert.ToByte(messageLength & 0xFF); Buffer.BlockCopy(workBuffer, 0, result, delimiterSize, workBuffer.Length); messageBuffer = new MessageBuffer(buffer, result); } break; case DelimiterType.dtMessageTailExcludeOnReceive: case DelimiterType.dtMessageTailIncludeOnReceive: if (connection.Delimiter != null && connection.Delimiter.Length >= 0) { //----- Need delimiter! byte[] result = new byte[workBuffer.Length + connection.Delimiter.Length]; Buffer.BlockCopy(workBuffer, 0, result, 0, workBuffer.Length); Buffer.BlockCopy(connection.Delimiter, 0, result, workBuffer.Length, connection.Delimiter.Length); messageBuffer = new MessageBuffer(buffer, result); } break; } return(messageBuffer); }