//
        // StartRequestHandlerTask()
        //
        // Take any messages on our PendingRequest list and write them to GCP
        private Task StartRequestHandlerTask()
        {
            OnDebugMessage("Started Request Handler");
            return(Task.Run(async() =>
            {
                while (!Done)
                {
                    var request = PendingRequests.Take(CancellationToken);
                    OnDebugMessage("Setup listen for request");

                    //If the listener isn't active, start it
                    if (DuplexStream == null || !ListenerIsActive)
                    {
                        // Initialize streaming call, retrieving the stream object
                        DuplexStream = FirestoreClient.Listen(ListenSettings);
                        ListenerIsActive = true;
                        OnDebugMessage("Response Task Not Active, starting");
                        ResponseHanderTask = StartResponseHandlerTask();
                    }
                    OnDebugMessage("Sending Request");
                    // Stream a request to the server
                    await DuplexStream.WriteAsync(request);
                    ActiveRequests.Enqueue(request);
                }
                OnDebugMessage("Request Handler Completed");
                await DuplexStream.WriteCompleteAsync();
            }));
        }
Esempio n. 2
0
        /// <summary>Snippet for Listen</summary>
        public async Task Listen()
        {
            // Snippet: Listen(CallSettings, BidirectionalStreamingSettings)
            // Create client
            FirestoreClient firestoreClient = FirestoreClient.Create();

            // Initialize streaming call, retrieving the stream object
            FirestoreClient.ListenStream response = firestoreClient.Listen();

            // Sending requests and retrieving responses can be arbitrarily interleaved
            // Exact sequence will depend on client/server behavior

            // Create task to do something with responses from server
            Task responseHandlerTask = Task.Run(async() =>
            {
                // Note that C# 8 code can use await foreach
                AsyncResponseStream <ListenResponse> responseStream = response.GetResponseStream();
                while (await responseStream.MoveNextAsync())
                {
                    ListenResponse responseItem = responseStream.Current;
                    // Do something with streamed response
                }
                // The response stream has completed
            });

            // Send requests to the server
            bool done = false;

            while (!done)
            {
                // Initialize a request
                ListenRequest request = new ListenRequest
                {
                    Database     = "",
                    AddTarget    = new Target(),
                    RemoveTarget = 0,
                    Labels       = { { "", "" }, },
                };
                // Stream a request to the server
                await response.WriteAsync(request);

                // Set "done" to true when sending requests is complete
            }

            // Complete writing requests to the stream
            await response.WriteCompleteAsync();

            // Await the response handler
            // This will complete once all server responses have been processed
            await responseHandlerTask;
            // End snippet
        }
        /// <summary>Snippet for Listen</summary>
        public async Task Listen()
        {
            // Snippet: Listen(CallSettings,BidirectionalStreamingSettings)
            // Create client
            FirestoreClient firestoreClient = FirestoreClient.Create();

            // Initialize streaming call, retrieving the stream object
            FirestoreClient.ListenStream duplexStream = firestoreClient.Listen();

            // Sending requests and retrieving responses can be arbitrarily interleaved.
            // Exact sequence will depend on client/server behavior.

            // Create task to do something with responses from server
            Task responseHandlerTask = Task.Run(async() =>
            {
                IAsyncEnumerator <ListenResponse> responseStream = duplexStream.ResponseStream;
                while (await responseStream.MoveNext())
                {
                    ListenResponse response = responseStream.Current;
                    // Do something with streamed response
                }
                // The response stream has completed
            });

            // Send requests to the server
            bool done = false;

            while (!done)
            {
                // Initialize a request
                ListenRequest request = new ListenRequest
                {
                    Database = new DatabaseRootName("[PROJECT]", "[DATABASE]").ToString(),
                };
                // Stream a request to the server
                await duplexStream.WriteAsync(request);

                // Set "done" to true when sending requests is complete
            }
            // Complete writing requests to the stream
            await duplexStream.WriteCompleteAsync();

            // Await the response handler.
            // This will complete once all server responses have been processed.
            await responseHandlerTask;
            // End snippet
        }