public async Task GetAllBytes(ChunReq chunReq) { Log("***GetBytes chunks started at:" + DateTime.Now); long start = DateTime.Now.Ticks; try { Stopwatch sw = new Stopwatch(); sw.Start(); using (var call = client.GetAllBytes(chunReq)) { var responseStream = call.ResponseStream; FullFile fullFile = null; while (await responseStream.MoveNext()) { fullFile = responseStream.Current; } // string responseString = Encoding.UTF8.GetString(responseBytes.ToArray()); sw.Stop(); long end = DateTime.Now.Ticks; long diff = end - start; // File.WriteAllBytes("fullFileAllBytes.jpg", fullFile.AllBytes.ToByteArray()); Log("***GetAllBytes response finished at:" + DateTime.Now + "took: " + sw.Elapsed); } } catch (RpcException e) { Log("RPC failed " + e); throw; } }
public override async Task GetAllBytes(ChunReq request, IServerStreamWriter <FullFile> responseStream, ServerCallContext context) { FullFile fullFile = new FullFile() { AllBytes = ByteString.CopyFrom(testBytes) }; await responseStream.WriteAsync(fullFile); }
public async Task GetBytes(ChunReq chunReq) { Log("***GetBytes chunks started at:" + DateTime.Now); long start = DateTime.Now.Ticks; Stopwatch sw = new Stopwatch(); sw.Start(); int chunkCounter = 0; List <byte> responseBytes = new List <byte>(); try { using (var call = client.GetBytes(chunReq)) { var responseStream = call.ResponseStream; while (await responseStream.MoveNext()) { Log("***Received bytes.. chunk counter " + chunkCounter); Chunk chunk = responseStream.Current; if (chunk.FirstChunk) { responseBytes = chunk.ChunkBytes.ToByteArray().ToList(); } else { List <byte> chunkBytes = chunk.ChunkBytes.ToByteArray().ToList(); responseBytes.AddRange(chunkBytes); } chunkCounter++; } sw.Stop(); // string responseString = Encoding.UTF8.GetString(responseBytes.ToArray()); long end = DateTime.Now.Ticks; long diff = end - start; Log("***GetBytes chunks finished at:" + DateTime.Now + "took: " + sw.Elapsed); try { Log("***writing file***"); Log("***writing file responseBytes lenght***" + responseBytes.Count()); byte[] resByteArray = responseBytes.ToArray(); Log("***writing file resByteArray lenght***" + resByteArray.Length); // File.WriteAllBytes("resFileChunk.jpg", resByteArray); } catch (Exception ex) { Log("***writing file ex ***" + ex.Message); } } } catch (RpcException e) { Log("RPC failed " + e); throw; } }
static void Main(string[] args) { int MaxMessageLengthBytes = Int32.MaxValue; ChannelOption maxSendMessageLength = new ChannelOption(ChannelOptions.MaxMessageLength, MaxMessageLengthBytes); ChannelOption[] grpcChannelOptions = { maxSendMessageLength }; var channel = new Channel("127.0.0.1:50052", ChannelCredentials.Insecure, grpcChannelOptions); //var channel = new Channel("127.0.0.1:50052", ChannelCredentials.Insecure); var client = new RouteGuideClient(new RouteGuide.RouteGuideClient(channel)); // Looking for a valid feature // client.GetFeature(409146138, -746188906); // Feature missing. //client.GetFeature(0, 0); // Looking for features between 40, -75 and 42, -73. //client.ListFeatures(400000000, -750000000, 420000000, -730000000).Wait(); // Record a few randomly selected points from the features file. // client.RecordRoute(RouteGuideUtil.ParseFeatures(RouteGuideUtil.DefaultFeaturesFile), 10).Wait(); // Send and receive some notes. //client.RouteChat().Wait(); ChunReq chunReq = new ChunReq() { ChunkCounter = 0 }; client.GetBytes(chunReq).Wait(); try { client.GetAllBytes(chunReq).Wait(); } catch (Exception ex) { } channel.ShutdownAsync().Wait(); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); }
public override async Task GetBytes(ChunReq request, IServerStreamWriter <Chunk> responseStream, ServerCallContext context) { int numberOfChunks = (int)Math.Ceiling((decimal)testBytes.Length / chunkSize); Console.WriteLine($"testBytes length:{testBytes.Length} number of chunks:{numberOfChunks}"); try { if (testBytes.Length <= chunkSize) { Chunk chunk = new Chunk() { FirstChunk = true, ChunkBytes = ByteString.CopyFrom(testBytes.Slice(0, testString.Length)), LastChunk = false }; await responseStream.WriteAsync(chunk); } for (int i = 0; i < numberOfChunks; i++) { int startIndex = i * chunkSize; int endIndex = startIndex + chunkSize; if (endIndex > testBytes.Length) { endIndex = testBytes.Length; } Console.WriteLine($"sending chunk start:{startIndex} end:{endIndex}"); Chunk chunk = new Chunk() { FirstChunk = (i == 0), ChunkBytes = ByteString.CopyFrom(testBytes.Slice(startIndex, endIndex)), LastChunk = false }; await responseStream.WriteAsync(chunk); } } catch (Exception ex) { //return Task.FromResult(true); } // return Task.FromResult(true); }