add basic GRPC communication
This commit is contained in:
parent
6e664ed6df
commit
84ac974427
6 changed files with 121 additions and 3 deletions
|
@ -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;
|
namespace netsd.Handlers;
|
||||||
|
|
||||||
public class Init
|
public class Init
|
||||||
|
@ -8,11 +15,29 @@ public class Init
|
||||||
{
|
{
|
||||||
_socketBuilder.WebHost.ConfigureKestrel(options =>
|
_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();
|
var app = _socketBuilder.Build();
|
||||||
|
|
||||||
|
app.MapGrpcService<StartGrpc>();
|
||||||
|
|
||||||
app.Run();
|
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
17
netsd/Handlers/Start.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,13 +2,17 @@ using System.CommandLine;
|
||||||
using netsd.Handlers;
|
using netsd.Handlers;
|
||||||
|
|
||||||
var initCommand = new Command("init", "Run and bring up all default services");
|
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#");
|
var rootCommand = new RootCommand("Linux service manager written in C#");
|
||||||
rootCommand.AddCommand(initCommand);
|
rootCommand.AddCommand(initCommand);
|
||||||
|
rootCommand.AddCommand(startCommand);
|
||||||
|
|
||||||
initCommand.SetHandler((() =>
|
initCommand.SetHandler((() =>
|
||||||
{
|
{
|
||||||
new Init();
|
new Init();
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
startCommand.SetHandler((() => new Start()));
|
||||||
|
|
||||||
return await rootCommand.InvokeAsync(args);
|
return await rootCommand.InvokeAsync(args);
|
||||||
|
|
17
netsd/Protos/Start.proto
Normal file
17
netsd/Protos/Start.proto
Normal 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;
|
||||||
|
}
|
47
netsd/Utils/UDSConnector.cs
Normal file
47
netsd/Utils/UDSConnector.cs
Normal 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
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
|
@ -28,10 +28,18 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" />
|
<FrameworkReference Include="Microsoft.AspNetCore.App" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<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-musl-x64.Microsoft.DotNet.ILCompiler" Version="7.0.0" />
|
||||||
<PackageReference Include="runtime.linux-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" />
|
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Protobuf Include="Protos\Start.proto" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
Reference in a new issue