Exemple #1
0
        private HelloRequest BuildHelloRequest(string sender, int count)
        {
            //build request
            var request = new HelloRequest {
                Name = sender + "-" + count, Surname = "abcdefg", Address = "aaaa"
            };

            //request to array
            var req            = request.ToByteArray();
            var parsed_request = HelloRequest.Parser.ParseFrom(req);

            Console.WriteLine($"Sending[{count}]: Size={request.CalculateSize()}, StreamSize={req.Length}, ToString={request}, Parsed={parsed_request}");

            ////request to array
            //var req_output_stream = new Google.Protobuf.CodedOutputStream(data);
            //request.WriteTo(req_output_stream);
            ////parse array to request
            //len = req_output_stream.Position;
            //var req = new byte[len];
            //Array.Copy(data, req, len);
            //var parsed_request = HelloRequest.Parser.ParseFrom(req);
            //Console.WriteLine($"Sending[{count}]: Size={request.CalculateSize()}, StreamSize={req_output_stream.Position}/{req_output_stream.SpaceLeft}, ToString={request}, Parsed={parsed_request}");

            return(request);
        }
Exemple #2
0
        public void SendSayHelloRequest(string host, int port, string sender, int count)
        {
            try
            {
                var descriptor = Greeter.Descriptor;
                Console.WriteLine(descriptor);

                //build channel
                var channel = new Channel(host, port, ChannelCredentials.Insecure);

                //build client
                var client = new Greeter.GreeterClient(channel);

                //build request
                var request = new HelloRequest {
                    Name = sender + "-" + count, Surname = "abcdefg", Address = "aaaa"
                };
                //request to array
                var req_output_stream = new Google.Protobuf.CodedOutputStream(data);
                request.WriteTo(req_output_stream);
                //parse array to request
                len = req_output_stream.Position;
                var req = new byte[len];
                Array.Copy(data, req, len);
                var parsed_request = HelloRequest.Parser.ParseFrom(req);

                Console.WriteLine($"Sending[{count}]: Size={request.CalculateSize()}, StreamSize={req_output_stream.Position}/{req_output_stream.SpaceLeft}, ToString={request}, Parsed={parsed_request}");

                //send request
                var reply = client.SayHello(request);


                //response to array
                var rep_output_stream = new Google.Protobuf.CodedOutputStream(data);
                reply.WriteTo(rep_output_stream);
                //parse array to response
                len = rep_output_stream.Position;
                var rep = new byte[len];
                Array.Copy(data, rep, len);
                var parsed_reply = GetTestReplyFromArray(rep);

                Console.WriteLine($"Reply[{count}]: Size={reply.CalculateSize()}, StreamSize={rep_output_stream.Position}/{rep_output_stream.SpaceLeft}, ToString={reply}, Parsed={parsed_reply}");
            }
            catch (Exception ex)
            {
                Console.WriteLine("XXX | sending error: " + ex);
            }
        }
        public async Task CallAsync()
        {
            var message = new HelloRequest
            {
                Name = "World"
            };

            var messageSize  = message.CalculateSize();
            var messageBytes = new byte[messageSize];

            message.WriteTo(new CodedOutputStream(messageBytes));

            var data = new byte[messageSize + 5];

            data[0] = 0;
            BinaryPrimitives.WriteUInt32BigEndian(data.AsSpan(1, 4), (uint)messageSize);
            messageBytes.CopyTo(data.AsSpan(5));

            var url = _useClientCertificate ? "https://" : "http://";

            url += Target;

            var request = new HttpRequestMessage(HttpMethod.Post, url + "/Greet.GreetService/SayHello");

            request.Version = new Version(2, 0);
            request.Content = new StreamContent(new MemoryStream(data));
            request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/grpc");
            if (_deadline != null)
            {
                request.Headers.Add("grpc-timeout", "1S");
            }

            var response = await _client !.SendAsync(request);

            response.EnsureSuccessStatusCode();

            await response.Content.ReadAsByteArrayAsync();

            var grpcStatus = response.TrailingHeaders.GetValues("grpc-status").SingleOrDefault();

            if (grpcStatus != "0")
            {
                throw new InvalidOperationException($"Unexpected grpc-status: {grpcStatus}");
            }
        }
