Skip to content

Commit

Permalink
feat!: Add support for returning result (#9)
Browse files Browse the repository at this point in the history
* feat!: Add support for returning result

Added support for returning results from remote methods.
In doing so, a need for embracing the async nature of the problem
was exposed, so now the protocol interfaces have to be declared with
methods returning either Task or Task<T>.

* Update src/apps/H.Ipc.Apps.Wpf/H.Ipc.Apps.Wpf.csproj

* fix: Cleanup according to review

- Avoid adding 'using' statements in generated code
- Revert some package version updates
- Remove temporary (debug/learning) code
- Add doc comments

* Update src/libs/H.Ipc.Generator/HIpcGenerator.cs

---------

Co-authored-by: Konstantin S <[email protected]>
  • Loading branch information
bitwreckage and HavenDV committed Apr 27, 2024
1 parent 025ae4e commit 0ab303f
Show file tree
Hide file tree
Showing 14 changed files with 492 additions and 93 deletions.
15 changes: 8 additions & 7 deletions src/apps/H.Ipc.Apps.Wpf/ClientWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Windows;
using System.Diagnostics.CodeAnalysis;
using System.Windows;
using H.Pipes;

namespace H.Ipc.Apps.Wpf;
Expand Down Expand Up @@ -73,11 +74,11 @@ private async void Window_Unloaded(object _, RoutedEventArgs e)
}
}

private void RaiseEvent1Button_Click(object sender, RoutedEventArgs e)
private async void RaiseEvent1Button_Click(object sender, RoutedEventArgs e)
{
try
{
ActionServiceClient.ShowTrayIcon();
await ActionServiceClient.ShowTrayIcon();
//if (Instance == null)
//{
// return;
Expand All @@ -92,11 +93,11 @@ private void RaiseEvent1Button_Click(object sender, RoutedEventArgs e)
}
}

private void RaiseEvent3Button_Click(object sender, RoutedEventArgs e)
private async void RaiseEvent3Button_Click(object sender, RoutedEventArgs e)
{
try
{
ActionServiceClient.HideTrayIcon();
await ActionServiceClient.HideTrayIcon();
//if (Instance == null)
//{
// return;
Expand Down Expand Up @@ -129,11 +130,11 @@ private void Method1Button_Click(object sender, RoutedEventArgs e)
}
}

private void Method2Button_Click(object sender, RoutedEventArgs e)
private async void Method2Button_Click(object sender, RoutedEventArgs e)
{
try
{
ActionServiceClient.SendText(Method2ArgumentTextBox.Text);
await ActionServiceClient.SendText(Method2ArgumentTextBox.Text);
}
catch (Exception exception)
{
Expand Down
33 changes: 26 additions & 7 deletions src/apps/H.Ipc.Apps.Wpf/IActionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

namespace H.Ipc.Apps.Wpf;

public class Person
{
public string? Name { get; set; }
}

public interface IActionService
{
void SendText(string text);
void ShowTrayIcon();
void HideTrayIcon();
Task SendText(string text);
Task ShowTrayIcon();
Task HideTrayIcon();

//event EventHandler<string> TextReceived;
Task<int> CalculateResult();
Task<Person> GetPerson();
}

[H.IpcGenerators.IpcClient]
Expand All @@ -19,18 +25,31 @@ public partial class ActionServiceClient : IActionService
[H.IpcGenerators.IpcServer]
public partial class ActionService : IActionService
{
public void ShowTrayIcon()
public Task ShowTrayIcon()
{
MessageBox.Show(nameof(ShowTrayIcon));
return Task.CompletedTask;
}

public void HideTrayIcon()
public Task HideTrayIcon()
{
MessageBox.Show(nameof(HideTrayIcon));
return Task.CompletedTask;
}

public Task<Person> GetPerson()
{
return Task.FromResult(new Person());
}

public Task<int> CalculateResult()
{
return Task.FromResult(10);
}

public void SendText(string text)
public Task SendText(string text)
{
MessageBox.Show(text);
return Task.CompletedTask;
}
}
1 change: 1 addition & 0 deletions src/libs/H.Ipc.Generator/H.Ipc.Generator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<IsPackable>false</IsPackable>
<GenerateDocumentationFile>false</GenerateDocumentationFile>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<NoWarn>$(NoWarn);CA1014;CA1031</NoWarn>
</PropertyGroup>

Expand Down
7 changes: 4 additions & 3 deletions src/libs/H.Ipc.Generator/HIpcGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.CodeAnalysis;
using H.Generators.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace H.Generators;
Expand Down Expand Up @@ -65,14 +66,14 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
InterfaceName: interfaceName,
Methods: methods);
}

private static FileWithName GetClientSourceCode(ClassData @class)
{
return new FileWithName(
Name: $"{@class.Name}.IpcClient.generated.cs",
Text: SourceGenerationHelper.GenerateClientImplementation(@class));
}

private static FileWithName GetServerSourceCode(ClassData @class)
{
return new FileWithName(
Expand Down
Loading

0 comments on commit 0ab303f

Please sign in to comment.