public virtual void Read(byte[] buffer, int offset, int length) { if (Header.BodyLength == 0) { Header = new OperationHeader { Magic = Converter.ToByte(buffer, HeaderIndexFor.Magic), OperationCode = Converter.ToByte(buffer, HeaderIndexFor.Opcode).ToOpCode(), KeyLength = Converter.ToInt16(buffer, HeaderIndexFor.KeyLength), ExtrasLength = Converter.ToByte(buffer, HeaderIndexFor.ExtrasLength), Status = (ResponseStatus)Converter.ToInt16(buffer, HeaderIndexFor.Status), BodyLength = Converter.ToInt32(buffer, HeaderIndexFor.Body), Opaque = Converter.ToUInt32(buffer, HeaderIndexFor.Opaque), Cas = Converter.ToUInt64(buffer, HeaderIndexFor.Cas) }; } LengthReceived += length; Data.Write(buffer, offset, length); }
protected void CreateHeader(OperationAsyncState state) { var buffer = state.Data.GetBuffer(); if (buffer.Length > 0) { state.Header = new OperationHeader { Magic = _converter.ToByte(buffer, HeaderIndexFor.Magic), OperationCode = _converter.ToByte(buffer, HeaderIndexFor.Opcode).ToOpCode(), KeyLength = _converter.ToInt16(buffer, HeaderIndexFor.KeyLength), ExtrasLength = _converter.ToByte(buffer, HeaderIndexFor.ExtrasLength), Status = (ResponseStatus)_converter.ToInt16(buffer, HeaderIndexFor.Status), BodyLength = _converter.ToInt32(buffer, HeaderIndexFor.Body), Opaque = _converter.ToUInt32(buffer, HeaderIndexFor.Opaque), Cas = _converter.ToUInt64(buffer, HeaderIndexFor.Cas) }; } }
internal static OperationHeader CreateHeader(this ReadOnlySpan <byte> buffer, ErrorMap errorMap, out ErrorCode errorCode) { if (buffer == null || buffer.Length < OperationHeader.Length) { errorCode = null; return(new OperationHeader { Status = ResponseStatus.None }); } int keyLength, framingExtrasLength; var magic = (Magic)buffer[HeaderOffsets.Magic]; if (magic == Magic.AltResponse) { framingExtrasLength = buffer[HeaderOffsets.FramingExtras]; keyLength = buffer[HeaderOffsets.AltKeyLength]; } else { framingExtrasLength = 0; keyLength = Converter.ToInt16(buffer.Slice(HeaderOffsets.KeyLength)); } var statusCode = Converter.ToInt16(buffer.Slice(HeaderOffsets.Status)); var status = GetResponseStatus(statusCode, errorMap, out errorCode); return(new OperationHeader { Magic = (byte)magic, OpCode = buffer[HeaderOffsets.Opcode].ToOpCode(), FramingExtrasLength = framingExtrasLength, KeyLength = keyLength, ExtrasLength = buffer[HeaderOffsets.ExtrasLength], DataType = (DataType)buffer[HeaderOffsets.Datatype], Status = status, BodyLength = Converter.ToInt32(buffer.Slice(HeaderOffsets.Body)), Opaque = Converter.ToUInt32(buffer.Slice(HeaderOffsets.Opaque)), Cas = Converter.ToUInt64(buffer.Slice(HeaderOffsets.Cas)) }); }
internal static OperationHeader CreateHeader(this byte[] buffer, ErrorMap errorMap, out ErrorCode errorCode) { if (buffer == null || buffer.Length < HeaderIndexFor.HeaderLength) { errorCode = null; return(new OperationHeader { Status = ResponseStatus.None }); } int keyLength, framingExtrasLength; var magic = (Magic)Converter.ToByte(buffer, HeaderIndexFor.Magic); if (magic == Magic.AltResponse) { framingExtrasLength = Converter.ToByte(buffer, HeaderIndexFor.FramingExtras); keyLength = Converter.ToByte(buffer, HeaderIndexFor.AltKeyLength); } else { framingExtrasLength = 0; keyLength = Converter.ToInt16(buffer, HeaderIndexFor.KeyLength); } var statusCode = Converter.ToInt16(buffer, HeaderIndexFor.Status); var status = GetResponseStatus(statusCode, errorMap, out errorCode); return(new OperationHeader { Magic = (byte)magic, OperationCode = Converter.ToByte(buffer, HeaderIndexFor.Opcode).ToOpCode(), FramingExtrasLength = framingExtrasLength, KeyLength = keyLength, ExtrasLength = Converter.ToByte(buffer, HeaderIndexFor.ExtrasLength), DataType = (DataType)Converter.ToByte(buffer, HeaderIndexFor.Datatype), Status = status, BodyLength = Converter.ToInt32(buffer, HeaderIndexFor.Body), Opaque = Converter.ToUInt32(buffer, HeaderIndexFor.Opaque), Cas = Converter.ToUInt64(buffer, HeaderIndexFor.Cas) }); }
private void ParseSpecs() { var response = _contentBytes.Memory.Slice(Header.BodyOffset); var commandIndex = 0; for (;;) { var bodyLength = _converter.ToInt32(response.Span.Slice(2)); var payLoad = response.Slice(6, bodyLength); var command = _specs[commandIndex++]; command.Status = (ResponseStatus)_converter.ToUInt16(response.Span); command.ValueIsJson = payLoad.Span.IsJson(); command.Bytes = payLoad; response = response.Slice(6 + bodyLength); if (response.Length <= 0) { break; } } }
public T ContentAs <T>(int index) { var response = _bytes; var statusOffset = 24;//Header.BodyOffset; var valueLengthOffset = statusOffset + 2; var valueOffset = statusOffset + 6; var operationSpecs = new List <OperationSpec>(); for (;;) { var bodyLength = _converter.ToInt32(response, valueLengthOffset); var payLoad = new byte[bodyLength]; Buffer.BlockCopy(response, valueOffset, payLoad, 0, bodyLength); var command = new OperationSpec { Status = (ResponseStatus)_converter.ToUInt16(response, statusOffset), ValueIsJson = payLoad.IsJson(0, bodyLength - 1), Bytes = payLoad }; operationSpecs.Add(command); statusOffset = valueOffset + bodyLength; valueLengthOffset = statusOffset + 2; valueOffset = statusOffset + 6; if (valueOffset >= response.Length) { break; } } var spec = operationSpecs[index]; return(_serializer.Deserialize <T>(spec.Bytes, 0, spec.Bytes.Length)); }
public T Deserialize <T>(byte[] buffer, int offset, int length) { object value = default(T); var typeCode = Type.GetTypeCode(typeof(T)); switch (typeCode) { case TypeCode.Empty: case TypeCode.DBNull: case TypeCode.String: case TypeCode.Char: value = Deserialize(buffer, offset, length); break; case TypeCode.Int16: if (length > 0) { value = _converter.ToInt16(buffer, offset); } break; case TypeCode.UInt16: if (length > 0) { value = _converter.ToUInt16(buffer, offset); } break; case TypeCode.Int32: if (length > 0) { value = _converter.ToInt32(buffer, offset); } break; case TypeCode.UInt32: if (length > 0) { value = _converter.ToUInt32(buffer, offset); } break; case TypeCode.Int64: if (length > 0) { value = _converter.ToInt64(buffer, offset); } break; case TypeCode.UInt64: if (length > 0) { value = _converter.ToUInt64(buffer, offset); } break; case TypeCode.Single: break; case TypeCode.Double: break; case TypeCode.Decimal: break; case TypeCode.DateTime: break; case TypeCode.Boolean: break; case TypeCode.SByte: break; case TypeCode.Byte: break; case TypeCode.Object: value = DeserializeAsJson <T>(buffer, offset, length); break; default: throw new ArgumentOutOfRangeException(); } return((T)value); }
/// <summary> /// Reads a <see cref="Int32" /> from a buffer, using network byte order. /// </summary> /// <param name="converter">The converter.</param> /// <param name="buffer">The buffer.</param> /// <returns></returns> public static int ToInt32(this IByteConverter converter, ReadOnlySpan <byte> buffer) { return(converter.ToInt32(buffer, true)); }