private void ReadContent(byte[] buffer, ErrorMap errorMap) { if (Header.BodyLength == 0 && buffer.Length >= HeaderIndexFor.HeaderLength) { var status = GetResponseStatus(Converter.ToInt16(buffer, HeaderIndexFor.Status), errorMap); 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 = status, BodyLength = Converter.ToInt32(buffer, HeaderIndexFor.Body), Opaque = Converter.ToUInt32(buffer, HeaderIndexFor.Opaque), Cas = Converter.ToUInt64(buffer, HeaderIndexFor.Cas) }; if (Opaque != Header.Opaque) { var msg = string.Format("Expected opaque {0} but got {1}", Opaque, Header.Opaque); HandleClientError(msg, ResponseStatus.ClientFailure); } } LengthReceived += buffer.Length; }
public async Task ReadAsync(byte[] buffer, ErrorMap errorMap = null) { if (buffer == null || buffer.Length == 0) { return; } ReadContent(buffer, errorMap); await Data.WriteAsync(buffer, 0, buffer.Length).ContinueOnAnyContext(); }
public void Read(byte[] buffer, ErrorMap errorMap = null) { if (buffer == null || buffer.Length == 0) { return; } ReadContent(buffer, errorMap); Data.Write(buffer, 0, buffer.Length); }
private ResponseStatus GetResponseStatus(short code, ErrorMap errorMap) { var status = (ResponseStatus)code; // Is it a known response status? if (Enum.IsDefined(typeof(ResponseStatus), status)) { return(status); } // If available, try and use the error map to get more details if (errorMap != null && errorMap.TryGetGetErrorCode(code, out ErrorCode)) { return(ResponseStatus.Failure); } return(ResponseStatus.UnknownError); }
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) }); }
internal static ResponseStatus GetResponseStatus(short code, ErrorMap errorMap, out ErrorCode errorCode) { var status = (ResponseStatus)code; // Is it a known response status? if (!Enum.IsDefined(typeof(ResponseStatus), status)) { status = ResponseStatus.UnknownError; } // If available, try and use the error map to get more details if (errorMap != null) { errorMap.TryGetGetErrorCode(code, out errorCode); } else { errorCode = null;//make the compiler happy } return(status); }
public Task ReadAsync(byte[] buffer, ErrorMap errorMap = null) { var header = buffer.CreateHeader(errorMap, out var errorCode); return(ReadAsync(buffer, header, errorCode)); }
public void Read(byte[] buffer, ErrorMap errorMap = null) { var header = buffer.CreateHeader(errorMap, out var errorCode); Read(buffer, header, errorCode); }