Skip to content

Commit

Permalink
feat: HelloRpc - server-to-client push demo
Browse files Browse the repository at this point in the history
  • Loading branch information
alexyakunin committed Dec 8, 2023
1 parent 8d5b4c5 commit 8ce88f3
Show file tree
Hide file tree
Showing 31 changed files with 207 additions and 68 deletions.
4 changes: 2 additions & 2 deletions docs/tutorial/Tutorial.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Stl.Fusion.Server" Version="6.8.4" />
<PackageReference Include="Stl.Fusion.Server" Version="6.8.6" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="System.CommandLine.DragonFruit" Version="0.4.0-alpha.22272.1" />
</ItemGroup>
Expand All @@ -30,7 +30,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Stl.Generators" Version="6.8.4" />
<PackageReference Include="Stl.Generators" Version="6.8.6" />
</ItemGroup>

</Project>
6 changes: 3 additions & 3 deletions src/Benchmark/Benchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0-rc.*" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0-rc.*" />
<PackageReference Include="Stl.Fusion.Server" Version="6.8.4" />
<PackageReference Include="Stl.RestEase" Version="6.8.4" />
<PackageReference Include="Stl.Fusion.Server" Version="6.8.6" />
<PackageReference Include="Stl.RestEase" Version="6.8.6" />
</ItemGroup>

<!-- Roslyn generators/analyzers -->
Expand All @@ -38,6 +38,6 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="RestEase.SourceGenerator" Version="1.6.4" />
<PackageReference Include="Stl.Generators" Version="6.8.4" />
<PackageReference Include="Stl.Generators" Version="6.8.6" />
</ItemGroup>
</Project>
12 changes: 12 additions & 0 deletions src/Benchmark/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"profiles": {
"Benchmark": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:54430;http://localhost:54431"
}
}
}
6 changes: 3 additions & 3 deletions src/Blazor/Abstractions/Abstractions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="8.0.0-rc.*" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
<PackageReference Include="Stl.Fusion" Version="6.8.4" />
<PackageReference Include="Stl.Fusion.Ext.Contracts" Version="6.8.4" />
<PackageReference Include="Stl.Fusion" Version="6.8.6" />
<PackageReference Include="Stl.Fusion.Ext.Contracts" Version="6.8.6" />
</ItemGroup>

<!-- Roslyn generators/analyzers -->
Expand All @@ -22,7 +22,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Stl.Generators" Version="6.8.4" />
<PackageReference Include="Stl.Generators" Version="6.8.6" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/Blazor/ConsoleClient/ConsoleClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Stl.Fusion" Version="6.8.4" />
<PackageReference Include="Stl.Fusion" Version="6.8.6" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions src/Blazor/Server/Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.0" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.0.2" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="2.0.1" />
<PackageReference Include="Stl.Fusion.Server" Version="6.8.4" />
<PackageReference Include="Stl.Fusion.EntityFramework" Version="6.8.4" />
<PackageReference Include="Stl.RestEase" Version="6.8.4" />
<PackageReference Include="Stl.Fusion.Server" Version="6.8.6" />
<PackageReference Include="Stl.Fusion.EntityFramework" Version="6.8.6" />
<PackageReference Include="Stl.RestEase" Version="6.8.6" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions src/Blazor/UI/UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
<PackageReference Include="Stl.Fusion.Blazor" Version="6.8.4" />
<PackageReference Include="Stl.Fusion.Blazor.Authentication" Version="6.8.4" />
<PackageReference Include="Stl.Fusion.Ext.Contracts" Version="6.8.4" />
<PackageReference Include="Stl.Fusion.Blazor" Version="6.8.6" />
<PackageReference Include="Stl.Fusion.Blazor.Authentication" Version="6.8.6" />
<PackageReference Include="Stl.Fusion.Ext.Contracts" Version="6.8.6" />
<PackageReference Include="UAParser" Version="3.1.46" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions src/HelloBlazorHybrid/Abstractions/Abstractions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Stl.Fusion" Version="6.8.4" />
<PackageReference Include="Stl.Fusion" Version="6.8.6" />
</ItemGroup>