Exemple #4
0
        private static async Task <HelloReply> MakeRawGrpcCall(HelloRequest request, HttpClient client)
        {
            var messageSize  = request.CalculateSize();
            var messageBytes = new byte[messageSize];

            request.WriteTo(new CodedOutputStream(messageBytes));

            var data = new byte[messageSize + 5];

            data[0] = 0;
            BinaryPrimitives.WriteUInt32BigEndian(data.AsSpan(1, 4), (uint)messageSize);
            messageBytes.CopyTo(data.AsSpan(5));

            using var httpRequest = new HttpRequestMessage(HttpMethod.Post, "/greet.Greeter/SayHello");
            httpRequest.Version   = new Version(2, 0);
            httpRequest.Content   = new StreamContent(new MemoryStream(data));
            httpRequest.Content.Headers.ContentType = new MediaTypeHeaderValue("application/grpc");
            httpRequest.Headers.TE.Add(new TransferCodingWithQualityHeaderValue("trailers"));

            var response = await client.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead);

            response.EnsureSuccessStatusCode();

            data = await response.Content.ReadAsByteArrayAsync();

            var responseMessage = HelloReply.Parser.ParseFrom(data.AsSpan(5, data.Length - 5).ToArray());

            var grpcStatus = response.TrailingHeaders.GetValues("grpc-status").SingleOrDefault();

            if (grpcStatus != "0")
            {
                throw new InvalidOperationException($"Unexpected grpc-status: {grpcStatus}");
            }

            return(responseMessage);
        }
Exemple #5
0
        private static async Task <HelloReply> MakeRawGrpcCall(HelloRequest request, HttpMessageInvoker client, bool streamRequest, bool streamResponse)
        {
            using var httpRequest = new HttpRequestMessage(HttpMethod.Post, RawGrpcUri);
            httpRequest.Version   = HttpVersion.Version20;

            if (!streamRequest)
            {
                var messageSize = request.CalculateSize();
                var data        = new byte[messageSize + HeaderSize];
                request.WriteTo(new CodedOutputStream(data));

                Array.Copy(data, 0, data, HeaderSize, messageSize);
                data[0] = 0;
                BinaryPrimitives.WriteUInt32BigEndian(data.AsSpan(1, 4), (uint)messageSize);

                httpRequest.Content = new ByteArrayContent(data);
                httpRequest.Content.Headers.TryAddWithoutValidation("Content-Type", "application/grpc");
            }
            else
            {
                httpRequest.Content = new PushUnaryContent <HelloRequest>(request);
            }

            httpRequest.Headers.TryAddWithoutValidation("TE", "trailers");

            using var response = await client.SendAsync(httpRequest, Cts.Token);

            response.EnsureSuccessStatusCode();

            HelloReply responseMessage;

            if (!streamResponse)
            {
                var data = await response.Content.ReadAsByteArrayAsync();

                responseMessage = HelloReply.Parser.ParseFrom(data.AsSpan(5).ToArray());
            }
            else
            {
                var responseStream = await response.Content.ReadAsStreamAsync();

                var data = new byte[HeaderSize];

                int read;
                var received = 0;
                while ((read = await responseStream.ReadAsync(data.AsMemory(received, HeaderSize - received), Cts.Token).ConfigureAwait(false)) > 0)
                {
                    received += read;

                    if (received == HeaderSize)
                    {
                        break;
                    }
                }

                if (received < HeaderSize)
                {
                    throw new InvalidDataException("Unexpected end of content while reading the message header.");
                }

                var length = (int)BinaryPrimitives.ReadUInt32BigEndian(data.AsSpan(1, 4));

                if (data.Length < length)
                {
                    data = new byte[length];
                }

                received = 0;
                while ((read = await responseStream.ReadAsync(data.AsMemory(received, length - received), Cts.Token).ConfigureAwait(false)) > 0)
                {
                    received += read;

                    if (received == length)
                    {
                        break;
                    }
                }

                read = await responseStream.ReadAsync(data, Cts.Token);

                if (read > 0)
                {
                    throw new InvalidDataException("Extra data returned.");
                }

                responseMessage = HelloReply.Parser.ParseFrom(data);
            }

            var grpcStatus = response.TrailingHeaders.GetValues("grpc-status").SingleOrDefault();

            if (grpcStatus != "0")
            {
                throw new InvalidOperationException($"Unexpected grpc-status: {grpcStatus}");
            }

            return(responseMessage);
        }