コード例 #1
0
ファイル: Message.cs プロジェクト: mgnsm/Millistream.NET
        /// <summary>
        /// Adds a UTF-8 string field to the current active message. The string is compressed with zlib using the compression level as set by <see cref="CompressionLevel" /> which is <see cref="CompressionLevel.Z_BEST_SPEED"/> by default.
        /// </summary>
        /// <param name="tag">The field tag.</param>
        /// <param name="value">The UTF-8 string field value.</param>
        /// <param name="length">The number of characters in <paramref name="value"/> to be added to the message.</param>
        /// <returns><see langword="true" /> if the field was successfully added, or <see langword="false" /> if the value could not be added (because there was no more memory, the message handle does not contain any messages, or the supplied value is not of the type specified).</returns>
        /// <exception cref="InvalidOperationException">The installed version of the native library doesn't include the mdf_message_add_string2 function.</exception>
        /// <exception cref="ObjectDisposedException">The <see cref="Message"/> instance has been disposed.</exception>
        /// <remarks>The corresponding native function is mdf_message_add_string2.</remarks>
        public bool AddString(uint tag, string value, int length)
        {
            ThrowIfDisposed();
            if (value == null)
            {
                return(_nativeImplementation.mdf_message_add_string2(Handle, tag, IntPtr.Zero, length) == 1);
            }

            if (length < 0)
            {
                return(false);

                fixed(char *c = value)
                {
                    int   byteCount = Encoding.UTF8.GetByteCount(c, length);
                    byte *b         = stackalloc byte[byteCount + 1];

                    Encoding.UTF8.GetBytes(c, length, b, byteCount);
                    return(_nativeImplementation.mdf_message_add_string2(Handle, tag, (IntPtr)b, byteCount) == 1);
                }
        }