Skip to content

Commit

Permalink
Extend Microsoft.Diagnostics.NETCore.Client to support TCPIP usage on…
Browse files Browse the repository at this point in the history
… Dotnet/Runtime
  • Loading branch information
mdh1418 committed Feb 14, 2022
1 parent 1e4ea38 commit 0bfbb2e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/Microsoft.Diagnostics.NETCore.Client/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#if DIAGNOSTICS_RUNTIME
using System;
using System.IO;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("common")]
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ public enum PortType
public enum TransportType
{
NamedPipe,
UnixDomainSocket
UnixDomainSocket,
#if DIAGNOSTICS_RUNTIME
TcpSocket
#endif
}

PortType _portType;

TransportType _transportType;

// For TcpSocket TransportType, the address format will be <hostname_or_ip>:<port>
public string Address { get; }

public bool IsConnectConfig => _portType == PortType.Connect;
Expand Down Expand Up @@ -57,6 +61,12 @@ public IpcEndpointConfig(string address, TransportType transportType, PortType p
throw new PlatformNotSupportedException($"{UnixDomainSocketSchema} is not supported on Windows, use {NamedPipeSchema}.");
break;
}
#if DIAGNOSTICS_RUNTIME
case TransportType.TcpSocket:
{
break;
}
#endif
default:
{
throw new NotSupportedException($"{transportType} not supported.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public IpcSocket(AddressFamily addressFamily, SocketType socketType, ProtocolTyp
{
}

// .NET 6 implements this method directly on Socket, but for earlier runtimes we need a polyfill
#if !NET6_0
public async Task<Socket> AcceptAsync(CancellationToken token)
{
using (token.Register(() => Close(0)))
Expand All @@ -44,6 +46,7 @@ public async Task<Socket> AcceptAsync(CancellationToken token)
}
}
}
#endif

public virtual void Connect(EndPoint remoteEP, TimeSpan timeout)
{
Expand All @@ -60,6 +63,8 @@ public virtual void Connect(EndPoint remoteEP, TimeSpan timeout)
}
}

// .NET 6 implements this method directly on Socket, but for earlier runtimes we need a polyfill
#if !NET6_0
public async Task ConnectAsync(EndPoint remoteEP, CancellationToken token)
{
using (token.Register(() => Close(0)))
Expand All @@ -82,6 +87,6 @@ public async Task ConnectAsync(EndPoint remoteEP, CancellationToken token)
}
}
}

#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Threading;
Expand Down Expand Up @@ -69,6 +70,15 @@ public static Stream Connect(IpcEndpointConfig config, TimeSpan timeout)
socket.Connect(new IpcUnixDomainSocketEndPoint(config.Address), timeout);
return new ExposedSocketNetworkStream(socket, ownsSocket: true);
}
#if DIAGNOSTICS_RUNTIME
else if (config.Transport == IpcEndpointConfig.TransportType.TcpSocket)
{
var tcpClient = new TcpClient ();
var endPoint = new IpcTcpSocketEndPoint(config.Address);
tcpClient.Connect(endPoint.EndPoint);
return tcpClient.GetStream();
}
#endif
else
{
throw new ArgumentException($"Unsupported IpcEndpointConfig transport type {config.Transport}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
<IsShipping>true</IsShipping>
</PropertyGroup>

<PropertyGroup Condition="'$(GitHubRepositoryName)' == 'runtime'">
<DefineConstants>$(DefineConstants);DIAGNOSTICS_RUNTIME</DefineConstants>
<NoWarn>CS1591,CS8073,CS0162</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="$(MicrosoftBclAsyncInterfacesVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="$(MicrosoftExtensionsLoggingVersion)" />
Expand All @@ -29,4 +34,8 @@
<InternalsVisibleTo Include="Microsoft.Diagnostics.Monitoring.WebApi" />
<InternalsVisibleTo Include="Microsoft.Diagnostics.NETCore.Client.UnitTests" />
</ItemGroup>

<ItemGroup>
<Compile Condition="'$(GitHubRepositoryName)' == 'runtime'" Include="**/*.cs" />
</ItemGroup>
</Project>

0 comments on commit 0bfbb2e

Please sign in to comment.