/// <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) { Log("Reading response length"); // Read the response length var lengthBuffer = new byte[4]; await ReadAllAsync(stream, lengthBuffer, 4, cancellationToken).ConfigureAwait(false); var length = BitConverter.ToUInt32(lengthBuffer, 0); // Read the response Log("Reading response of length {0}", length); var responseBuffer = new byte[length]; await 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 new MismatchedVersionBuildResponse(); case ResponseType.AnalyzerInconsistency: return new AnalyzerInconsistencyBuildResponse(); default: throw new InvalidOperationException("Received invalid response type from server."); } } }
public async Task ReadWriteCompleted() { var response = new CompletedBuildResponse(42, utf8output: false, output: "a string"); var memoryStream = new MemoryStream(); await response.WriteAsync(memoryStream, default(CancellationToken)); Assert.True(memoryStream.Position > 0); memoryStream.Position = 0; var read = (CompletedBuildResponse)(await BuildResponse.ReadAsync(memoryStream, default(CancellationToken))); Assert.Equal(42, read.ReturnCode); Assert.False(read.Utf8Output); Assert.Equal("a string", read.Output); Assert.Equal("", read.ErrorOutput); }