当前位置: 首页>>代码示例>>C#>>正文


C# TimeoutHelper.GetCancellationTokenAsync方法代码示例

本文整理汇总了C#中System.Runtime.TimeoutHelper.GetCancellationTokenAsync方法的典型用法代码示例。如果您正苦于以下问题:C# TimeoutHelper.GetCancellationTokenAsync方法的具体用法?C# TimeoutHelper.GetCancellationTokenAsync怎么用?C# TimeoutHelper.GetCancellationTokenAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Runtime.TimeoutHelper的用法示例。


在下文中一共展示了TimeoutHelper.GetCancellationTokenAsync方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CreateWebSocketAsync

        public override async Task<WebSocket> CreateWebSocketAsync(Uri address, WebHeaderCollection headers, ICredentials credentials,
            WebSocketTransportSettings settings, TimeoutHelper timeoutHelper)
        {
            ClientWebSocket webSocket = new ClientWebSocket();
            webSocket.Options.Credentials = credentials;
            webSocket.Options.AddSubProtocol(settings.SubProtocol);
            webSocket.Options.KeepAliveInterval = settings.KeepAliveInterval;
            foreach (var headerObj in headers)
            {
                var header = headerObj as string;
                webSocket.Options.SetRequestHeader(header, headers[header]);
            }

            var cancelToken = await timeoutHelper.GetCancellationTokenAsync();
            await webSocket.ConnectAsync(address, cancelToken);
            return webSocket;
        }
开发者ID:AndyGerlicher,项目名称:wcf,代码行数:17,代码来源:CoreClrClientWebSocketFactory.cs

示例2: ReadChunkedBufferedMessageAsync

 private async Task<Message> ReadChunkedBufferedMessageAsync(Task<Stream> inputStreamTask, TimeoutHelper timeoutHelper)
 {
     try
     {
         return await _encoder.ReadMessageAsync(await inputStreamTask, _factory.BufferManager, _factory.MaxBufferSize, _contentType, await timeoutHelper.GetCancellationTokenAsync());
     }
     catch (XmlException xmlException)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
             new ProtocolException(SR.MessageXmlProtocolError, xmlException));
     }
 }
开发者ID:KKhurin,项目名称:wcf,代码行数:12,代码来源:HttpResponseMessageHelper.cs

示例3: WriteEndOfMessageAsync

            public async void WriteEndOfMessageAsync(Action<object> callback, object state)
            {
                if (TD.WebSocketAsyncWriteStartIsEnabled())
                {
                    // TODO: Open bug about not emitting the hostname/port
                    TD.WebSocketAsyncWriteStart(
                        _webSocket.GetHashCode(),
                        0,
                        string.Empty);
                }

                var timeoutHelper = new TimeoutHelper(_closeTimeout);
                var cancelTokenTask = timeoutHelper.GetCancellationTokenAsync();
                try
                {
                    var cancelToken = await cancelTokenTask;
                    await _webSocket.SendAsync(new ArraySegment<byte>(Array.Empty<byte>(), 0, 0), _outgoingMessageType, true, cancelToken);

                    if (TD.WebSocketAsyncWriteStopIsEnabled())
                    {
                        TD.WebSocketAsyncWriteStop(_webSocket.GetHashCode());
                    }
                }
                catch (Exception ex)
                {
                    if (Fx.IsFatal(ex))
                    {
                        throw;
                    }

                    if (cancelTokenTask.Result.IsCancellationRequested)
                    {
                        throw Fx.Exception.AsError(
                            new TimeoutException(InternalSR.TaskTimedOutError(timeoutHelper.OriginalTimeout)));
                    }

                    throw WebSocketHelper.ConvertAndTraceException(ex, timeoutHelper.OriginalTimeout,
                        WebSocketHelper.SendOperation);

                }
                finally
                {
                    callback.Invoke(state);
                }
            }
开发者ID:AndyGerlicher,项目名称:wcf,代码行数:45,代码来源:WebSocketTransportDuplexSessionChannel.cs

