public void Dispose() { Client?.Close(); _server?.Stop(); while (!NetworkUnil.IsTcpPortAvailable(ServerPort)) { Thread.Sleep(1000); } }
public void Initialize(string path, IRequestHandler handler) { using (var mutex = new Mutex(false, GlobalMutexId)) { bool mutexAcquired = false; try { // Because the tests can run on multiple threads we must synchronize // to ensure that we don't start different test servers on the same port. if ((mutexAcquired = mutex.WaitOne(5000, false))) { if (!Initialized) { ServerPort = NetworkUnil.FindAvailableTcpPort(); var dispatcher = new DefaultRequestDispatcher(); dispatcher.RegisterHandler(path, handler); _path = path; _server = new RtspServer(ServerPort, dispatcher); _server.Start(); // Wait until the serer port is not available. while (NetworkUnil.IsTcpPortAvailable(ServerPort)) { Thread.Sleep(1000); } Client = new RtspClient(ServerUriEndpoint); Initialized = true; } } } catch (AbandonedMutexException) { // do nothing } catch (Exception) { // Do nothing since this is just a test, and if we fail here the tests // are going to fail also. } finally { if (mutexAcquired) { mutex.ReleaseMutex(); } } } }