-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6442 from peppy/named-pipes-for-ipc
Switch to using named pipes for IPC
- Loading branch information
Showing
9 changed files
with
461 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
#nullable disable | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using NUnit.Framework; | ||
using osu.Framework.Platform; | ||
using osu.Framework.Tests.IO; | ||
|
||
namespace osu.Framework.Tests.Platform | ||
{ | ||
[TestFixture] | ||
public partial class IPCTest | ||
{ | ||
[Test] | ||
public void TestNoPipeNameSpecifiedCoexist() | ||
{ | ||
using (var host1 = new BackgroundGameHeadlessGameHost(@"server", new HostOptions { IPCPipeName = null })) | ||
using (var host2 = new HeadlessGameHost(@"client", new HostOptions { IPCPipeName = null })) | ||
{ | ||
Assert.IsTrue(host1.IsPrimaryInstance, @"Host 1 wasn't able to bind"); | ||
Assert.IsTrue(host2.IsPrimaryInstance, @"Host 2 wasn't able to bind"); | ||
} | ||
} | ||
|
||
[Test] | ||
public void TestDifferentPipeNamesCoexist() | ||
{ | ||
using (var host1 = new BackgroundGameHeadlessGameHost(@"server", new HostOptions { IPCPipeName = "test-app" })) | ||
using (var host2 = new HeadlessGameHost(@"client", new HostOptions { IPCPipeName = "test-app-2" })) | ||
{ | ||
Assert.IsTrue(host1.IsPrimaryInstance, @"Host 1 wasn't able to bind"); | ||
Assert.IsTrue(host2.IsPrimaryInstance, @"Host 2 wasn't able to bind"); | ||
} | ||
} | ||
|
||
[Test] | ||
public void TestOneWay() | ||
{ | ||
using (var server = new BackgroundGameHeadlessGameHost(@"server", new HostOptions { IPCPipeName = "test-app" })) | ||
using (var client = new HeadlessGameHost(@"client", new HostOptions { IPCPipeName = "test-app" })) | ||
{ | ||
Assert.IsTrue(server.IsPrimaryInstance, @"Server wasn't able to bind"); | ||
Assert.IsFalse(client.IsPrimaryInstance, @"Client was able to bind when it shouldn't have been able to"); | ||
|
||
var serverChannel = new IpcChannel<Foobar>(server); | ||
var clientChannel = new IpcChannel<Foobar>(client); | ||
|
||
async Task waitAction() | ||
{ | ||
using (var received = new SemaphoreSlim(0)) | ||
{ | ||
serverChannel.MessageReceived += message => | ||
{ | ||
Assert.AreEqual("example", message.Bar); | ||
// ReSharper disable once AccessToDisposedClosure | ||
received.Release(); | ||
return null; | ||
}; | ||
|
||
await clientChannel.SendMessageAsync(new Foobar { Bar = "example" }).ConfigureAwait(false); | ||
|
||
if (!await received.WaitAsync(10000).ConfigureAwait(false)) | ||
throw new TimeoutException("Message was not received in a timely fashion"); | ||
} | ||
} | ||
|
||
Assert.IsTrue(Task.Run(waitAction).Wait(10000), @"Message was not received in a timely fashion"); | ||
} | ||
} | ||
|
||
[Test] | ||
public void TestTwoWay() | ||
{ | ||
using (var server = new BackgroundGameHeadlessGameHost(@"server", new HostOptions { IPCPipeName = "test-app" })) | ||
using (var client = new HeadlessGameHost(@"client", new HostOptions { IPCPipeName = "test-app" })) | ||
{ | ||
Assert.IsTrue(server.IsPrimaryInstance, @"Server wasn't able to bind"); | ||
Assert.IsFalse(client.IsPrimaryInstance, @"Client was able to bind when it shouldn't have been able to"); | ||
|
||
var serverChannel = new IpcChannel<Foobar, Foobar>(server); | ||
var clientChannel = new IpcChannel<Foobar, Foobar>(client); | ||
|
||
async Task waitAction() | ||
{ | ||
using (var received = new SemaphoreSlim(0)) | ||
{ | ||
serverChannel.MessageReceived += message => | ||
{ | ||
Assert.AreEqual("example", message.Bar); | ||
// ReSharper disable once AccessToDisposedClosure | ||
received.Release(); | ||
|
||
return new Foobar { Bar = "test response" }; | ||
}; | ||
|
||
var response = await clientChannel.SendMessageWithResponseAsync(new Foobar { Bar = "example" }).ConfigureAwait(false); | ||
|
||
if (!await received.WaitAsync(10000).ConfigureAwait(false)) | ||
throw new TimeoutException("Message was not received in a timely fashion"); | ||
|
||
Assert.That(response?.Bar, Is.EqualTo("test response")); | ||
} | ||
} | ||
|
||
Assert.IsTrue(Task.Run(waitAction).Wait(10000), @"Message was not received in a timely fashion"); | ||
} | ||
} | ||
|
||
[Test] | ||
public void TestIpcLegacyPortSupport() | ||
{ | ||
#pragma warning disable CS0618 // Type or member is obsolete | ||
using (var server = new BackgroundGameHeadlessGameHost(@"server", new HostOptions { IPCPort = 45356 })) | ||
using (var client = new HeadlessGameHost(@"client", new HostOptions { IPCPort = 45356 })) | ||
#pragma warning restore CS0618 // Type or member is obsolete | ||
{ | ||
Assert.IsTrue(server.IsPrimaryInstance, @"Server wasn't able to bind"); | ||
Assert.IsFalse(client.IsPrimaryInstance, @"Client was able to bind when it shouldn't have been able to"); | ||
|
||
var serverChannel = new IpcChannel<Foobar, object>(server); | ||
var clientChannel = new IpcChannel<Foobar, object>(client); | ||
|
||
async Task waitAction() | ||
{ | ||
using (var received = new SemaphoreSlim(0)) | ||
{ | ||
serverChannel.MessageReceived += message => | ||
{ | ||
Assert.AreEqual("example", message.Bar); | ||
// ReSharper disable once AccessToDisposedClosure | ||
received.Release(); | ||
return null; | ||
}; | ||
|
||
await clientChannel.SendMessageAsync(new Foobar { Bar = "example" }).ConfigureAwait(false); | ||
|
||
if (!await received.WaitAsync(10000).ConfigureAwait(false)) | ||
throw new TimeoutException("Message was not received in a timely fashion"); | ||
} | ||
} | ||
|
||
Assert.IsTrue(Task.Run(waitAction).Wait(10000), @"Message was not received in a timely fashion"); | ||
} | ||
} | ||
|
||
private class Foobar | ||
{ | ||
public string Bar; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
using System; | ||
using System.Globalization; | ||
using osu.Framework.Platform; | ||
|
||
namespace osu.Framework | ||
|
@@ -11,12 +13,20 @@ namespace osu.Framework | |
public class HostOptions | ||
{ | ||
/// <summary> | ||
/// The IPC port to bind. This port should be between 1024 and 49151, | ||
/// should be shared by all instances of a given osu!framework app, | ||
/// but be distinct from IPC ports specified by other osu!framework apps. | ||
/// Use <see cref="IPCPipeName"/> instead. | ||
/// </summary> | ||
[Obsolete("Use IPCPipeName instead.")] // can be removed 20250603. | ||
public int? IPCPort | ||
{ | ||
set => IPCPipeName = value?.ToString(CultureInfo.InvariantCulture); | ||
} | ||
|
||
/// <summary> | ||
/// The IPC pipe name to bind. This should be shared by all instances of | ||
/// an osu!framework app that want to perform inter-process communications. | ||
/// See <see cref="IIpcHost"/> for more details on usage. | ||
/// </summary> | ||
public int? IPCPort { get; set; } | ||
public string? IPCPipeName { get; set; } | ||
|
||
/// <summary> | ||
/// Whether this is a portable installation. Will cause all game files to be placed alongside the executable, rather than in the standard data directory. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.