Exemplo n.º 1
0
        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.");
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        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();
        }
Exemplo n.º 4
0
        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();
        }
Exemplo n.º 5
0
        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();
        }
Exemplo n.º 6
0
Arquivo: PPPoE.cs Projeto: qcyb/pppoed
        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();
            }
        }
Exemplo n.º 7
0
        public void ThrowsAnExceptionWhenStartAfterDisposed()
        {
            var api = new Mock <IRasConnectionNotification>();

            var target = new RasConnectionWatcher(api.Object);

            target.Dispose();

            Assert.Throws <ObjectDisposedException>(() => target.Start());
        }
Exemplo n.º 8
0
        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();
        }
Exemplo n.º 9
0
        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);
        }
Exemplo n.º 10
0
        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();
        }
Exemplo n.º 11
0
        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);
        }