Exemplo n.º 1
0
        private async void gRPCUnaryTry()
        {
            AppendText("starting unary calling...");
            var channel = GrpcChannel.ForAddress(DefaultChannelAddress);
            var client  = new StreamShoper.StreamShoperClient(channel);

            var request = new ExampleRequest {
                PageIndex = 2, PageSize = 10, IsDescending = true
            };

            var response = await client.UnaryCallAsync(request);

            AppendText(response.Message);
        }
Exemplo n.º 2
0
        private async void gRPCBothStreamTry()
        {
            try
            {
                AppendText("starting Both Steam Calling...");
                var channel = GrpcChannel.ForAddress(DefaultChannelAddress);
                var client  = new StreamShoper.StreamShoperClient(channel);

                var cancelSource = new CancellationTokenSource();
                var token        = cancelSource.Token;

                var call = client.StreamingBothWays(null, null, token);

                for (int i = 8; i >= 0; i--)
                {
                    AppendText($"#From Client# the index is {i}");
                    var request = new ExampleRequest {
                        PageIndex = i, PageSize = 10, IsDescending = true
                    };
                    await call.RequestStream.WriteAsync(request);

                    await call.ResponseStream.MoveNext(token);

                    var response = call.ResponseStream.Current;
                    AppendText(response.Message);

                    AppendText("Waiting 2 seconds...");
                    await Task.Delay(2000);

                    //await Task.Delay(100);
                }
                AppendText("Starting cancelling...");
                cancelSource.Cancel();
            }
            catch (Exception ex)
            {
                AppendText("Cancelling...");
            }
        }
Exemplo n.º 3
0
        private async void gRPCServerStreamTry()
        {
            AppendText("starting server stream...");
            var channel = GrpcChannel.ForAddress(@"https://localhost:5001");
            var client  = new StreamShoper.StreamShoperClient(channel);

            var request = new ExampleRequest {
                PageIndex = 2, PageSize = 10, IsDescending = true
            };
            var cancelSource = new CancellationTokenSource();
            var token        = cancelSource.Token;

            using var call = client.StreamingFromServer(request, null, null, token);

            int maxIndex = 10;

            while (true)
            {
                try
                {
                    await call.ResponseStream.MoveNext(token);

                    var response = call.ResponseStream.Current;
                    AppendText(response.Message);
                    if (response.TotalCount >= 10)
                    {
                        AppendText("Meet max, cancelling...");
                        cancelSource.Cancel();
                    }
                }
                catch (Exception ex)
                {
                    AppendText("Exit...");
                    break;
                }
            }
        }
Exemplo n.º 4
0
        private async void gRPCBigDataBothStreamTry()
        {
            Stopwatch sw1 = new Stopwatch();
            Stopwatch sw2 = new Stopwatch();

            sw1.Start();

            try
            {
                AppendText("starting Both Steam Calling...");
                var channel = GrpcChannel.ForAddress(DefaultChannelAddress);
                var client  = new StreamShoper.StreamShoperClient(channel);

                var cancelSource = new CancellationTokenSource();
                var token        = cancelSource.Token;

                var call = client.SendingBigDataPackage(null, null, token);
                AppendText("Start to send data...");


                sw2.Start();

                AppendText($"Parameters: Send Times is {SendTimes}, Package size is {packageSize}M");

                for (int i = SendTimes; i >= 0; i--)
                {
                    AppendText($"#From Client# the index is {i}");
                    var request = new BigDataRequest {
                        DataType = $"index{i}"
                    };

                    var strTmp = new string('A', (int)(1024 * 1024 * packageSize));

                    var size = ((double)Encoding.UTF8.GetByteCount(strTmp) / (1024 * 1024)).ToString("f3");
                    AppendText($"Per item size is {size}M");

                    var times = 1; // total ~1M
                    while (times > 0)
                    {
                        request.Content.Add(strTmp);
                        times--;
                    }

                    await call.RequestStream.WriteAsync(request);

                    await call.ResponseStream.MoveNext(token);

                    var response = call.ResponseStream.Current;
                    AppendText(response.Message);

                    //AppendText("Waiting 2 seconds...");
                    //await Task.Delay(2000);

                    //await Task.Delay(100);
                }
                AppendText("Starting cancelling...");
                cancelSource.Cancel();
                AppendText("Cancelled");
                sw2.Stop();
            }
            catch (Exception ex)
            {
                AppendText(ex.Message);
                AppendText("Exception, Cancelling...");
            }

            sw1.Stop();

            AppendText($"Sending data time cost:{sw2.Elapsed.TotalSeconds}");
            AppendText($"Total time cost:{sw1.Elapsed.TotalSeconds}");
        }