Skip to content

Commit a9827e5

Browse files
authored
Add transporter example (#1025)
1 parent 55cde44 commit a9827e5

File tree

17 files changed

+505
-37
lines changed

17 files changed

+505
-37
lines changed

examples/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,20 @@ This example uses [tye](https://github.com/dotnet/tye) to run the solution and f
228228
* Client factory
229229
* Mixing frontend RESTful API with backend gRPC services
230230
* App development with [Tye](https://github.com/dotnet/tye)
231+
232+
## [Transporter](./Transporter)
233+
234+
**.NET 5 or later required**
235+
236+
The transporter example shows how to use gRPC over non-TCP transports. This example uses a [Unix domain socket (UDS)](https://en.wikipedia.org/wiki/Unix_domain_socket) to send gRPC messages between the client and server.
237+
238+
To use gRPC with UDS:
239+
240+
1. The client creates a channel with a custom SocketsConnectionFactory. The factory connects to a specified UDS endpoint.
241+
2. The server configures a UDS endpoint with [KestrelServerOptions.ListenUnixSocket](https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.server.kestrel.core.kestrelserveroptions.listenunixsocket) in *Program.cs*.
242+
243+
##### Scenarios:
244+
245+
* Unix domain sockets
246+
* SocketsConnectionFactory
247+
* [KestrelServerOptions.ListenUnixSocket](https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.server.kestrel.core.kestrelserveroptions.listenunixsocket)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Protobuf Include="..\Proto\greet.proto" GrpcServices="Client" Link="Protos\greet.proto" />
10+
11+
<PackageReference Include="Google.Protobuf" Version="$(GoogleProtobufPackageVersion)" />
12+
<PackageReference Include="Grpc.Net.Client" Version="$(GrpcDotNetPackageVersion)" />
13+
<PackageReference Include="Grpc.Tools" Version="$(GrpcPackageVersion)" PrivateAssets="All" />
14+
</ItemGroup>
15+
16+
</Project>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#region Copyright notice and license
2+
3+
// Copyright 2019 The gRPC Authors
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
#endregion
18+
19+
using System;
20+
using System.IO;
21+
using System.Net.Http;
22+
using System.Net.Sockets;
23+
using System.Threading;
24+
using System.Threading.Tasks;
25+
using Greet;
26+
using Grpc.Core;
27+
using Grpc.Net.Client;
28+
29+
namespace Client
30+
{
31+
public partial class Program
32+
{
33+
public static readonly string SocketPath = Path.Combine(Path.GetTempPath(), "grpc-transporter.tmp");
34+
35+
static async Task Main(string[] args)
36+
{
37+
using var channel = GrpcChannel.ForAddress("http://localhost", new GrpcChannelOptions
38+
{
39+
HttpHandler = CreateHttpHandler(SocketPath)
40+
});
41+
var client = new Greeter.GreeterClient(channel);
42+
43+
var reply = await client.SayHelloAsync(new HelloRequest { Name = "GreeterClient" });
44+
Console.WriteLine("Greeting: " + reply.Message);
45+
46+
Console.WriteLine("Shutting down");
47+
Console.WriteLine("Press any key to exit...");
48+
Console.ReadKey();
49+
}
50+
51+
private static SocketsHttpHandler CreateHttpHandler(string socketPath)
52+
{
53+
var udsEndPoint = new UnixDomainSocketEndPoint(socketPath);
54+
var connectionFactory = new UnixDomainSocketConnectionFactory(udsEndPoint);
55+
var socketsHttpHandler = new SocketsHttpHandler
56+
{
57+
ConnectionFactory = connectionFactory
58+
};
59+
60+
return socketsHttpHandler;
61+
}
62+
}
63+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#region Copyright notice and license
2+
3+
// Copyright 2019 The gRPC Authors
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
#endregion
18+
19+
using System.Net;
20+
using System.Net.Connections;
21+
using System.Net.Sockets;
22+
using System.Threading;
23+
using System.Threading.Tasks;
24+
25+
namespace Client
26+
{
27+
public class UnixDomainSocketConnectionFactory : SocketsConnectionFactory
28+
{
29+
private readonly EndPoint _endPoint;
30+
31+
public UnixDomainSocketConnectionFactory(EndPoint endPoint) : base(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified)
32+
{
33+
_endPoint = endPoint;
34+
}
35+
36+
public override ValueTask<Connection> ConnectAsync(EndPoint? endPoint, IConnectionProperties? options = null, CancellationToken cancellationToken = default)
37+
{
38+
return base.ConnectAsync(_endPoint, options, cancellationToken);
39+
}
40+
}
41+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2019 The gRPC Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
syntax = "proto3";
16+
17+
package greet;
18+
19+
// The greeting service definition.
20+
service Greeter {
21+
// Sends a greeting
22+
rpc SayHello (HelloRequest) returns (HelloReply);
23+
}
24+
25+
// The request message containing the user's name.
26+
message HelloRequest {
27+
string name = 1;
28+
}
29+
30+
// The response message containing the greetings
31+
message HelloReply {
32+
string message = 1;
33+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#region Copyright notice and license
2+
3+
// Copyright 2019 The gRPC Authors
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
#endregion
18+
19+
using System.IO;
20+
using Microsoft.AspNetCore.Hosting;
21+
using Microsoft.Extensions.Hosting;
22+
23+
namespace Server
24+
{
25+
public class Program
26+
{
27+
public static readonly string SocketPath = Path.Combine(Path.GetTempPath(), "grpc-transporter.tmp");
28+
29+
public static void Main(string[] args)
30+
{
31+
CreateHostBuilder(args).Build().Run();
32+
}
33+
34+
public static IHostBuilder CreateHostBuilder(string[] args) =>
35+
Host.CreateDefaultBuilder(args)
36+
.ConfigureWebHostDefaults(webBuilder =>
37+
{
38+
webBuilder.UseStartup<Startup>();
39+
webBuilder.ConfigureKestrel(options =>
40+
{
41+
if (File.Exists(SocketPath))
42+
{
43+
File.Delete(SocketPath);
44+
}
45+
46+
options.ListenUnixSocket(SocketPath);
47+
});
48+
});
49+
}
50+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"profiles": {
3+
"Server": {
4+
"commandName": "Project",
5+
"launchBrowser": false,
6+
"applicationUrl": "https://localhost:5001",
7+
"environmentVariables": {
8+
"ASPNETCORE_ENVIRONMENT": "Development"
9+
}
10+
}
11+
}
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Protobuf Include="..\Proto\greet.proto" GrpcServices="Server" Link="Protos\greet.proto" />
9+
10+
<PackageReference Include="Grpc.AspNetCore" Version="$(GrpcDotNetPackageVersion)" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#region Copyright notice and license
2+
3+
// Copyright 2019 The gRPC Authors
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
#endregion
18+
19+
using System.Threading.Tasks;
20+
using Greet;
21+
using Grpc.Core;
22+
using Microsoft.Extensions.Logging;
23+
24+
namespace Server
25+
{
26+
public class GreeterService : Greeter.GreeterBase
27+
{
28+
private readonly ILogger _logger;
29+
30+
public GreeterService(ILoggerFactory loggerFactory)
31+
{
32+
_logger = loggerFactory.CreateLogger<GreeterService>();
33+
}
34+
35+
public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
36+
{
37+
_logger.LogInformation($"Sending hello to {request.Name}");
38+
return Task.FromResult(new HelloReply { Message = "Hello " + request.Name });
39+
}
40+
}
41+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#region Copyright notice and license
2+
3+
// Copyright 2019 The gRPC Authors
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
#endregion
18+
19+
using Microsoft.AspNetCore.Builder;
20+
using Microsoft.AspNetCore.Hosting;
21+
using Microsoft.Extensions.DependencyInjection;
22+
using Microsoft.Extensions.Hosting;
23+
24+
namespace Server
25+
{
26+
public class Startup
27+
{
28+
public void ConfigureServices(IServiceCollection services)
29+
{
30+
services.AddGrpc();
31+
}
32+
33+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
34+
{
35+
if (env.IsDevelopment())
36+
{
37+
app.UseDeveloperExceptionPage();
38+
}
39+
40+
app.UseRouting();
41+
42+
app.UseEndpoints(endpoints =>
43+
{
44+
endpoints.MapGrpcService<GreeterService>();
45+
});
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)