Skip to content

Commit 11a7146

Browse files
authored
Update to RC2 and re-add transporter example (#1056)
1 parent 637afdb commit 11a7146

File tree

23 files changed

+418
-25
lines changed

23 files changed

+418
-25
lines changed

Diff for: build/dependencies.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<BenchmarkDotNetPackageVersion>0.12.1</BenchmarkDotNetPackageVersion>
44
<GoogleProtobufPackageVersion>3.13.0</GoogleProtobufPackageVersion>
5-
<GrpcDotNetPackageVersion>2.30.0</GrpcDotNetPackageVersion> <!-- Used by example projects -->
5+
<GrpcDotNetPackageVersion>2.32.0-pre1</GrpcDotNetPackageVersion> <!-- Used by example projects -->
66
<GrpcPackageVersion>2.32.0-pre1</GrpcPackageVersion>
77
<MicrosoftAspNetCoreAppPackageVersion>3.1.3</MicrosoftAspNetCoreAppPackageVersion>
88
<MicrosoftAspNetCoreBlazorPackageVersion>5.0.0-rc.1.20431.20</MicrosoftAspNetCoreBlazorPackageVersion>

Diff for: examples/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,20 @@ To use gRPC with UDS:
245245
* Unix domain sockets
246246
* SocketsConnectionFactory
247247
* [KestrelServerOptions.ListenUnixSocket](https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.server.kestrel.core.kestrelserveroptions.listenunixsocket)
248+
249+
## [Transporter](./Transporter)
250+
251+
**.NET 5 or later required**
252+
253+
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.
254+
255+
To use gRPC with UDS:
256+
257+
1. The client creates a channel with a `ConnectCallback`. The callback connects to a specified UDS endpoint.
258+
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*.
259+
260+
##### Scenarios:
261+
262+
* Unix domain sockets
263+
* SocketsHttpHandler.ConnectCallback
264+
* [KestrelServerOptions.ListenUnixSocket](https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.server.kestrel.core.kestrelserveroptions.listenunixsocket)

Diff for: examples/Transporter/Client/Client.csproj

+16
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>

Diff for: examples/Transporter/Client/Program.cs

+63
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+
ConnectCallback = connectionFactory.ConnectAsync
58+
};
59+
60+
return socketsHttpHandler;
61+
}
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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;
22+
using System.Net.Http;
23+
using System.Net.Sockets;
24+
using System.Threading;
25+
using System.Threading.Tasks;
26+
27+
namespace Client
28+
{
29+
public class UnixDomainSocketConnectionFactory
30+
{
31+
private readonly EndPoint _endPoint;
32+
33+
public UnixDomainSocketConnectionFactory(EndPoint endPoint)
34+
{
35+
_endPoint = endPoint;
36+
}
37+
38+
public async ValueTask<Stream> ConnectAsync(SocketsHttpConnectionContext _, CancellationToken cancellationToken = default)
39+
{
40+
var socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
41+
42+
try
43+
{
44+
await socket.ConnectAsync(_endPoint, cancellationToken).ConfigureAwait(false);
45+
return new NetworkStream(socket, true);
46+
}
47+
catch
48+
{
49+
socket.Dispose();
50+
throw;
51+
}
52+
}
53+
}
54+
}

Diff for: examples/Transporter/Proto/greet.proto

+33
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+
}

Diff for: examples/Transporter/Server/Program.cs

+50
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+
}

Diff for: examples/Transporter/Server/Server.csproj

+13
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>
+41
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+
}

Diff for: examples/Transporter/Server/Startup.cs

+48
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+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Debug",
5+
"System": "Information",
6+
"Grpc": "Information",
7+
"Microsoft": "Information"
8+
}
9+
}
10+
}

Diff for: examples/Transporter/Server/appsettings.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information"
5+
}
6+
},
7+
"AllowedHosts": "*",
8+
"Kestrel": {
9+
"EndpointDefaults": {
10+
"Protocols": "Http2"
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)