/// <summary> /// May throw exceptions if there are pipe problems. /// </summary> /// <param name="stream"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public static async Task <BuildResponse> ReadAsync(PipeStream stream, CancellationToken cancellationToken) { CompilerServerLogger.Log("Reading response length"); // Read the response length var lengthBuffer = new byte[4]; await BuildProtocolConstants.ReadAllAsync(stream, lengthBuffer, 4, cancellationToken).ConfigureAwait(false); var length = BitConverter.ToUInt32(lengthBuffer, 0); // Read the response CompilerServerLogger.Log("Reading response of length {0}", length); var responseBuffer = new byte[length]; await BuildProtocolConstants.ReadAllAsync(stream, responseBuffer, responseBuffer.Length, cancellationToken).ConfigureAwait(false); using (var reader = new BinaryReader(new MemoryStream(responseBuffer), Encoding.Unicode)) { int returnCode = reader.ReadInt32(); string output = BuildProtocolConstants.ReadLengthPrefixedString(reader); string errorOutput = BuildProtocolConstants.ReadLengthPrefixedString(reader); return(new BuildResponse(returnCode, output, errorOutput)); } }
/// <summary> /// May throw exceptions if there are pipe problems. /// </summary> /// <param name="stream"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public static async Task <BuildResponse> ReadAsync(Stream stream, CancellationToken cancellationToken) { CompilerServerLogger.Log("Reading response length"); // Read the response length var lengthBuffer = new byte[4]; await BuildProtocolConstants.ReadAllAsync(stream, lengthBuffer, 4, cancellationToken).ConfigureAwait(false); var length = BitConverter.ToUInt32(lengthBuffer, 0); // Read the response CompilerServerLogger.Log("Reading response of length {0}", length); var responseBuffer = new byte[length]; await BuildProtocolConstants.ReadAllAsync(stream, responseBuffer, responseBuffer.Length, cancellationToken).ConfigureAwait(false); using (var reader = new BinaryReader(new MemoryStream(responseBuffer), Encoding.Unicode)) { var responseType = (ResponseType)reader.ReadInt32(); switch (responseType) { case ResponseType.Completed: return(CompletedBuildResponse.Create(reader)); case ResponseType.MismatchedVersion: return(MismatchedVersionBuildResponse.Create(reader)); default: throw new InvalidOperationException("Received invalid response type from server."); } } }
/// <summary> /// Read a Request from the given stream. /// /// The total request size must be less than 1MB. /// </summary> /// <returns>null if the Request was too large, the Request otherwise.</returns> public static async Task <BuildRequest> ReadAsync(Stream inStream, CancellationToken cancellationToken) { // Read the length of the request var lengthBuffer = new byte[4]; CompilerServerLogger.Log("Reading length of request"); await BuildProtocolConstants.ReadAllAsync(inStream, lengthBuffer, 4, cancellationToken).ConfigureAwait(false); var length = BitConverter.ToInt32(lengthBuffer, 0); // Back out if the request is > 1MB if (length > 0x100000) { CompilerServerLogger.Log("Request is over 1MB in length, cancelling read."); return(null); } cancellationToken.ThrowIfCancellationRequested(); // Read the full request var responseBuffer = new byte[length]; await BuildProtocolConstants.ReadAllAsync(inStream, responseBuffer, length, cancellationToken).ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); CompilerServerLogger.Log("Parsing request"); // Parse the request into the Request data structure. using (var reader = new BinaryReader(new MemoryStream(responseBuffer), Encoding.Unicode)) { var protocolVersion = reader.ReadUInt32(); var language = (BuildProtocolConstants.RequestLanguage)reader.ReadUInt32(); uint argumentCount = reader.ReadUInt32(); var argumentsBuilder = ImmutableArray.CreateBuilder <Argument>((int)argumentCount); for (int i = 0; i < argumentCount; i++) { cancellationToken.ThrowIfCancellationRequested(); argumentsBuilder.Add(BuildRequest.Argument.ReadFromBinaryReader(reader)); } return(new BuildRequest(protocolVersion, language, argumentsBuilder.ToImmutableArray())); } }