Exemple #1
0
        public Task ConnectAsync(AsyncDuplexStreamingCall <T, S> sender,
                                 Func <S, ProcessResult> process_function,
                                 Action <Exception> exception_function,
                                 CancellationToken token)
        {
            Cancel();
            canceling         = false;
            Sender            = sender.ToADStreamingCall();
            _tokenSource      = new CancellationTokenSource();
            _completeTask     = new TaskCompletionSource <bool>();
            _needCompletition = true;
            token.Register(() => { _tokenSource?.Cancel(); });

#pragma warning disable 4014
            Task.Run(async() =>
#pragma warning restore 4014
            {
                try
                {
                    while (await Sender.Call.ResponseStream.MoveNext())
                    {
                        _tokenSource.Token.ThrowIfCancellationRequested();
                        S evnt = Sender.Call.ResponseStream.Current;
                        ProcessResult result = process_function(evnt);
                        if (result == ProcessResult.ConnectionComplete)
                        {
                            ShouldCompleteConnection();
                        }
                        if (result == ProcessResult.Exit)
                        {
                            break;
                        }
                    }
                }
                catch (TaskCanceledException f)
                {
                    logger.Debug($"Stream Task Canceled");
                    ShouldCompleteConnectionWithError(f);
                }
                catch (Exception e)
                {
                    if (!canceling)
                    {
                        logger.Debug($"Stream Exception {e.Message}");
                        //RpcException rpc = e as RpcException;
                        if (!ShouldCompleteConnectionWithError(e))
                        {
                            exception_function(e);
                        }
                    }
                    else
                    {
                        logger.Debug($"Stream Cancel in progress");
                    }
                }

                logger.Debug($"Stream completed");
            });
            return(_completeTask.Task);
        }
Exemple #2
0
 public void Cancel()
 {
     canceling = true;
     Sender?.Dispose();
     _tokenSource?.Cancel();
     _tokenSource      = null;
     _needCompletition = false;
     Sender            = null;
 }