<!-- Roslyn generators/analyzers -->
Expand All @@ -20,7 +20,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Stl.Generators" Version="6.8.4" />
<PackageReference Include="Stl.Generators" Version="6.8.6" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions src/HelloBlazorHybrid/Server/Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<PackageReference Include="Microsoft.AspNetCore.Authentication.MicrosoftAccount" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection.EntityFrameworkCore" Version="8.0.0-rc.*" />
<PackageReference Include="Stl.Fusion.EntityFramework" Version="6.8.4" />
<PackageReference Include="Stl.Fusion.Server" Version="6.8.4" />
<PackageReference Include="Stl.Fusion.EntityFramework" Version="6.8.6" />
<PackageReference Include="Stl.Fusion.Server" Version="6.8.6" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/HelloBlazorHybrid/Services/Services.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Stl.Fusion.EntityFramework" Version="6.8.4" />
<PackageReference Include="Stl.Fusion.EntityFramework" Version="6.8.6" />
</ItemGroup>

</Project>
6 changes: 3 additions & 3 deletions src/HelloBlazorHybrid/UI/UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
<PackageReference Include="Stl.Fusion.Blazor" Version="6.8.4" />
<PackageReference Include="Stl.Fusion.Blazor.Authentication" Version="6.8.4" />
<PackageReference Include="Stl.Fusion.Ext.Contracts" Version="6.8.4" />
<PackageReference Include="Stl.Fusion.Blazor" Version="6.8.6" />
<PackageReference Include="Stl.Fusion.Blazor.Authentication" Version="6.8.6" />
<PackageReference Include="Stl.Fusion.Ext.Contracts" Version="6.8.6" />
<PackageReference Include="UAParser" Version="3.1.47" />
</ItemGroup>

Expand Down
6 changes: 3 additions & 3 deletions src/HelloBlazorServer/HelloBlazorServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Stl.Fusion.Blazor" Version="6.8.4" />
<PackageReference Include="Stl.Fusion.Server" Version="6.8.4" />
<PackageReference Include="Stl.Fusion.Blazor" Version="6.8.6" />
<PackageReference Include="Stl.Fusion.Server" Version="6.8.6" />
</ItemGroup>

<!-- Roslyn generators/analyzers -->
<ItemGroup>
<PackageReference Include="Stl.Generators" Version="6.8.4" />
<PackageReference Include="Stl.Generators" Version="6.8.6" />
</ItemGroup>
</Project>
8 changes: 4 additions & 4 deletions src/HelloCart/HelloCart.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.0" />
<PackageReference Include="Stl.Fusion.EntityFramework" Version="6.8.4" />
<PackageReference Include="Stl.Fusion.EntityFramework.Redis" Version="6.8.4" />
<PackageReference Include="Stl.Fusion.Server" Version="6.8.4" />
<PackageReference Include="Stl.Fusion.EntityFramework" Version="6.8.6" />
<PackageReference Include="Stl.Fusion.EntityFramework.Redis" Version="6.8.6" />
<PackageReference Include="Stl.Fusion.Server" Version="6.8.6" />
</ItemGroup>

<!-- Roslyn generators/analyzers -->
Expand All @@ -22,7 +22,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Stl.Generators" Version="6.8.4" />
<PackageReference Include="Stl.Generators" Version="6.8.6" />
</ItemGroup>

</Project>
12 changes: 12 additions & 0 deletions src/HelloCart/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"profiles": {
"HelloCart": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:54424;http://localhost:54425"
}
}
}
4 changes: 2 additions & 2 deletions src/HelloCommandR/HelloCommandR.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Stl.Fusion" Version="6.8.4" />
<PackageReference Include="Stl.Fusion" Version="6.8.6" />
</ItemGroup>

<!-- Roslyn generators/analyzers -->
<ItemGroup>
<PackageReference Include="Stl.Generators" Version="6.8.4" />
<PackageReference Include="Stl.Generators" Version="6.8.6" />
</ItemGroup>
</Project>
17 changes: 17 additions & 0 deletions src/HelloRpc/ClientNotifier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Stl.Rpc;

namespace Samples.HelloRpc;

public interface IClientNotifier : IRpcService
{
Task Notify(Symbol peerKey, DateTime time, CancellationToken cancellationToken = default);
}

public class ClientNotifier : IClientNotifier
{
public Task Notify(Symbol peerKey, DateTime time, CancellationToken cancellationToken = default)
{
Console.WriteLine($"Server-to-client push: {time}");
return Task.CompletedTask;
}
}
35 changes: 33 additions & 2 deletions src/HelloRpc/Greeter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Runtime.Serialization;
using MemoryPack;
using Stl.Rpc;
using Stl.Rpc.Infrastructure;

namespace Samples.HelloRpc;

Expand All @@ -16,8 +17,38 @@ public partial record Message(
[property: DataMember(Order = 0), MemoryPackOrder(0)] string Text
);

