-
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
67 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
<Project Sdk="Microsoft.Build.Traversal/2.0.19"> | ||
<ItemGroup> | ||
<ProjectReference Include="src\**\*.csproj" Exclude="examples\**\*.csproj" /> | ||
<ProjectReference Include="src\**\*.csproj" Exclude="examples\**\*.csproj;docs\**\*.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup Condition="$(Packing) != 'true'"> | ||
<ProjectReference Include="**\*.csproj" Exclude="examples\**\*.csproj" /> | ||
<ProjectReference Include="**\*.csproj" Exclude="examples\**\*.csproj;docs\**\*.csproj" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<!-- this file exists only to make it easy to work with the folder in devenv --> | ||
<TargetFramework>$(DefaultTFM)</TargetFramework> | ||
</PropertyGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Streams | ||
|
||
gRPC has a `stream` concept that allows client-streaming, server-streaming, and full-duplex (independent bidirectional) streaming of messages. Inside | ||
protobuf-net.Grpc, this is typically exposed via the `IAsyncEnumerable<T>` API, which is an asynchronous sequence of messages of type `T`. For example, | ||
|
||
``` | ||
public async IAsyncEnumerable<SomeResponse> SomeDuplexMethod(IAsyncEnumerable<SomeRequest> requests) | ||
{ | ||
// very basic request/response server using streaming | ||
await foreach (var req in requests) | ||
{ | ||
yield return ApplySomeTransformation(req); | ||
} | ||
} | ||
``` | ||
|
||
This is *fine*, but .NET has another "stream", i.e. `System.IO.Stream` - a sequence of *bytes*. | ||
|
||
As of 1.2.2, protobuf-net.Grpc has limited (and growing) support for `Stream` as an exchange mechanism. Currently supported scenarios: | ||
|
||
- `Task<Stream> SomeMethod(/* optional single request message, optional context/cancellation */);` | ||
- `ValueTask<Stream> SomeMethod(/* optional single request message, optional context/cancellation */);` | ||
|
||
For example: | ||
|
||
``` c# | ||
public async Task<Stream> GetFileContents(SomeRequest request) | ||
{ | ||
var localPath = await CheckAccessAndMapToLocalPath(request.Path); | ||
|
||
return File.OpenRead(localPath); | ||
} | ||
``` | ||
|
||
This hands a `Stream` back to the library, with the library assuming control of how to transmit that, disposing the stream when done (as an implementation detail: it | ||
is sent as a `stream` of [`BytesValue`](https://github.com/protocolbuffers/protobuf/blob/main/src/google/protobuf/wrappers.proto) messages, | ||
with bespoke marshalling). As you would expect, the client can access this data trivially: | ||
|
||
``` c# | ||
await using var data = proxy.GetFileContents(request); | ||
await using var localFile = File.Create(localCachePath); | ||
await data.CopyToAsync(localFile); | ||
``` | ||
|
||
--- | ||
|
||
These are just trivial examples; more complex scenarios are possible, for example using `Pipe` on the server to allow the worker to provide | ||
data after the initial response (this will be more direct when the supported APIs are extended to include pipes directly). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters