public void RaisesTheErrorEventWhenAnErrorOccursWithinDisconnectedEvent() { Action <RasConnectionEventArgs> onDisconnectedCallback = null; bool executed = false; var api = new Mock <IRasConnectionNotification>(); api.Setup(o => o.Subscribe(It.IsAny <RasNotificationContext>())).Callback <RasNotificationContext>(context => { onDisconnectedCallback = context.OnDisconnectedCallback; }); var target = new RasConnectionWatcher(api.Object); target.Disconnected += (sender, e) => { throw new TestException(); }; target.Error += (sender, e) => { executed = true; }; target.Start(); Assert.IsNotNull(onDisconnectedCallback); var eventData = new Mock <RasConnectionEventArgs>(); onDisconnectedCallback(eventData.Object); Assert.True(executed, "The error event was not executed as expected."); }
public void OnDisconnectedCallbackMustRaiseTheDisconnectedEvent() { var executed = false; var api = new Mock <IRasConnectionNotification>(); api.Setup(o => o.Subscribe(It.IsAny <RasNotificationContext>())).Callback <RasNotificationContext>(c => { Assert.Throws <ArgumentNullException>(() => c.OnDisconnectedCallback(null)); c.OnDisconnectedCallback(new RasConnectionEventArgs(new RasConnectionInformation( new IntPtr(1), "Test", "", Guid.NewGuid(), Guid.NewGuid()))); }); var target = new RasConnectionWatcher(api.Object); target.Disconnected += (sender, e) => { executed = true; }; target.Start(); Assert.True(executed); }
private void Run() { // This should contain the name. dialer.EntryName = "Your Entry"; // If your account requires credentials that have not been persisted, they can be passed here. dialer.Credentials = new NetworkCredential("Username", "Password"); // This specifies the default location for Windows phone books. dialer.PhoneBookPath = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Microsoft\Network\Connections\Pbk\rasphone.pbk"); // Dials the connection synchronously. This will still raise events, and it will also allow for timeouts // if the cancellation token is passed into the api via the overload. var connection = dialer.Connect(); Console.WriteLine($"Connected: [{connection.EntryName}] @ {connection.Handle}"); watcher.Connection = connection; watcher.Start(); // This will just force disconnect, however this could also be external if the connection is dropped due to the // network on the machine being physically disconnected. Console.WriteLine("Just waiting for a bit before forcing disconnect..."); Thread.Sleep(TimeSpan.FromSeconds(10)); connection.Disconnect(CancellationToken.None); watcher.Stop(); }
public MyCustomApplicationContext() { MainForm = new Form { ShowInTaskbar = false, WindowState = FormWindowState.Minimized }; _watcher = new RasConnectionWatcher(); _watcher.Connected += OnConnection; _watcher.Disconnected += OnDisconnected; _watcher.Start(); _checkIcon = Image.FromFile("check.png"); _offIcon = IconFromImage(Image.FromFile("off.png")); _onIcon = IconFromImage(Image.FromFile("on.png")); _trayIcon = new NotifyIcon { Visible = true, ContextMenuStrip = new ContextMenuStrip() }; UpdateTrayIcon(); }
private async Task RunCoreAsync() { watcher.Start(); while (ShouldContinueExecution()) { try { using (var tcs = CancellationTokenSource.CreateLinkedTokenSource(CancellationSource.Token)) { try { await ConnectAsync(tcs.Token); } finally { await DisconnectAsync(tcs.Token); } WaitUntilNextExecution(tcs.Token); } } catch (Exception ex) { Console.WriteLine(ex); } } watcher.Stop(); }
public void DialUp() { try { dialer.PhoneBookPath = Path.Combine( AppDomain.CurrentDomain.SetupInformation.ApplicationBase, @"PPPoEd.pbk"); dialer.EntryName = "PPPoEd"; dialer.Credentials = new NetworkCredential(Username, Password); //__establish_connection__ Logger.Write("Connecting..."); connection = dialer.Connect(); Logger.Write($"Connected: [{connection.EntryName}] @ {connection.Handle}"); //__connection_watch__ watcher.Connection = connection; watcher.Start(); } //__dial_failed__ catch (RasException e) { Logger.Write($"Error {e.NativeErrorCode}: {e.Message}"); ReDial(); } }
public void ThrowsAnExceptionWhenStartAfterDisposed() { var api = new Mock <IRasConnectionNotification>(); var target = new RasConnectionWatcher(api.Object); target.Dispose(); Assert.Throws <ObjectDisposedException>(() => target.Start()); }
private void Run() { // Start watching for connection changes. watcher.Start(); Console.WriteLine("Press any key to stop watching for connection changes..."); Console.ReadKey(true); // Stop watching for connection changes. watcher.Stop(); }
public void StartWillSubscribeWithoutAConnection() { var api = new Mock <IRasConnectionNotification>(); api.Setup(o => o.Subscribe(It.IsAny <RasNotificationContext>())).Callback <RasNotificationContext>(c => { Assert.IsNull(c.Connection); Assert.IsNotNull(c.OnConnectedCallback); Assert.IsNotNull(c.OnDisconnectedCallback); }); var target = new RasConnectionWatcher(api.Object); target.Start(); api.Verify(o => o.Subscribe(It.IsAny <RasNotificationContext>()), Times.Once); }
private async Task RunCoreAsync() { watcher.Start(); while (ShouldContinueExecution()) { using var tcs = CancellationTokenSource.CreateLinkedTokenSource(CancellationSource.Token); try { await RunOnceAsync(tcs.Token); } finally { await WaitForALittleWhileAsync(tcs.Token, false); } } watcher.Stop(); }
public void RestartsTheWatcherWhenChangedWhileActive() { var connection = new Mock <RasConnection>(); var api = new Mock <IRasConnectionNotification>(); api.SetupSequence(o => o.IsActive) .Returns(false) .Returns(true) .Returns(true) .Returns(false); var target = new RasConnectionWatcher(api.Object); target.Start(); target.Connection = connection.Object; api.Verify(o => o.Subscribe(It.IsAny <RasNotificationContext>()), Times.Exactly(2)); api.Verify(o => o.Reset(), Times.Once); }