/// <summary>
        /// Writes a <see cref="Int32" /> 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[] FromInt32(this IByteConverter converter, int value, bool useNbo)
        {
            var buffer = new byte[sizeof(int)];

            converter.FromInt32(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[24];
            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.Length, 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.FromInt32(Opaque, header, HeaderIndexFor.Opaque);
            Converter.FromUInt64(Cas, header, HeaderIndexFor.Cas);

            return(header);
        }
Пример #4
0
        private byte[] CreateHeader(IByteConverter converter, int extrasLength, int bodyLength)
        {
            var header     = new byte[OperationHeader.Length];
            var headerSpan = header.AsSpan();

            headerSpan[HeaderOffsets.Magic] = (byte)Magic.Response;
            converter.FromInt16((short)ResponseStatus.Success, headerSpan.Slice(HeaderOffsets.Status));
            headerSpan[HeaderOffsets.Opcode]       = (byte)OpCode.Get;
            headerSpan[HeaderOffsets.ExtrasLength] = (byte)extrasLength;
            converter.FromInt32(bodyLength + extrasLength, headerSpan.Slice(HeaderOffsets.BodyLength));
            headerSpan[HeaderOffsets.Datatype] = (byte)DataType.Json;

            return(header);
        }
 /// <summary>
 /// Writes a <see cref="Int32" /> to a new buffer, using network byte order.
 /// </summary>
 /// <param name="converter">The converter.</param>
 /// <param name="value">The value.</param>
 public static byte[] FromInt32(this IByteConverter converter, int value)
 {
     return(converter.FromInt32(value, true));
 }
 /// <summary>
 /// Writes a <see cref="Int32"/> 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 FromInt32(this IByteConverter converter, int value, Span <byte> buffer)
 {
     converter.FromInt32(value, buffer, true);
 }