Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
using System.Collections.Generic;
using System.CommandLine;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
Expand All @@ -20,6 +22,7 @@ internal sealed class EgressHelper
{
private static Stream StdInStream;
private static CancellationTokenSource CancelSource = new CancellationTokenSource();
private const int ExpectedPayloadProtocolVersion = 1;

internal static CliCommand CreateEgressCommand<TOptions>(EgressProvider<TOptions> provider, Action<ExtensionEgressPayload, TOptions, ILogger> configureOptions = null) where TOptions : class, new()
{
Expand All @@ -35,8 +38,32 @@ internal sealed class EgressHelper
EgressArtifactResult result = new();
try
{
string jsonConfig = Console.ReadLine();
ExtensionEgressPayload configPayload = JsonSerializer.Deserialize<ExtensionEgressPayload>(jsonConfig);
StdInStream = Console.OpenStandardInput();

int dotnetMonitorPayloadProtocolVersion;
long payloadLengthBuffer;
byte[] payloadBuffer;

using (BinaryReader reader = new BinaryReader(StdInStream, Encoding.UTF8, leaveOpen: true))
{
dotnetMonitorPayloadProtocolVersion = reader.ReadInt32();
if (dotnetMonitorPayloadProtocolVersion != ExpectedPayloadProtocolVersion)
{
throw new EgressException(string.Format(CultureInfo.CurrentCulture, Strings.ErrorMessage_IncorrectPayloadVersion, dotnetMonitorPayloadProtocolVersion, ExpectedPayloadProtocolVersion));
}

payloadLengthBuffer = reader.ReadInt64();

if (payloadLengthBuffer < 0)
{
throw new ArgumentOutOfRangeException(nameof(payloadLengthBuffer));
}
}

payloadBuffer = new byte[payloadLengthBuffer];
await ReadExactlyAsync(payloadBuffer, token);

ExtensionEgressPayload configPayload = JsonSerializer.Deserialize<ExtensionEgressPayload>(payloadBuffer);

using ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
{
Expand Down Expand Up @@ -113,9 +140,27 @@ private static async Task GetStream(Stream outputStream, CancellationToken cance
{
const int DefaultBufferSize = 0x10000;

StdInStream = Console.OpenStandardInput();
await StdInStream.CopyToAsync(outputStream, DefaultBufferSize, cancellationToken);
}

private static async Task ReadExactlyAsync(Memory<byte> buffer, CancellationToken token)
{
#if NET7_0_OR_GREATER
await StdInStream.ReadExactlyAsync(buffer, token);
#else
int totalRead = 0;
while (totalRead < buffer.Length)
{
int read = await StdInStream.ReadAsync(buffer.Slice(totalRead), token).ConfigureAwait(false);
if (read == 0)
{
throw new EndOfStreamException();
}

totalRead += read;
}
#endif
}
}

internal sealed class ExtensionEgressPayload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,20 @@
<PackageReference Include="Microsoft.Extensions.Logging" Version="$(MicrosoftExtensionsLoggingVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="$(MicrosoftExtensionsLoggingConsoleVersion)" />
</ItemGroup>

<ItemGroup>
<Compile Update="Strings.Designer.cs">
<DependentUpon>Strings.resx</DependentUpon>
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
</Compile>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Strings.resx">
<SubType>Designer</SubType>
<LastGenOutput>Strings.Designer.cs</LastGenOutput>
<Generator>ResXFileCodeGenerator</Generator>
</EmbeddedResource>
</ItemGroup>
</Project>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema

Version 2.0

The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.

Example:

... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>

There are any number of "resheader" rows that contain simple
name/value pairs.

Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.

The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:

Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.

mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="ErrorMessage_GenericEgressFailure" xml:space="preserve">
<value>The egress operation failed due to an internal error.</value>
</data>
<data name="ErrorMessage_IncorrectPayloadVersion" xml:space="preserve">
<value>Unsupported egress extension protocol. Please ensure both dotnet-monitor and this extension are up-to-date. Dotnet-Monitor Version: v{0}; Extension Version: v{1}</value>
<comment>Gets the format string for failing an egress operation due to using the incorrect payload protocol version
2 Format Parameters:
0. extensionVersion: The extension's version
1. payloadVersion: The payload's version</comment>
</data>
</root>
23 changes: 18 additions & 5 deletions src/Tools/dotnet-monitor/Egress/Extension/EgressExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -26,6 +27,7 @@ internal partial class EgressExtension : IExtension, IEgressExtension
private readonly ILogger<EgressExtension> _logger;
private readonly ExtensionManifest _manifest;
private readonly IDictionary<string, string> _processEnvironmentVariables = new Dictionary<string, string>();
private const int PayloadProtocolVersion = 1;

private static readonly TimeSpan WaitForProcessExitTimeout = TimeSpan.FromMilliseconds(2000);

Expand Down Expand Up @@ -75,9 +77,6 @@ public async Task<EgressArtifactResult> EgressArtifact(
{
_manifest.Validate();

// This is really weird, yes, but this is one of 2 overloads for [Stream].WriteAsync(...) that supports a CancellationToken, so we use a ReadOnlyMemory<char> instead of a string.
ReadOnlyMemory<char> NewLine = new ReadOnlyMemory<char>("\r\n".ToCharArray());

ProcessStartInfo pStart = new ProcessStartInfo()
{
RedirectStandardInput = true,
Expand Down Expand Up @@ -148,9 +147,23 @@ public async Task<EgressArtifactResult> EgressArtifact(

parser.BeginReading();

await JsonSerializer.SerializeAsync<ExtensionEgressPayload>(p.StandardInput.BaseStream, payload, options: null, token);
await p.StandardInput.WriteAsync(NewLine, token);
// p.StandardInput.BaseStream Format: Version (int), Payload Length (long), Payload, Artifact
using Stream intermediateStream = new MemoryStream();
await JsonSerializer.SerializeAsync(intermediateStream, payload, options: null, token);

using (BinaryWriter writer = new BinaryWriter(p.StandardInput.BaseStream, Encoding.UTF8, leaveOpen: true))
{
writer.Write(PayloadProtocolVersion);
writer.Write(intermediateStream.Position);

intermediateStream.Position = 0;

writer.Flush();
}

await intermediateStream.CopyToAsync(p.StandardInput.BaseStream, token);
await p.StandardInput.BaseStream.FlushAsync(token);

_logger.ExtensionConfigured(pStart.FileName, p.Id);

await action(p.StandardInput.BaseStream, token);
Expand Down