public class Greeter : IGreeter
public class Greeter(IClientNotifier clientNotifier) : IGreeter
{
private readonly Dictionary<RpcPeer, Task> _notificationTasks = new();

public async Task<Message> SayHello(string name, CancellationToken cancellationToken = default)
=> new($"Hello, {name}!");
{
StartClientNotifications(RpcInboundContext.Current!.Peer);
return new Message($"Hello, {name}!");
}

private void StartClientNotifications(RpcPeer peer)
{
lock (_notificationTasks) {
if (_notificationTasks.ContainsKey(peer))
return;

_notificationTasks.Add(peer, Task.Run(async () => {
var cancellationToken = peer.StopToken;
try {
while (true) {
await Task.Delay(1000, cancellationToken).ConfigureAwait(false);
await clientNotifier.Notify(peer.Ref.Key, DateTime.Now, cancellationToken).ConfigureAwait(false);
}
}
finally {
lock (_notificationTasks) {
_notificationTasks.Remove(peer);
}
}
}));


}
}
}
7 changes: 5 additions & 2 deletions src/HelloRpc/HelloRpc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Stl.Rpc.Server" Version="6.8.4" />
<PackageReference Include="Stl.Rpc.Server" Version="6.8.6" />
</ItemGroup>

<!-- Roslyn generators/analyzers -->
Expand All @@ -19,6 +19,9 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Stl.Generators" Version="6.8.4" />
<PackageReference Include="Stl.Generators" Version="6.8.6" />
</ItemGroup>
<ItemGroup>
<None Remove="Properties\launchSettings.json" />
</ItemGroup>
</Project>
50 changes: 39 additions & 11 deletions src/HelloRpc/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
using Microsoft.AspNetCore.Builder;
using System.Diagnostics;
using Microsoft.AspNetCore.Builder;
using Samples.HelloRpc;
using Stl.Rpc;
using Stl.Rpc.Server;
using static System.Console;

#pragma warning disable ASP0000

RpcServiceRegistry.ConstructionDumpLogLevel = LogLevel.Information;
var stopTokenSource = new CancellationTokenSource();
var cancellationToken = stopTokenSource.Token;
CancelKeyPress += (_, _) => stopTokenSource.Cancel();

var baseUrl = "http://localhost:22222/";
await (args switch {
[ "server" ] => RunServer(),
Expand All @@ -16,29 +22,51 @@
async Task RunServer()
{
var builder = WebApplication.CreateBuilder();
builder.Logging.ClearProviders().AddDebug();
builder.Logging.ClearProviders().AddConsole();
var rpc = builder.Services.AddRpc();
rpc.AddWebSocketServer();
rpc.AddServer<IGreeter, Greeter>();
rpc.AddClient<IClientNotifier>();
builder.Services.AddSingleton<RpcCallRouter>(c => {
// This service is necessary only because of IClientNotifier, which requires call routing
RpcHub? rpcHub = null;
return (methodDef, args) => {
rpcHub ??= c.RpcHub(); // We can't resolve it earlier, coz otherwise it will trigger recursion
if (methodDef.Service.Type == typeof(IClientNotifier)) {
var peerRef = new RpcPeerRef(args.Get<Symbol>(0), true);
return rpcHub.GetServerPeer(peerRef);
}
return rpcHub.GetClientPeer(RpcPeerRef.Default);
};
});
var app = builder.Build();

app.Urls.Add(baseUrl);
app.UseWebSockets();
app.MapRpcWebSocketServer();
await app.RunAsync(baseUrl);

await app.StartAsync(cancellationToken);
WriteLine("Server started, press Ctrl-C to stop it.");
await Task.Delay(TimeSpan.FromDays(1), cancellationToken).SilentAwait();
WriteLine("Server is shutting down.");
await app.StopAsync();
}

async Task RunClient()
{
var services = new ServiceCollection();
services.AddLogging(logging => logging.AddConsole());
var rpc = services.AddRpc();
rpc.AddWebSocketClient(baseUrl);
rpc.AddClient<IGreeter>();
var serviceProvider = services.BuildServiceProvider();
rpc.AddServer<IClientNotifier, ClientNotifier>();

using var serviceProvider = services.BuildServiceProvider();
var greeter = serviceProvider.GetRequiredService<IGreeter>();
while (true) {
Write("Your name: ");
var name = ReadLine() ?? "";
var message = await greeter.SayHello(name);
WriteLine(message);
}

Write("Your name: ");
var name = ReadLine() ?? "";
var message = await greeter.SayHello(name, cancellationToken);
WriteLine(message);
WriteLine("Press Ctrl-C to exit.");
await Task.Delay(TimeSpan.FromDays(1), cancellationToken).SilentAwait();
}
Loading

0 comments on commit 8ce88f3

Please sign in to comment.