示例4: StartNextReceiveAsync

            async void StartNextReceiveAsync()
            {
                Fx.Assert(_receiveTask == null || _receiveTask.Task.IsCompleted, "this.receiveTask is not completed.");
                _receiveTask = new TaskCompletionSource<object>();
                int currentState = Interlocked.CompareExchange(ref _asyncReceiveState, AsyncReceiveState.Started, AsyncReceiveState.Finished);
                Fx.Assert(currentState == AsyncReceiveState.Finished, "currentState is not AsyncReceiveState.Finished: " + currentState);
                if (currentState != AsyncReceiveState.Finished)
                {
                    throw FxTrace.Exception.AsError(new InvalidOperationException());
                }

                try
                {
                    if (_useStreaming)
                    {
                        if (_streamWaitTask != null)
                        {
                            //// Wait until the previous stream message finished.
                            await _streamWaitTask.Task;
                        }

                        _streamWaitTask = new TaskCompletionSource<object>();
                    }

                    if (_pendingException == null)
                    {
                        if (!_useStreaming)
                        {
                            await ReadBufferedMessageAsync();
                        }
                        else
                        {
                            byte[] buffer = _bufferManager.TakeBuffer(_receiveBufferSize);
                            bool success = false;
                            try
                            {
                                if (TD.WebSocketAsyncReadStartIsEnabled())
                                {
                                    TD.WebSocketAsyncReadStart(_webSocket.GetHashCode());
                                }

                                try
                                {
                                    WebSocketReceiveResult result;
                                    TimeoutHelper helper = new TimeoutHelper(_asyncReceiveTimeout);
                                    var cancelToken = await helper.GetCancellationTokenAsync();
                                    result = await _webSocket.ReceiveAsync(new ArraySegment<byte>(buffer, 0, _receiveBufferSize), cancelToken);
                                    CheckCloseStatus(result);
                                    _pendingMessage = PrepareMessage(result, buffer, result.Count);

                                    if (TD.WebSocketAsyncReadStopIsEnabled())
                                    {
                                        TD.WebSocketAsyncReadStop(
                                            _webSocket.GetHashCode(),
                                            result.Count,
                                            String.Empty);
                                    }
                                }
                                catch (AggregateException ex)
                                {
                                    WebSocketHelper.ThrowCorrectException(ex, _asyncReceiveTimeout, WebSocketHelper.ReceiveOperation);
                                }
                                success = true;
                            }
                            catch (Exception ex)
                            {
                                if (Fx.IsFatal(ex))
                                {
                                    throw;
                                }

                                _pendingException = WebSocketHelper.ConvertAndTraceException(ex, _asyncReceiveTimeout, WebSocketHelper.ReceiveOperation);
                            }
                            finally
                            {
                                if (!success)
                                {
                                    _bufferManager.ReturnBuffer(buffer);
                                }
                            }
                        }
                    }
                }
                finally
                {
                    if (Interlocked.CompareExchange(ref _asyncReceiveState, AsyncReceiveState.Finished, AsyncReceiveState.Started) == AsyncReceiveState.Started)
                    {
                        _receiveTask.SetResult(null);
                    }
                }
            }
开发者ID:SoumikMukherjeeDOTNET,项目名称:wcf,代码行数:91,代码来源:WebSocketTransportDuplexSessionChannel.cs

示例5: CloseOutputSessionCoreAsync

        protected override async Task CloseOutputSessionCoreAsync(TimeSpan timeout)
        {
            if (TD.WebSocketCloseOutputSentIsEnabled())
            {
                TD.WebSocketCloseOutputSent(
                    WebSocket.GetHashCode(),
                    _webSocketCloseDetails.OutputCloseStatus.ToString(),
                    RemoteAddress != null ? RemoteAddress.ToString() : string.Empty);
            }

            var helper = new TimeoutHelper(timeout);
            var cancelToken = await helper.GetCancellationTokenAsync();
            try
            {
                await CloseOutputAsync(cancelToken);
            }
            catch (Exception ex)
            {
                if (Fx.IsFatal(ex))
                {
                    throw;
                }

                if (cancelToken.IsCancellationRequested)
                {
                    throw Fx.Exception.AsError(new TimeoutException(InternalSR.TaskTimedOutError(timeout)));
                }

                throw WebSocketHelper.ConvertAndTraceException(ex, timeout, WebSocketHelper.ReceiveOperation);
            }
        }
开发者ID:SoumikMukherjeeDOTNET,项目名称:wcf,代码行数:31,代码来源:WebSocketTransportDuplexSessionChannel.cs


注:本文中的System.Runtime.TimeoutHelper.GetCancellationTokenAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。