Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for monitoring changes to connections. #29

Merged
merged 24 commits into from
Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
a6d430d
Initial check-in of the connection monitoring feature.
jeff-winn Jan 10, 2019
99a2688
Merge pull request #27 from winnster/master
jeff-winn Jan 12, 2019
3557560
Merging changes from WIN7 to features/monitoring.
jeff-winn Jan 12, 2019
7e28868
Fixing merge issues.
jeff-winn Jan 12, 2019
4b51a56
Correcting the namespace of the Composition classes.
jeff-winn Jan 13, 2019
e07ba9f
Correcting the folder name for the composition tests namespace.
jeff-winn Jan 13, 2019
d5445fd
Moving the service provider extensions.
jeff-winn Jan 14, 2019
bc0495a
Updating the connection watcher to support reuse of the component.
jeff-winn Jan 16, 2019
f908633
Correcting the namespace for the composition abstractions.
jeff-winn Jan 16, 2019
6a44f54
Adding registered callback support to simplify the notification callb…
jeff-winn Feb 6, 2019
f18b59d
Adding unit tests for the RasConnectionWatcher.
jeff-winn Feb 6, 2019
f544bf4
Fixing some code analysis warnings.
jeff-winn Feb 6, 2019
212f995
Changing apis to use leaky abstraction of IDisposable for maintenance…
jeff-winn Feb 22, 2019
5f8f11f
Moving the console runner to the tools folder.
jeff-winn Mar 7, 2019
8c254b3
Restructuring the console runner csproj file to the new format.
jeff-winn Mar 7, 2019
28d6230
Updating the public api for the connection watcher.
jeff-winn Mar 11, 2019
3477c03
Updating the console runner.
jeff-winn Mar 12, 2019
b4ad82b
Wrapping the connection information in a class.
jeff-winn Mar 12, 2019
b31e86c
Repositioning the injection code to dependency injection.
jeff-winn Mar 12, 2019
8018b3b
Replacing DependencyInjection with IoC namespace.
jeff-winn Mar 12, 2019
28db77e
Changing to use Microsoft MinIoC container.
jeff-winn Mar 13, 2019
e445200
Fixing an issue when restarting the watcher.
jeff-winn Mar 13, 2019
7b79f5e
Removing unused classes.
jeff-winn Mar 13, 2019
0f236b5
Adding error event for handling background thread exceptions.
jeff-winn Mar 13, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions DotRas.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27428.2037
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "1. Tests", "1. Tests", "{9D34A309-06DD-4B78-9424-672421884BCB}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "2. Tests", "2. Tests", "{9D34A309-06DD-4B78-9424-672421884BCB}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{C1AE3EAA-AFFF-4D81-8BAF-53A000F40235}"
ProjectSection(SolutionItems) = preProject
.gitignore = .gitignore
AnyCPU.runsettings = AnyCPU.runsettings
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "2. Runner", "2. Runner", "{6908689B-72AB-4390-BDAA-857EA62E925A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleRunner", "test\ConsoleRunner\ConsoleRunner.csproj", "{82CDADEF-9BA9-480C-A059-DAF35FD69B08}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotRas", "src\DotRas\DotRas.csproj", "{DA4B359A-F13F-46FE-81C4-FCD6EB7EB2CA}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotRas.Tests", "test\DotRas.Tests\DotRas.Tests.csproj", "{1F46ADDE-3E40-4BB3-B933-11DD919839AE}"
Expand All @@ -33,16 +29,20 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Includes", "Includes", "{48
includes\um\winnt.h = includes\um\winnt.h
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "3. Tools", "3. Tools", "{B3BCF167-8512-4154-A462-5BC1F7B1925F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleRunner", "tools\ConsoleRunner\ConsoleRunner.csproj", "{82CDADEF-9BA9-480C-A059-DAF35FD69B08}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "1. Examples", "1. Examples", "{C342DFB6-A477-4974-B389-102D84DD4F2E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WatchConnectionsForChanges", "examples\WatchConnectionsForChanges\WatchConnectionsForChanges.csproj", "{A3DE66F4-33AD-4F63-A989-7382CC4CEA42}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{82CDADEF-9BA9-480C-A059-DAF35FD69B08}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{82CDADEF-9BA9-480C-A059-DAF35FD69B08}.Debug|Any CPU.Build.0 = Debug|Any CPU
{82CDADEF-9BA9-480C-A059-DAF35FD69B08}.Release|Any CPU.ActiveCfg = Release|Any CPU
{82CDADEF-9BA9-480C-A059-DAF35FD69B08}.Release|Any CPU.Build.0 = Release|Any CPU
{DA4B359A-F13F-46FE-81C4-FCD6EB7EB2CA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DA4B359A-F13F-46FE-81C4-FCD6EB7EB2CA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DA4B359A-F13F-46FE-81C4-FCD6EB7EB2CA}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -51,14 +51,23 @@ Global
{1F46ADDE-3E40-4BB3-B933-11DD919839AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1F46ADDE-3E40-4BB3-B933-11DD919839AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1F46ADDE-3E40-4BB3-B933-11DD919839AE}.Release|Any CPU.Build.0 = Release|Any CPU
{82CDADEF-9BA9-480C-A059-DAF35FD69B08}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{82CDADEF-9BA9-480C-A059-DAF35FD69B08}.Debug|Any CPU.Build.0 = Debug|Any CPU
{82CDADEF-9BA9-480C-A059-DAF35FD69B08}.Release|Any CPU.ActiveCfg = Release|Any CPU
{82CDADEF-9BA9-480C-A059-DAF35FD69B08}.Release|Any CPU.Build.0 = Release|Any CPU
{A3DE66F4-33AD-4F63-A989-7382CC4CEA42}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A3DE66F4-33AD-4F63-A989-7382CC4CEA42}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A3DE66F4-33AD-4F63-A989-7382CC4CEA42}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A3DE66F4-33AD-4F63-A989-7382CC4CEA42}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{82CDADEF-9BA9-480C-A059-DAF35FD69B08} = {6908689B-72AB-4390-BDAA-857EA62E925A}
{1F46ADDE-3E40-4BB3-B933-11DD919839AE} = {9D34A309-06DD-4B78-9424-672421884BCB}
{48D5F528-BF5E-4B79-B6A2-3CD77E28C482} = {C1AE3EAA-AFFF-4D81-8BAF-53A000F40235}
{82CDADEF-9BA9-480C-A059-DAF35FD69B08} = {B3BCF167-8512-4154-A462-5BC1F7B1925F}
{A3DE66F4-33AD-4F63-A989-7382CC4CEA42} = {C342DFB6-A477-4974-B389-102D84DD4F2E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3BBA19DF-42BF-442B-95C4-17D4A554714E}
Expand Down
6 changes: 6 additions & 0 deletions examples/WatchConnectionsForChanges/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1" />
</startup>
</configuration>
87 changes: 87 additions & 0 deletions examples/WatchConnectionsForChanges/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System;
using DotRas;

