|
| 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 Google.Protobuf; |
| 20 | +using Grpc.Net.Client; |
| 21 | +using Upload; |
| 22 | + |
| 23 | +namespace Client |
| 24 | +{ |
| 25 | + public class Program |
| 26 | + { |
| 27 | + private const int ChunkSize = 1024 * 32; // 32 KB |
| 28 | + |
| 29 | + static async Task Main(string[] args) |
| 30 | + { |
| 31 | + using var channel = GrpcChannel.ForAddress("https://localhost:5001"); |
| 32 | + var client = new Uploader.UploaderClient(channel); |
| 33 | + |
| 34 | + Console.WriteLine("Starting call"); |
| 35 | + var call = client.UploadFile(); |
| 36 | + |
| 37 | + Console.WriteLine("Sending file metadata"); |
| 38 | + await call.RequestStream.WriteAsync(new UploadFileRequest |
| 39 | + { |
| 40 | + Metadata = new FileMetadata |
| 41 | + { |
| 42 | + FileName = "pancakes.jpg" |
| 43 | + } |
| 44 | + }); |
| 45 | + |
| 46 | + var buffer = new byte[ChunkSize]; |
| 47 | + await using var readStream = File.OpenRead("pancakes.jpg"); |
| 48 | + |
| 49 | + while (true) |
| 50 | + { |
| 51 | + var count = await readStream.ReadAsync(buffer); |
| 52 | + if (count == 0) |
| 53 | + { |
| 54 | + break; |
| 55 | + } |
| 56 | + |
| 57 | + Console.WriteLine("Sending file data chunk of length " + count); |
| 58 | + await call.RequestStream.WriteAsync(new UploadFileRequest |
| 59 | + { |
| 60 | + Data = UnsafeByteOperations.UnsafeWrap(buffer.AsMemory(0, count)) |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + Console.WriteLine("Complete request"); |
| 65 | + await call.RequestStream.CompleteAsync(); |
| 66 | + |
| 67 | + var response = await call; |
| 68 | + Console.WriteLine("Upload id: " + response.Id); |
| 69 | + |
| 70 | + Console.WriteLine("Shutting down"); |
| 71 | + Console.WriteLine("Press any key to exit..."); |
| 72 | + Console.ReadKey(); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments