Skip to content

Commit

Permalink
Repalce namedpipe IPC by Socket IPC
Browse files Browse the repository at this point in the history
  • Loading branch information
rokonec committed Jun 7, 2024
1 parent babfa61 commit 922d4be
Showing 1 changed file with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.IO.Pipes;
using System.Runtime.Serialization.Formatters.Binary;
Expand Down Expand Up @@ -265,7 +266,6 @@ public void SocketCtr_SocketInformation_WhenProtocolInformationTooShort_Throws()
public abstract class PolymorphicTests<T> where T : SocketHelperBase, new()
{
private static readonly T Helper = new T();
private readonly string _ipcPipeName = Path.GetRandomFileName();

private static void WriteSocketInfo(Stream stream, SocketInformation socketInfo)
{
Expand Down Expand Up @@ -317,19 +317,21 @@ public async Task DuplicateAndClose_TcpServerHandler(AddressFamily addressFamily
// Async is allowed on the listener:
using Socket handlerOriginal = await listener.AcceptAsync();

// pipe used to exchange socket info
await using NamedPipeServerStream pipeServerStream =
new NamedPipeServerStream(_ipcPipeName, PipeDirection.Out);
// socket used to exchange duplicated socket info with server code
using Socket ipcServerListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ipcServerListener.BindToAnonymousPort(IPAddress.Loopback);
ipcServerListener.Listen(1);
string ipcPortString = ((IPEndPoint)ipcServerListener.LocalEndPoint).Port.ToString(CultureInfo.InvariantCulture);

if (sameProcess)
{
Task handlerCode = Task.Run(() => HandlerServerCode(_ipcPipeName));
Task handlerCode = Task.Run(() => HandlerServerCode(ipcPortString));
RunCommonHostLogic(Environment.ProcessId);
await handlerCode;
}
else
{
RemoteInvokeHandle hServerProc = RemoteExecutor.Invoke(HandlerServerCode, _ipcPipeName);
RemoteInvokeHandle hServerProc = RemoteExecutor.Invoke(HandlerServerCode, ipcPortString);

// Since RunCommonHostLogic can throw, we need to make sure the server process is disposed
try
Expand All @@ -344,23 +346,27 @@ public async Task DuplicateAndClose_TcpServerHandler(AddressFamily addressFamily

void RunCommonHostLogic(int processId)
{
pipeServerStream.WaitForConnection();
using Socket ipcServer = ipcServerListener.Accept();

// Duplicate the socket:
SocketInformation socketInfo = handlerOriginal.DuplicateAndClose(processId);
WriteSocketInfo(pipeServerStream, socketInfo);
// Send socketInfo to server code
using var ipcStream = new NetworkStream(ipcServer, false);
WriteSocketInfo(new NetworkStream(ipcServer), socketInfo);

// Send client data:
client.Send(TestBytes);
}

static async Task<int> HandlerServerCode(string ipcPipeName)
static async Task<int> HandlerServerCode(string ipcPortString)
{
await using NamedPipeClientStream pipeClientStream =
new NamedPipeClientStream(".", ipcPipeName, PipeDirection.In);
pipeClientStream.Connect();
int ipcPort = int.Parse(ipcPortString, CultureInfo.InvariantCulture);
using Socket ipcClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
await ipcClient.ConnectAsync(IPAddress.Loopback, ipcPort);

await using var ipcStream = new NetworkStream(ipcClient, true);
SocketInformation socketInfo = ReadSocketInfo(ipcStream);

SocketInformation socketInfo = ReadSocketInfo(pipeClientStream);
using Socket handler = new Socket(socketInfo);

Assert.True(handler.IsBound);
Expand Down

0 comments on commit 922d4be

Please sign in to comment.