/// <summary> /// Compares the current state with a passed in state for equality and /// if they are equal, replaces the state with the passed in value. /// </summary> /// <param name="value">The passed in state.</param> /// <param name="comparand">The state that is compared to the current state.</param> /// <returns></returns> public SCTPSocketState CompareExchangeState(SCTPSocketState value, params SCTPSocketState[] comparands) { Utility.ThrowIfArgumentNullOrEmpty("comparands", comparands); SCTPSocketState state = (SCTPSocketState)this.state; for (int i = 0; i < comparands.Length; i++) { state = (SCTPSocketState)Interlocked.CompareExchange(ref this.state, (int)value, (int)comparands[i]); if (comparands.Contains(state) == true) { break; } } return(state); }
/// <summary> /// Waits a specified amount of time for the TCB to have a state from a list of specified states. /// </summary> /// <param name="states">A list of states to wait for.</param> /// <param name="timeout">The amount of time to wait.</param> /// <param name="state">The state on exit.</param> /// <returns>True if one of the states is met within the timeout period; otherwise false.</returns> public bool WaitForStates(SCTPSocketState[] states, int timeout, out SCTPSocketState state) { state = this.State; DateTime timeoutTime = DateTime.UtcNow.AddMilliseconds(timeout); while (timeout == -1 || DateTime.UtcNow < timeoutTime) { for (int i = 0; i < states.Length; i++) { state = this.State; if (state == states[i]) { return(true); } } Thread.Sleep(100); } return(false); }
/// <summary> /// Waits a specified amount of time for the TCB to have a specified state. /// </summary> /// <param name="state">The state to wait for.</param> /// <param name="timeout">The amount of time to wait.</param> /// <returns>True if the state is met within the timeout period; otherwise false.</returns> public bool WaitForState(SCTPSocketState state, int timeout) { return(this.WaitForStates(new SCTPSocketState[] { state }, timeout, out SCTPSocketState s)); }