public override void WriteBody(OperationBuilder builder)
        {
            using (var bufferOwner = MemoryPool <byte> .Shared.Rent(OperationSpec.MaxPathLength))
            {
                var buffer = bufferOwner.Memory.Span;

                var pathLength = Converter.FromString(Path, buffer);

                builder.Write(bufferOwner.Memory.Slice(0, pathLength));
            }

            if (!CurrentSpec.RemoveBrackets)
            {
                // We can serialize directly
                Transcoder.Serializer.Serialize(builder, CurrentSpec.Value);
            }
            else
            {
                using (var stream = MemoryStreamFactory.GetMemoryStream())
                {
                    Transcoder.Serializer.Serialize(stream, CurrentSpec.Value);

                    ReadOnlyMemory <byte> body = stream.GetBuffer().AsMemory(0, (int)stream.Length);
                    body = body.StripBrackets();

                    builder.Write(body);
                }
            }
        }
        public virtual void HandleClientError(string message, ResponseStatus responseStatus)
        {
            Reset(responseStatus);
            var msgBytes = Encoding.UTF8.GetBytes(message);

            if (Data == null)
            {
                Data = MemoryStreamFactory.GetMemoryStream();
            }
            Data.Write(msgBytes, 0, msgBytes.Length);
        }
        public override byte[] SerializeAsJson(object value)
        {
            if (value == null)
            {
                return(new byte[0]);
            }

            using (var ms = MemoryStreamFactory.GetMemoryStream())
            {
                _formatter.Serialize(ms, value);
                return(ms.ToArray());
            }
        }
        protected OperationBase()
        {
            Opaque = SequenceGenerator.GetNext();
            Data   = MemoryStreamFactory.GetMemoryStream();
            Header = new OperationHeader {
                Status = ResponseStatus.None
            };
            Key = string.Empty;

            //temporarily make a static - later should be pluggable/set externally
            Converter  = DefaultConverter;
            Transcoder = DefaultTranscoder;
        }
 /// <summary>
 /// Serializes the specified object into a buffer.
 /// </summary>
 /// <param name="obj">The object to serialize.</param>
 /// <returns>A <see cref="byte"/> array that is the serialized value of the key.</returns>
 public byte[] Serialize(object obj)
 {
     using (var ms = MemoryStreamFactory.GetMemoryStream())
     {
         using (var sw = new StreamWriter(ms))
         {
             using (var jr = new JsonTextWriter(sw))
             {
                 var serializer = JsonSerializer.Create(SerializerSettings);
                 serializer.Serialize(jr, obj);
             }
         }
         return(ms.ToArray());
     }
 }
        public virtual void Reset(ResponseStatus status)
        {
            Data?.Dispose();
            Data = MemoryStreamFactory.GetMemoryStream();

            Header = new OperationHeader
            {
                Magic      = Header.Magic,
                OpCode     = OpCode,
                Cas        = Header.Cas,
                BodyLength = Header.BodyLength,
                Key        = Key,
                Status     = status
            };
        }
Exemple #7
0
        public virtual void Reset(ResponseStatus status)
        {
            if (Data != null)
            {
                Data.Dispose();
            }
            Data           = MemoryStreamFactory.GetMemoryStream();
            LengthReceived = 0;

            Header = new OperationHeader
            {
                Magic         = Header.Magic,
                OperationCode = OperationCode,
                Cas           = Header.Cas,
                BodyLength    = Header.BodyLength,
                Key           = Key,
                Status        = status
            };
        }
Exemple #8
0
        protected OperationBase(string key, IVBucket vBucket, ITypeTranscoder transcoder, uint opaque, uint timeout)
        {
            if (RequiresKey && string.IsNullOrWhiteSpace(key))
            {
                throw new MissingKeyException();
            }

            Key          = key;
            Transcoder   = transcoder;
            Opaque       = opaque;
            CreationTime = DateTime.UtcNow;
            Timeout      = timeout;
            VBucket      = vBucket;
            Converter    = transcoder.Converter;
            MaxRetries   = DefaultRetries;
            Data         = MemoryStreamFactory.GetMemoryStream();
            Header       = new OperationHeader {
                Status = ResponseStatus.None
            };
        }
Exemple #9
0
        private void WriteSpecValue(OperationBuilder builder, OperationSpec spec)
        {
            if (!spec.RemoveBrackets)
            {
                // We can serialize directly
                Transcoder.Serializer.Serialize(builder, spec.Value);
            }
            else
            {
                using (var stream = MemoryStreamFactory.GetMemoryStream())
                {
                    Transcoder.Serializer.Serialize(stream, spec.Value);

                    ReadOnlyMemory <byte> bytes = stream.GetBuffer().AsMemory(0, (int)stream.Length);
                    bytes = bytes.StripBrackets();

                    builder.Write(bytes);
                }
            }
        }