Archived
0
0
Fork 0

add basic GRPC communication

This commit is contained in:
Daryl Ronningen 2022-11-16 20:22:00 -08:00
parent 6e664ed6df
commit 84ac974427
Signed by: Daryl Ronningen
GPG key ID: FD23F0C934A5EC6B
6 changed files with 121 additions and 3 deletions

View file

@ -1,3 +1,10 @@
using Grpc.Core;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.DependencyInjection;
using StartGrpc;
namespace netsd.Handlers;
public class Init
@ -8,11 +15,29 @@ public class Init
{
_socketBuilder.WebHost.ConfigureKestrel(options =>
{
options.ListenUnixSocket("/tmp/netsd.sock");
options.ListenUnixSocket("/tmp/netsd.sock", listenOptions =>
{
listenOptions.Protocols = HttpProtocols.Http2;
});
});
_socketBuilder.Services.AddGrpc();
var app = _socketBuilder.Build();
app.MapGrpcService<StartGrpc>();
app.Run();
}
}
public class StartGrpc : StartRpc.StartRpcBase
{
public override Task<StartReply> StartService(StartRequest request, ServerCallContext context)
{
return Task.FromResult(new StartReply
{
Success = true,
});
}
}

17
netsd/Handlers/Start.cs Normal file
View file

@ -0,0 +1,17 @@
using netsd.Utils;
using StartGrpc;
namespace netsd.Handlers;
public class Start
{
public Start()
{
using var channel = UDSConnector.CreateChannel();
var client = new StartRpc.StartRpcClient(channel);
var reply = client.StartService(new StartRequest{ Name = "Awoo" });
Console.WriteLine(reply.Success);
}
}

View file

@ -2,13 +2,17 @@ using System.CommandLine;
using netsd.Handlers;
var initCommand = new Command("init", "Run and bring up all default services");
var startCommand = new Command("start", "Starts up a netsd service");
var rootCommand = new RootCommand("Linux service manager written in C#");
rootCommand.AddCommand(initCommand);
rootCommand.AddCommand(startCommand);
initCommand.SetHandler((() =>
{
new Init();
}));
startCommand.SetHandler((() => new Start()));
return await rootCommand.InvokeAsync(args);

17
netsd/Protos/Start.proto Normal file
View file

@ -0,0 +1,17 @@
syntax = "proto3";
option csharp_namespace = "StartGrpc";
package netsd;
service StartRpc {
rpc StartService (StartRequest) returns (StartReply);
}
message StartRequest {
string name = 1;
}
message StartReply {
bool Success = 1;
}

View file

@ -0,0 +1,47 @@
using System.Net;
using System.Net.Sockets;
using Grpc.Net.Client;
namespace netsd.Utils;
public class UDSConnector
{
private readonly EndPoint _endPoint;
private static readonly string _socketPath = Path.Combine(Path.GetTempPath(), "netsd.sock");
private UDSConnector(EndPoint endPoint)
{
_endPoint = endPoint;
}
private async ValueTask<Stream> _connectAsync(SocketsHttpConnectionContext _, CancellationToken cancellationToken = default)
{
var socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
try
{
await socket.ConnectAsync(_endPoint, cancellationToken).ConfigureAwait(false);
return new NetworkStream(socket, true);
}
catch
{
socket.Dispose();
throw;
}
}
public static GrpcChannel CreateChannel()
{
var udsEndPoint = new UnixDomainSocketEndPoint(_socketPath);
var connectionFactory = new UDSConnector(udsEndPoint);
var socketsHttpHandler = new SocketsHttpHandler
{
ConnectCallback = connectionFactory._connectAsync
};
return GrpcChannel.ForAddress("http://localhost", new GrpcChannelOptions
{
HttpHandler = socketsHttpHandler
});
}
}

View file

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
@ -26,12 +26,20 @@
<UseNativeHttpHandler>true</UseNativeHttpHandler>
<UseSystemResourceKeys>true</UseSystemResourceKeys>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" />
<PackageReference Include="Grpc.AspNetCore" Version="2.50.0-pre1" />
<PackageReference Include="runtime.linux-musl-x64.Microsoft.DotNet.ILCompiler" Version="7.0.0" />
<PackageReference Include="runtime.linux-x64.Microsoft.DotNet.ILCompiler" Version="7.0.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
</ItemGroup>
<ItemGroup>
<Protobuf Include="Protos\Start.proto" />
</ItemGroup>
</Project>