@@ -82,11 +82,12 @@ public static BuildRequest Create(RequestLanguage language,
8282 {
8383 Debug . Assert ( ! string . IsNullOrWhiteSpace ( compilerHash ) , "CompilerHash is required to send request to the build server" ) ;
8484
85- Log ( "Creating BuildRequest" ) ;
86- Log ( $ "Working directory: { workingDirectory } ") ;
87- Log ( $ "Temp directory: { tempDirectory } ") ;
88- Log ( $ "Lib directory: { libDirectory ?? "null" } ") ;
89- Log ( $ "Compiler hash: { compilerHash } ") ;
85+ Log ( $@ "
86+ Creating BuildRequest
87+ Working directory: { workingDirectory }
88+ Temp directory: { tempDirectory }
89+ Lib directory: { libDirectory ?? null }
90+ Compiler hash: { compilerHash } " ) ;
9091
9192 var requestLength = args . Count + 1 + ( libDirectory == null ? 0 : 1 ) ;
9293 var requestArgs = new List < Argument > ( requestLength ) ;
@@ -108,7 +109,6 @@ public static BuildRequest Create(RequestLanguage language,
108109 for ( int i = 0 ; i < args . Count ; ++ i )
109110 {
110111 var arg = args [ i ] ;
111- Log ( $ "argument[{ i } ] = { arg } ") ;
112112 requestArgs . Add ( new Argument ( ArgumentId . CommandLineArgument , i , arg ) ) ;
113113 }
114114
@@ -118,7 +118,7 @@ public static BuildRequest Create(RequestLanguage language,
118118 public static BuildRequest CreateShutdown ( )
119119 {
120120 var requestArgs = new [ ] { new Argument ( ArgumentId . Shutdown , argumentIndex : 0 , value : "" ) } ;
121- return new BuildRequest ( BuildProtocolConstants . ProtocolVersion , RequestLanguage . CSharpCompile , GetCommitHash ( ) , requestArgs ) ;
121+ return new BuildRequest ( BuildProtocolConstants . ProtocolVersion , RequestLanguage . CSharpCompile , GetCommitHash ( ) ?? "" , requestArgs ) ;
122122 }
123123
124124 /// <summary>
@@ -127,19 +127,17 @@ public static BuildRequest CreateShutdown()
127127 /// The total request size must be less than 1MB.
128128 /// </summary>
129129 /// <returns>null if the Request was too large, the Request otherwise.</returns>
130- public static async Task < BuildRequest ? > ReadAsync ( Stream inStream , CancellationToken cancellationToken )
130+ public static async Task < BuildRequest > ReadAsync ( Stream inStream , CancellationToken cancellationToken )
131131 {
132132 // Read the length of the request
133133 var lengthBuffer = new byte [ 4 ] ;
134- Log ( "Reading length of request" ) ;
135134 await ReadAllAsync ( inStream , lengthBuffer , 4 , cancellationToken ) . ConfigureAwait ( false ) ;
136135 var length = BitConverter . ToInt32 ( lengthBuffer , 0 ) ;
137136
138137 // Back out if the request is > 1MB
139138 if ( length > 0x100000 )
140139 {
141- Log ( "Request is over 1MB in length, cancelling read." ) ;
142- return null ;
140+ throw new ArgumentException ( "Request is over 1MB in length" ) ;
143141 }
144142
145143 cancellationToken . ThrowIfCancellationRequested ( ) ;
@@ -150,7 +148,6 @@ public static BuildRequest CreateShutdown()
150148
151149 cancellationToken . ThrowIfCancellationRequested ( ) ;
152150
153- Log ( "Parsing request" ) ;
154151 // Parse the request into the Request data structure.
155152 using ( var reader = new BinaryReader ( new MemoryStream ( requestBuffer ) , Encoding . Unicode ) )
156153 {
@@ -182,8 +179,6 @@ public static BuildRequest CreateShutdown()
182179 using ( var memoryStream = new MemoryStream ( ) )
183180 using ( var writer = new BinaryWriter ( memoryStream , Encoding . Unicode ) )
184181 {
185- // Format the request.
186- Log ( "Formatting request" ) ;
187182 writer . Write ( ProtocolVersion ) ;
188183 writer . Write ( ( uint ) Language ) ;
189184 writer . Write ( CompilerHash ) ;
@@ -203,17 +198,12 @@ public static BuildRequest CreateShutdown()
203198 // Back out if the request is > 1 MB
204199 if ( memoryStream . Length > 0x100000 )
205200 {
206- Log ( "Request is over 1MB in length, cancelling write" ) ;
207- throw new ArgumentOutOfRangeException ( ) ;
201+ throw new ArgumentOutOfRangeException ( "Request is over 1MB in length" ) ;
208202 }
209203
210- // Send the request to the server
211- Log ( "Writing length of request." ) ;
212204 await outStream . WriteAsync ( BitConverter . GetBytes ( length ) , 0 , 4 ,
213205 cancellationToken ) . ConfigureAwait ( false ) ;
214206
215- Log ( "Writing request of size {0}" , length ) ;
216- // Write the request
217207 memoryStream . Position = 0 ;
218208 await memoryStream . CopyToAsync ( outStream , bufferSize : length , cancellationToken : cancellationToken ) . ConfigureAwait ( false ) ;
219209 }
@@ -311,8 +301,6 @@ public async Task WriteAsync(Stream outStream,
311301 using ( var memoryStream = new MemoryStream ( ) )
312302 using ( var writer = new BinaryWriter ( memoryStream , Encoding . Unicode ) )
313303 {
314- // Format the response
315- Log ( "Formatting Response" ) ;
316304 writer . Write ( ( int ) Type ) ;
317305
318306 AddResponseBody ( writer ) ;
@@ -325,16 +313,13 @@ public async Task WriteAsync(Stream outStream,
325313 // Write the length of the response
326314 int length = checked ( ( int ) memoryStream . Length ) ;
327315
328- Log ( "Writing response length" ) ;
329316 // There is no way to know the number of bytes written to
330317 // the pipe stream. We just have to assume all of them are written.
331318 await outStream . WriteAsync ( BitConverter . GetBytes ( length ) ,
332319 0 ,
333320 4 ,
334321 cancellationToken ) . ConfigureAwait ( false ) ;
335322
336- // Write the response
337- Log ( "Writing response of size {0}" , length ) ;
338323 memoryStream . Position = 0 ;
339324 await memoryStream . CopyToAsync ( outStream , bufferSize : length , cancellationToken : cancellationToken ) . ConfigureAwait ( false ) ;
340325 }
@@ -350,14 +335,12 @@ await outStream.WriteAsync(BitConverter.GetBytes(length),
350335 /// <returns></returns>
351336 public static async Task < BuildResponse > ReadAsync ( Stream stream , CancellationToken cancellationToken = default ( CancellationToken ) )
352337 {
353- Log ( "Reading response length" ) ;
354338 // Read the response length
355339 var lengthBuffer = new byte [ 4 ] ;
356340 await ReadAllAsync ( stream , lengthBuffer , 4 , cancellationToken ) . ConfigureAwait ( false ) ;
357341 var length = BitConverter . ToUInt32 ( lengthBuffer , 0 ) ;
358342
359343 // Read the response
360- Log ( "Reading response of length {0}" , length ) ;
361344 var responseBuffer = new byte [ length ] ;
362345 await ReadAllAsync ( stream ,
363346 responseBuffer ,
@@ -381,7 +364,7 @@ await ReadAllAsync(stream,
381364 case ResponseType . Shutdown :
382365 return ShutdownBuildResponse . Create ( reader ) ;
383366 case ResponseType . Rejected :
384- return new RejectedBuildResponse ( ) ;
367+ return RejectedBuildResponse . Create ( reader ) ;
385368 default :
386369 throw new InvalidOperationException ( "Received invalid response type from server." ) ;
387370 }
@@ -505,13 +488,30 @@ protected override void AddResponseBody(BinaryWriter writer) { }
505488
506489 internal sealed class RejectedBuildResponse : BuildResponse
507490 {
491+ public string Reason ;
492+
508493 public override ResponseType Type => ResponseType . Rejected ;
509494
495+ public RejectedBuildResponse ( string reason )
496+ {
497+ Reason = reason ;
498+ }
499+
510500 /// <summary>
511501 /// AnalyzerInconsistency has no body.
512502 /// </summary>
513503 /// <param name="writer"></param>
514- protected override void AddResponseBody ( BinaryWriter writer ) { }
504+ protected override void AddResponseBody ( BinaryWriter writer )
505+ {
506+ WriteLengthPrefixedString ( writer , Reason ) ;
507+ }
508+
509+ public static RejectedBuildResponse Create ( BinaryReader reader )
510+ {
511+ var reason = ReadLengthPrefixedString ( reader ) ;
512+ Debug . Assert ( reason is object ) ;
513+ return new RejectedBuildResponse ( reason ) ;
514+ }
515515 }
516516
517517 // The id numbers below are just random. It's useful to use id numbers
@@ -592,14 +592,13 @@ public static void WriteLengthPrefixedString(BinaryWriter writer, string? value)
592592 /// Reads the value of <see cref="CommitHashAttribute.Hash"/> of the assembly <see cref="BuildRequest"/> is defined in
593593 /// </summary>
594594 /// <returns>The hash value of the current assembly or an empty string</returns>
595- public static string GetCommitHash ( )
595+ public static string ? GetCommitHash ( )
596596 {
597597 var hashAttributes = typeof ( BuildRequest ) . Assembly . GetCustomAttributes < CommitHashAttribute > ( ) ;
598598 var hashAttributeCount = hashAttributes . Count ( ) ;
599599 if ( hashAttributeCount != 1 )
600600 {
601- Log ( $ "Error reading CommitHashAttribute. Exactly 1 attribute is required, found { hashAttributeCount } ") ;
602- return string . Empty ;
601+ return null ;
603602 }
604603 return hashAttributes . Single ( ) . Hash ;
605604 }
@@ -616,21 +615,16 @@ internal static async Task ReadAllAsync(
616615 int totalBytesRead = 0 ;
617616 do
618617 {
619- Log ( "Attempting to read {0} bytes from the stream" ,
620- count - totalBytesRead ) ;
621618 int bytesRead = await stream . ReadAsync ( buffer ,
622619 totalBytesRead ,
623620 count - totalBytesRead ,
624621 cancellationToken ) . ConfigureAwait ( false ) ;
625622 if ( bytesRead == 0 )
626623 {
627- Log ( "Unexpected -- read 0 bytes from the stream." ) ;
628624 throw new EndOfStreamException ( "Reached end of stream before end of read." ) ;
629625 }
630- Log ( "Read {0} bytes" , bytesRead ) ;
631626 totalBytesRead += bytesRead ;
632627 } while ( totalBytesRead < count ) ;
633- Log ( "Finished read" ) ;
634628 }
635629 }
636630}
0 commit comments