/// <summary>
        /// Writes a <see cref="UInt64" /> to a new buffer.
        /// </summary>
        /// <param name="converter">The converter.</param>
        /// <param name="value">The value.</param>
        /// <param name="useNbo">If <c>true</c> will make most significant byte first.</param>
        public static byte[] FromUInt64(this IByteConverter converter, ulong value, bool useNbo)
        {
            var buffer = new byte[sizeof(ulong)];

            converter.FromUInt64(value, buffer, useNbo);

            return(buffer);
        }
예제 #2
0
        public byte[] Serialize <T>(T value)
        {
            var bytes    = new byte[] { };
            var typeCode = Type.GetTypeCode(typeof(T));

            switch (typeCode)
            {
            case TypeCode.Empty:
            case TypeCode.DBNull:
            case TypeCode.String:
            case TypeCode.Char:
                _converter.FromString(Convert.ToString(value), ref bytes, 0);
                break;

            case TypeCode.Int16:
                _converter.FromInt16(Convert.ToInt16(value), ref bytes, 0);
                break;

            case TypeCode.UInt16:
                _converter.FromUInt16(Convert.ToUInt16(value), ref bytes, 0);
                break;

            case TypeCode.Int32:
                _converter.FromInt32(Convert.ToInt32(value), ref bytes, 0);
                break;

            case TypeCode.UInt32:
                _converter.FromUInt32(Convert.ToUInt32(value), ref bytes, 0);
                break;

            case TypeCode.Int64:
                _converter.FromInt64(Convert.ToInt64(value), ref bytes, 0);
                break;

            case TypeCode.UInt64:
                _converter.FromUInt64(Convert.ToUInt64(value), ref bytes, 0);
                break;

            case TypeCode.Single:
            case TypeCode.Double:
            case TypeCode.Decimal:
            case TypeCode.DateTime:
            case TypeCode.Boolean:
            case TypeCode.SByte:
            case TypeCode.Byte:
            case TypeCode.Object:
                bytes = SerializeAsJson(value);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
            return(bytes);
        }
예제 #3
0
        public virtual byte[] CreateHeader(byte[] extras, byte[] body, byte[] key)
        {
            var header      = new byte[OperationHeader.Length];
            var totalLength = extras.GetLengthSafe() + key.GetLengthSafe() + body.GetLengthSafe();

            Converter.FromByte((byte)Magic.Request, header, HeaderIndexFor.Magic);
            Converter.FromByte((byte)OperationCode, header, HeaderIndexFor.Opcode);
            Converter.FromInt16((short)key.GetLengthSafe(), header, HeaderIndexFor.KeyLength);
            Converter.FromByte((byte)extras.GetLengthSafe(), header, HeaderIndexFor.ExtrasLength);

            if (VBucket != null)
            {
                Converter.FromInt16((short)VBucket.Index, header, HeaderIndexFor.VBucket);
            }

            Converter.FromInt32(totalLength, header, HeaderIndexFor.BodyLength);
            Converter.FromUInt32(Opaque, header, HeaderIndexFor.Opaque);
            Converter.FromUInt64(Cas, header, HeaderIndexFor.Cas);

            return(header);
        }
 /// <summary>
 /// Writes a <see cref="UInt64" /> to a new buffer, using network byte order.
 /// </summary>
 /// <param name="converter">The converter.</param>
 /// <param name="value">The value.</param>
 public static byte[] FromUInt64(this IByteConverter converter, ulong value)
 {
     return(converter.FromUInt64(value, true));
 }
 /// <summary>
 /// Writes a <see cref="UInt64"/> to a buffer, using network byte order.
 /// </summary>
 /// <param name="converter">The converter.</param>
 /// <param name="value">The value.</param>
 /// <param name="buffer">The buffer.</param>
 public static void FromUInt64(this IByteConverter converter, ulong value, Span <byte> buffer)
 {
     converter.FromUInt64(value, buffer, true);
 }