namespace WatchConnectionsForChanges
{
internal class Program : IDisposable
{
private readonly RasConnectionWatcher watcher;

public static void Main(string[] args)
{
try
{
using (var p = new Program())
{
p.Run();
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}

Console.WriteLine("Press any key to terminate...");
Console.ReadKey(true);
}

public Program()
{
watcher = new RasConnectionWatcher();

watcher.Connected += OnConnectionConnected;
watcher.Disconnected += OnConnectionDisconnected;
}

~Program()
{
Dispose(false);
}

/// <summary>
/// This method gets called when the operating system notifies DotRas that a new connection has been established.
/// </summary>
/// <param name="sender">This is the object which raised the event.</param>
/// <param name="e">This object carries the event data.</param>
private void OnConnectionConnected(object sender, RasConnectionEventArgs e)
{
Console.WriteLine($"Connected: {e.ConnectionInformation.EntryName}");
}

/// <summary>
/// This method gets called when the operating system notifies DotRas that an existing connection has disconnected.
/// </summary>
/// <param name="sender">This is the object which raised the event.</param>
/// <param name="e">This object carries the event data.</param>
private void OnConnectionDisconnected(object sender, RasConnectionEventArgs e)
{
Console.WriteLine($"Disconnected: {e.ConnectionInformation.EntryName}");
}

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 Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

private void Dispose(bool disposing)
{
if (disposing)
{
watcher.Dispose();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net471</TargetFramework>
<ApplicationIcon />
<OutputType>Exe</OutputType>
<StartupObject />
<AssemblyName>WatchConnectionsForChanges</AssemblyName>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\DotRas\DotRas.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>

</Project>
5 changes: 5 additions & 0 deletions src/DotRas/DisposableObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public void Dispose()

lock (SyncRoot)
{
if (disposed)
{
return;
}

Dispose(true);
GC.SuppressFinalize(this);
}
Expand Down
30 changes: 30 additions & 0 deletions src/DotRas/ErrorEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;

namespace DotRas
{
/// <summary>
/// Contains event data for error events.
/// </summary>
public class ErrorEventArgs : EventArgs
{
private readonly Exception error;

/// <summary>
/// Initializes a new instance of the <see cref="ErrorEventArgs"/> class.
/// </summary>
/// <param name="error">The exception which occurred.</param>
public ErrorEventArgs(Exception error)
{
this.error = error ?? throw new ArgumentNullException(nameof(error));
}

/// <summary>
/// Gets the exception which occurred.
/// </summary>
/// <returns>The exception which occurred.</returns>
public Exception GetException()
{
return error;
}
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Threading;
using DotRas.Internal.Abstractions.Primitives;

namespace DotRas.Internal.Abstractions.Factories
{
internal interface IRegisteredCallbackFactory
{
IRegisteredCallback Create(WaitOrTimerCallback callback, object state);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.Threading;
using System;
using System.Threading;

namespace DotRas.Internal.Abstractions.Primitives
{
internal interface IManualResetEvent
internal interface IManualResetEvent : IDisposable
{
void Set();
void Reset();
Expand Down
10 changes: 10 additions & 0 deletions src/DotRas/Internal/Abstractions/Primitives/IRegisteredCallback.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using Microsoft.Win32.SafeHandles;

namespace DotRas.Internal.Abstractions.Primitives
{
internal interface IRegisteredCallback : IDisposable
{
SafeWaitHandle Handle { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace DotRas.Internal.Abstractions.Primitives
{
internal interface ITaskCancellationSource
internal interface ITaskCancellationSource : IDisposable
{
CancellationToken Token { get; }

Expand Down
5 changes: 3 additions & 2 deletions src/DotRas/Internal/Abstractions/Primitives/IValueWaiter.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.Threading;
using System;
using System.Threading;

namespace DotRas.Internal.Abstractions.Primitives
{
internal interface IValueWaiter<T>
internal interface IValueWaiter<T> : IDisposable
{
T Value { get; }
bool IsSet { get; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace DotRas.Internal.Abstractions.Services
{
internal interface IRasConnectionNotification : IDisposable
{
bool IsActive { get; }

void Subscribe(RasNotificationContext context);
void Reset();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace DotRas.Internal.Abstractions.Services
{
internal interface IRasConnectionNotificationCallbackHandler
{
void Initialize();
void OnCallback(object obj, bool timedOut);
}
}
5 changes: 3 additions & 2 deletions src/DotRas/Internal/Abstractions/Services/IRasDial.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;

namespace DotRas.Internal.Abstractions.Services
{
internal interface IRasDial
internal interface IRasDial : IDisposable
{
bool IsBusy { get; }
Task<RasConnection> DialAsync(RasDialContext context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace DotRas.Internal.Abstractions.Services
{
internal interface IRasDialCallbackHandler
internal interface IRasDialCallbackHandler : IDisposable
{
void Initialize(ITaskCompletionSource<RasConnection> completionSource, Action<StateChangedEventArgs> onStateChangedCallback, Action onCompletedCallback, CancellationToken cancellationToken);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace DotRas.Internal.Abstractions.Services
{
internal struct RasDialContext
internal class RasDialContext
{
public string PhoneBookPath { get; set; }
public string EntryName { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace DotRas.Internal.Abstractions.Services
{
internal class RasNotificationContext
{
public IRasConnection Connection { get; set; }

public Action<RasConnectionEventArgs> OnConnectedCallback { get; set; }
public Action<RasConnectionEventArgs> OnDisconnectedCallback { get; set; }
}
}
Loading