/// <summary> /// Waits for pending requests to finish, and channels to close. /// </summary> /// <param name="timeout">The maximum time to wait for the forwarded port to stop.</param> partial void InternalStop(TimeSpan timeout) { if (timeout == TimeSpan.Zero) { return; } var stopWatch = new Stopwatch(); stopWatch.Start(); // break out of loop when one of the following conditions are met: // * the forwarded port is restarted // * all pending requests have been processed and corresponding channel are closed // * the specified timeout has elapsed while (!IsStarted) { // break out of loop when all pending requests have been processed if (Interlocked.CompareExchange(ref _pendingRequests, 0, 0) == 0) { break; } // break out of loop when specified timeout has elapsed if (stopWatch.Elapsed >= timeout && timeout != SshNet.Session.InfiniteTimeSpan) { break; } // give channels time to process pending requests ThreadAbstraction.Sleep(50); } stopWatch.Stop(); }
public void ShouldExecuteActionOnSeparateThread() { DateTime? executionTime = null; int executionCount = 0; EventWaitHandle waitHandle = new ManualResetEvent(false); Action action = () => { ThreadAbstraction.Sleep(500); executionCount++; executionTime = DateTime.Now; waitHandle.Set(); }; DateTime start = DateTime.Now; ThreadAbstraction.ExecuteThread(action); Assert.AreEqual(0, executionCount); Assert.IsNull(executionTime); Assert.IsTrue(waitHandle.WaitOne(2000)); Assert.AreEqual(1, executionCount); Assert.IsNotNull(executionTime); var elapsedTime = executionTime.Value - start; Assert.IsTrue(elapsedTime > TimeSpan.Zero); Assert.IsTrue(elapsedTime > TimeSpan.FromMilliseconds(500)); Assert.IsTrue(elapsedTime < TimeSpan.FromMilliseconds(1000)); }
partial void InternalStop(TimeSpan timeout) { if (timeout == TimeSpan.Zero) { return; } var stopWatch = new Stopwatch(); stopWatch.Start(); while (true) { // break out of loop when all pending requests have been processed if (Interlocked.CompareExchange(ref _pendingRequests, 0, 0) == 0) { break; } // break out of loop when specified timeout has elapsed if (stopWatch.Elapsed >= timeout && timeout != SshNet.Session.InfiniteTimeSpan) { break; } // give channels time to process pending requests ThreadAbstraction.Sleep(50); } stopWatch.Stop(); }
/// <summary> /// Called when channel is closed by the server. /// </summary> protected override void OnClose() { base.OnClose(); // This timeout needed since when channel is closed it does not immediately becomes available // but it takes time for the server to clean up resource and allow new channels to be created. ThreadAbstraction.Sleep(100); }
private static int ReadByte(Stream stream) { var b = stream.ReadByte(); while (b < 0) { ThreadAbstraction.Sleep(100); b = stream.ReadByte(); } return(b); }
/// <summary> /// Stops remote port forwarding. /// </summary> /// <param name="timeout">The maximum amount of time to wait for pending requests to finish processing.</param> protected override void StopPort(TimeSpan timeout) { // if the port not started, then there's nothing to stop if (!IsStarted) { return; } // mark forwarded port stopped, this also causes open of new channels to be rejected _isStarted = false; base.StopPort(timeout); // send global request to cancel direct tcpip Session.SendMessage(new GlobalRequestMessage(GlobalRequestName.CancelTcpIpForward, true, BoundHost, BoundPort)); // wait for response on global request to cancel direct tcpip or completion of message // listener loop (in which case response on global request can never be received) WaitHandle.WaitAny(new[] { _globalRequestResponse, Session.MessageListenerCompleted }, timeout); // unsubscribe from session events as either the tcpip forward is cancelled at the // server, or our session message loop has completed Session.RequestSuccessReceived -= Session_RequestSuccess; Session.RequestFailureReceived -= Session_RequestFailure; Session.ChannelOpenReceived -= Session_ChannelOpening; var startWaiting = DateTime.Now; while (true) { // break out of loop when all pending requests have been processed if (Interlocked.CompareExchange(ref _pendingRequests, 0, 0) == 0) { break; } // determine time elapsed since waiting for pending requests to finish var elapsed = DateTime.Now - startWaiting; // break out of loop when specified timeout has elapsed if (elapsed >= timeout && timeout != SshNet.Session.InfiniteTimeSpan) { break; } // give channels time to process pending requests ThreadAbstraction.Sleep(50); } }