private void checkBoxTurn_CheckedChanged(object sender, EventArgs e) { if (checkBoxTurn.Checked) { var ui = TaskScheduler.FromCurrentSynchronizationContext(); Task.Factory.StartNew(delegate() { using (var mc = new ManagedConductor()) { var ok = mc.RunTurnServer("0.0.0.0:3478", textBoxExtIP.Text, "test", "auth.txt"); if (!ok) { Task.Factory.StartNew(delegate() { MessageBox.Show("TURN server start failed ;/"); }, CancellationToken.None, TaskCreationOptions.None, ui); } else { using (turnCancel = new CancellationTokenSource()) { var stop = turnCancel.Token; while (!stop.IsCancellationRequested && mc.ProcessMessages(1000)) { Debug.WriteLine("."); } Task.Factory.StartNew(delegate() { MessageBox.Show("TURN server stoped."); }, CancellationToken.None, TaskCreationOptions.None, ui); } } } }, TaskCreationOptions.LongRunning); } else { if (turnCancel != null && !turnCancel.IsCancellationRequested) { turnCancel.Cancel(); turnCancel = null; checkBoxTurn.Enabled = false; // after dispose it fails to start again wtf?.. ;/ } } }
private Task InitConnection() { TaskCompletionSource <bool> completion = new TaskCompletionSource <bool>(); _conductorThread = new Thread(() => { Thread.Sleep(5); //Wait for function to return at least! try { _conductor = new ManagedConductor(); ManagedConductor.InitializeSSL(); foreach (var server in IceServers) { _conductor.AddServerConfig(server.Url, server.UserName, server.Credentials); } _conductor.AddServerConfig("stun:stun1.l.google.com:19302", String.Empty, String.Empty); _conductor.AddServerConfig("stun:stun2.l.google.com:19302", String.Empty, String.Empty); _conductor.SetAudio(false); _conductor.SetVideoCapturer(640, 480, 5, false); if (!_conductor.InitializePeerConnection()) { completion.SetException(new ResonanceWebRTCConnectionFailedException(new Exception("Error initializing peer connection."))); return; } _conductor.CreateDataChannel("resonance"); _conductor.OnIceCandidate += _conductor_OnIceCandidate; _conductor.OnDataBinaryMessage += _conductor_OnDataBinaryMessage; _conductor.OnError += _conductor_OnError; _conductor.OnFailure += _conductor_OnFailure; _conductor.OnIceStateChanged += _conductor_OnIceStateChanged; _conductor.ProcessMessages(1000); } catch (Exception ex) { completion.SetException(ex); return; } _conductorInitialized = true; completion.SetResult(true); while (!_closeConnection) { _conductor.ProcessMessages(1000); Thread.Sleep(10); } _conductor.OnIceCandidate -= _conductor_OnIceCandidate; _conductor.OnDataBinaryMessage -= _conductor_OnDataBinaryMessage; _conductor.OnError -= _conductor_OnError; _conductor.OnFailure -= _conductor_OnFailure; _conductor.OnIceStateChanged -= _conductor_OnIceStateChanged; try { _conductor.Dispose(); } catch { } }); _conductorThread.SetApartmentState(ApartmentState.STA); _conductorThread.IsBackground = true; _conductorThread.Start(); return(completion.Task); }
private void checkBoxTurn_CheckedChanged(object sender, EventArgs e) { if (checkBoxTurn.Checked) { var ui = TaskScheduler.FromCurrentSynchronizationContext(); Task.Factory.StartNew(delegate () { using (var mc = new ManagedConductor()) { var ok = mc.RunTurnServer("0.0.0.0:3478", textBoxExtIP.Text, "test", "auth.txt"); if (!ok) { Task.Factory.StartNew(delegate () { MessageBox.Show("TURN server start failed ;/"); }, CancellationToken.None, TaskCreationOptions.None, ui); } else { using (turnCancel = new CancellationTokenSource()) { var stop = turnCancel.Token; while (!stop.IsCancellationRequested && mc.ProcessMessages(1000)) { Debug.WriteLine("."); } Task.Factory.StartNew(delegate () { MessageBox.Show("TURN server stoped."); }, CancellationToken.None, TaskCreationOptions.None, ui); } } } }, TaskCreationOptions.LongRunning); } else { if (turnCancel != null && !turnCancel.IsCancellationRequested) { turnCancel.Cancel(); turnCancel = null; checkBoxTurn.Enabled = false; // after dispose it fails to start again wtf?.. ;/ } } }