/// <summary> /// Marks this operation as timed-out, callbacks with the exception /// and sets a handler when the response is received /// </summary> public bool SetTimedOut(OperationTimedOutException ex, Action onReceive) { var callback = _callback; //When the data is received, invoke on receive callback _callback = (_, __) => onReceive(); //Set the _callback first, as the Invoke method does not check previous state Thread.MemoryBarrier(); var previousState = Interlocked.Exchange(ref _state, StateTimedout); switch (previousState) { case StateInit: //Call the original callback Task.Factory.StartNew(() => callback(ex, null), CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default); return(true); case StateCompleted: //it arrived while changing the state //Invoke on receive _callback = Noop; //it hasn't actually timed out return(false); } //For cancelled, do not invoke the previous return(true); }
private void OnTimeout(OperationState state) { var ex = new OperationTimedOutException(Address, Configuration.SocketOptions.ReadTimeoutMillis); //Invoke if it hasn't been invoked yet //Once the response is obtained, we decrement the timed out counter var timedout = state.SetTimedOut(ex, () => Interlocked.Decrement(ref _timedOutOperations)); if (!timedout) { //The response was obtained since the timer elapsed, move on return; } //Increase timed-out counter Interlocked.Increment(ref _timedOutOperations); }
/// <summary> /// Marks this operation as timed-out, callbacks with the exception /// and sets a handler when the response is received /// </summary> public bool SetTimedOut(OperationTimedOutException ex, Action onReceive) { var previousState = Interlocked.CompareExchange(ref _state, StateTimedout, StateInit); if (previousState != StateInit) { return(false); } //When the data is received, invoke on receive callback var callback = Interlocked.Exchange(ref _callback, (_, __) => onReceive()); Thread.MemoryBarrier(); _timeoutCallbackSet = true; Task.Factory.StartNew(() => callback(ex, null), CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default); return(true); }
/// <summary> /// Marks this operation as timed-out, callbacks with the exception /// and sets a handler when the response is received /// </summary> public bool MarkAsTimedOut(OperationTimedOutException ex, Action onReceive, long timestamp) { var previousState = Interlocked.CompareExchange(ref _state, StateTimedout, StateInit); if (previousState != StateInit) { return(false); } //When the data is received, invoke on receive callback var callback = Interlocked.Exchange(ref _callback, (_, __, ___) => onReceive()); #if !NETCORE Thread.MemoryBarrier(); #else Interlocked.MemoryBarrier(); #endif _timeoutCallbackSet = true; Task.Factory.StartNew(() => callback(RequestError.CreateClientError(ex, false), null, timestamp), CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default); return(true); }
private void OnTimeout(object stateObj) { var streamId = (short)stateObj; OperationState state; if (!_pendingOperations.TryGetValue(streamId, out state)) { return; } var ex = new OperationTimedOutException(Address, Configuration.SocketOptions.ReadTimeoutMillis); //Invoke if it hasn't been invoked yet //Once the response is obtained, we decrement the timed out counter var timedout = state.SetTimedOut(ex, () => Interlocked.Decrement(ref _timedOutOperations) ); if (!timedout) { //The response was obtained since the timer elapsed, move on return; } //Increase timed-out counter Interlocked.Increment(ref _timedOutOperations); }
private void OnTimeout(OperationState state) { var ex = new OperationTimedOutException(Address, Configuration.SocketOptions.ReadTimeoutMillis); //Invoke if it hasn't been invoked yet //Once the response is obtained, we decrement the timed out counter var timedout = state.SetTimedOut(ex, () => Interlocked.Decrement(ref _timedOutOperations) ); if (!timedout) { //The response was obtained since the timer elapsed, move on return; } //Increase timed-out counter Interlocked.Increment(ref _timedOutOperations); }
/// <summary> /// Marks this operation as timed-out, callbacks with the exception /// and sets a handler when the response is received /// </summary> public bool SetTimedOut(OperationTimedOutException ex, Action onReceive) { var callback = _callback; //When the data is received, invoke on receive callback _callback = (_, __) => onReceive(); //Set the _callback first, as the Invoke method does not check previous state Thread.MemoryBarrier(); var previousState = Interlocked.Exchange(ref _state, StateTimedout); switch (previousState) { case StateInit: //Call the original callback Task.Factory.StartNew(() => callback(ex, null), CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default); return true; case StateCompleted: //it arrived while changing the state //Invoke on receive _callback = Noop; //it hasn't actually timed out return false; } //For cancelled, do not invoke the previous return true; }
/// <summary> /// Marks this operation as timed-out, callbacks with the exception /// and sets a handler when the response is received /// </summary> public bool SetTimedOut(OperationTimedOutException ex, Action onReceive) { var previousState = Interlocked.CompareExchange(ref _state, StateTimedout, StateInit); if (previousState != StateInit) { return false; } //When the data is received, invoke on receive callback var callback = Interlocked.Exchange(ref _callback, (_, __) => onReceive()); Thread.MemoryBarrier(); _timeoutCallbackSet = true; Task.Factory.StartNew(() => callback(ex, null), CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default); return true; }