Ejemplo n.º 1
0
            /// <summary>
            /// Bi-directional streaming example. Send some chat messages, and print any
            /// chat messages that are sent from the server.
            /// </summary>
            public async Task RouteChat()
            {
                try
                {
                    Log("*** RouteChat  Bi-directional streaming example. Send some chat messages, and print any chat messages that are sent from the server.");
                    Log("*** RouteChat");
                    var requests = new List <RouteNote>
                    {
                        NewNote("First message", 0, 0),
                        NewNote("Second message", 0, 1),
                        NewNote("Third message", 1, 0),
                        NewNote("Fourth message", 0, 0)
                    };

                    using (var call = client.RouteChat())
                    {
                        var responseReaderTask = Task.Run(async() =>
                        {
                            while (await call.ResponseStream.MoveNext())
                            {
                                var note = call.ResponseStream.Current;
                                Log("Got message \"{0}\" at {1}, {2}", note.Message,
                                    note.Location.Latitude, note.Location.Longitude);
                            }
                        });

                        foreach (RouteNote request in requests)
                        {
                            Log("Sending message \"{0}\" at {1}, {2}", request.Message,
                                request.Location.Latitude, request.Location.Longitude);

                            await call.RequestStream.WriteAsync(request);
                        }
                        await call.RequestStream.CompleteAsync();

                        await responseReaderTask;

                        Log("Finished RouteChat");
                    }
                }
                catch (RpcException e)
                {
                    Log("RPC failed", e);
                    throw;
                }
            }
Ejemplo n.º 2
0
    /// <summary>
    /// This method handles the task of calling the remote gRPC BI-Directional Service RouteChat by passing a STREAM of
    /// RouteNote Message Types. A response STREAM returns a series of accumulated RouteNote Message Types.
    /// </summary>
    /// <param name="notesOfInterest"></param>
    /// <returns></returns>
    public async Task RouteChat(Routeguide.RouteNote[] notesOfInterest)
    {
        try
        {
            var cts        = new CancellationTokenSource(TimeSpan.FromSeconds(6));
            var thisStream = _client.RouteChat(cancellationToken: cts.Token);
            //Using a Task.Run(async ()...here as we essentially want (2) things to run in parallel, and
            //only return when both are complete.
            var responseReaderTask = Task.Run(async() =>
            {
                while (await thisStream.ResponseStream.MoveNext())
                {
                    //This AddText.. method is different, its capable of getting the UI updated from a different thread.
                    _myRouteGuideUiHandler.AddTextToUi(thisStream.ResponseStream.Current.Message, true);
                }

#if DEBUG
                Debug.Log("RouteChat RECEIVE messages complete");
#endif
            });

            foreach (RouteNote request in notesOfInterest)
            {
                await thisStream.RequestStream.WriteAsync(request);
            }
#if DEBUG
            Debug.Log("RouteChat SEND messages complete");
#endif

            await thisStream.RequestStream.CompleteAsync();

            await responseReaderTask;
        }
        catch (RpcException e)
        {
            _myRouteGuideUiHandler.AddTextToUi("RouteChat Service is unavailable. " + e.Message, false);
        }

#if DEBUG
        Debug.Log("async Task RouteChat Finished");
#endif
    }
Ejemplo n.º 3
0
        public async Task RouteChat()
        {
            try
            {
                using (var call = client.RouteChat())
                {
                    var responseReaderTask = Task.Run(async() =>
                    {
                        while (await call.ResponseStream.MoveNext())
                        {
                            var note = call.ResponseStream.Current;
                            //Log("Got message {0}", note.Datasend.ToStringUtf8());
                            strRpcMessage = string.Format("{0}", note.Datasend.ToStringUtf8());
                        }
                    });

                    string strTemp = "";
                    //FileStream fs = new FileStream("CloudPoint.txt", FileMode.Open);
                    //StreamReader sr = new StreamReader(fs);
                    RouteNote noteSend = new RouteNote();

                    int total    = 0;
                    int iCpyCunt = 0;
                    int index    = 0;
                    int size     = 4096 * 10;

                    //byte[] buffer = new byte[1024];
                    //fs.Read(buffer, index, 4096);
                    byte[] buffer = System.IO.File.ReadAllBytes("CloudPoint.txt");
                    total = buffer.Length;

                    while (total > 0)
                    {
                        byte[] bufWrite;
                        if (total > size)
                        {
                            iCpyCunt = size;
                        }
                        else
                        {
                            iCpyCunt = total;
                        }

                        bufWrite = new byte[iCpyCunt];
                        Array.Copy(buffer, index, bufWrite, 0, iCpyCunt);
                        strTemp           = Encoding.UTF8.GetString(bufWrite);
                        noteSend.Datasend = ByteString.CopyFromUtf8(strTemp);
                        noteSend.Size     = strTemp.Length;
                        await call.RequestStream.WriteAsync(noteSend);

                        total -= iCpyCunt;
                        index += iCpyCunt;
                    }

                    //while ((strTemp = sr.ReadLine()) != null)
                    //{
                    //    noteSend.Datasend = ByteString.CopyFromUtf8(strTemp);
                    //    noteSend.Size = strTemp.Length;
                    //    await call.RequestStream.WriteAsync(noteSend);
                    //}
                    await call.RequestStream.CompleteAsync();

                    await responseReaderTask;
                }
            }
            catch (RpcException e)
            {
                //Log("RPC failed", e);
                throw;
            }
        }