Ejemplo n.º 1
0
    /// <summary>
    /// This method handles the task of calling the remote gRPC Service ListFeatures by passing a Message Type of
    /// Rectangle which contains (2) Points. The result is a gRPC response STREAM of Feature Message Types.
    /// </summary>
    /// <param name="areaOfInterest">A Routeguide Rectangle containing two Points</param>
    /// <returns></returns>
    public async Task ListFeatures(Routeguide.Rectangle areaOfInterest)
    {
        Debug.Log("ListFeatures Client Lo latitude: " + areaOfInterest.Lo.Latitude +
                  ",  Lo longitude: " + areaOfInterest.Lo.Longitude + "\n" +
                  ",  Hi latitude: " + areaOfInterest.Hi.Latitude +
                  ",  Hi longitude: " + areaOfInterest.Hi.Longitude);

        StringBuilder responseText = new StringBuilder();

        try
        {
            var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
            //Sending and Receiving will be sequential - send first, then receive stream response second
            var response = _client.ListFeatures(areaOfInterest, cancellationToken: cts.Token);
            while (await response.ResponseStream.MoveNext())
            {
                var thisItemName = response.ResponseStream.Current.Name;
                if (!String.IsNullOrEmpty(thisItemName))
                {
                    _myRouteGuideUiHandler.AddTextToUi(thisItemName, false);
                }
            }
        }
        catch (RpcException e)
        {
            _myRouteGuideUiHandler.AddTextToUi("ListFeatures Service is unavailable. " + e.Message, false);
        }

#if DEBUG
        Debug.Log("async Task ListFeatures Finished");
#endif
    }
Ejemplo n.º 2
0
            /// <summary>
            /// Server-streaming example. Calls listFeatures with a rectangle of interest. Prints each response feature as it arrives.
            /// </summary>
            public async Task ListFeatures(int lowLat, int lowLon, int hiLat, int hiLon)
            {
                try
                {
                    Log("***Server-streaming example. Calls listFeatures with a rectangle of interest. Prints each response feature as it arrives.   ListFeatures: lowLat={0} lowLon={1} hiLat={2} hiLon={3}", lowLat, lowLon, hiLat,
                        hiLon);
                    Log("*** ListFeatures: lowLat={0} lowLon={1} hiLat={2} hiLon={3}", lowLat, lowLon, hiLat,
                        hiLon);

                    Rectangle request = new Rectangle
                    {
                        Lo = new Point {
                            Latitude = lowLat, Longitude = lowLon
                        },
                        Hi = new Point {
                            Latitude = hiLat, Longitude = hiLon
                        }
                    };

                    using (var call = client.ListFeatures(request))
                    {
                        var           responseStream = call.ResponseStream;
                        StringBuilder responseLog    = new StringBuilder("Result: ");

                        while (await responseStream.MoveNext())
                        {
                            Feature feature = responseStream.Current;
                            responseLog.Append(feature.ToString());
                        }
                        Log(responseLog.ToString());
                    }
                }
                catch (RpcException e)
                {
                    Log("RPC failed " + e);
                    throw;
                }
            }
Ejemplo n.º 3
0
        private static async Task TestListFeaturesAsync(Channel channel, int deltaLatitude = 5, int deltaLongitude = 5, CancellationToken cancellationToken = default)
        {
            var       client    = new RouteGuide.RouteGuideClient(channel);
            const int longitude = 34;
            const int latitude  = 1;

            using (var request = client.ListFeatures(new Rectangle()
            {
                Lo = new Point()
                {
                    Latitude = latitude, Longitude = longitude
                }
                , Hi = new Point()
                {
                    Latitude = latitude + deltaLatitude, Longitude = longitude + deltaLongitude
                }
            }, cancellationToken: cancellationToken))
            {
                try
                {
                    bool hasNext = await request.ResponseStream.MoveNext(cancellationToken).ConfigureAwait(false);

                    while (hasNext)
                    {
                        //Console.WriteLine($"status: {request.GetStatus()}"); status can only be accessed once the call has finished
                        Console.WriteLine(request.ResponseStream.Current.Name);
                        hasNext = await request.ResponseStream.MoveNext(cancellationToken).ConfigureAwait(false);
                    }
                }
                catch (RpcException e)
                {
                    Console.WriteLine($"request status: {request.GetStatus()}");
                    Console.WriteLine($"ex status: {e.Status}");
                }
            }
        }