Skip to content

Commit

Permalink
.NET 4.8 & 6.0. Nullable reference types.
Browse files Browse the repository at this point in the history
  • Loading branch information
menees committed Jan 3, 2022
1 parent 8ba3c7a commit 5cf68c5
Show file tree
Hide file tree
Showing 24 changed files with 322 additions and 285 deletions.
36 changes: 27 additions & 9 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,58 @@
<RepositoryUrl>https://github.com/menees/WirePeep</RepositoryUrl>
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
<SignAssembly>true</SignAssembly>
<LangVersion>8.0</LangVersion>
<Copyright>Copyright © 2002-2021 Bill Menees</Copyright>
<LangVersion>latest</LangVersion>
<Copyright>Copyright © 2020-$([System.DateTime]::UtcNow.ToString(`yyyy`)) Bill Menees</Copyright>

<!-- These paths must be relative to the projects that include the property (not to this .props file). -->
<CodeAnalysisRuleSet>../WirePeep.ruleset</CodeAnalysisRuleSet>
<AssemblyOriginatorKeyFile>../WirePeep.snk</AssemblyOriginatorKeyFile>
<!-- When TargetFrameworks changes, also update the app's Properties\PublishProfiles\*.pubxml files. -->
<TargetFrameworks>net48;net6.0-windows</TargetFrameworks>

<RepoSrcFolder>$(MSBuildThisFileDirectory)</RepoSrcFolder>
<CodeAnalysisRuleSet>$(RepoSrcFolder)WirePeep.ruleset</CodeAnalysisRuleSet>
<AssemblyOriginatorKeyFile>$(RepoSrcFolder)WirePeep.snk</AssemblyOriginatorKeyFile>
<UseCustomCodeAnalyzers>true</UseCustomCodeAnalyzers>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<ProduceReferenceAssembly>false</ProduceReferenceAssembly>

<!-- Make the assembly, file, and NuGet package versions the same. -->
<Version>1.0.4</Version>
<Version>1.1.0</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<DefineConstants>DEBUG;TRACE</DefineConstants>
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>

<!-- Only set the BuildTime's date in Debug builds, so it doesn't force a rebuild every time.-->
<!-- https://stackoverflow.com/a/6656553/1882616 -->
<BuildTime Condition="'$(BuildTime)' == ''">$([System.DateTime]::UtcNow.ToString(`yyyy-MM-dd 00:00:00Z`))</BuildTime>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
<DefineConstants>TRACE</DefineConstants>

<!-- Set the full BuildTime in Release builds. It will cause a rebuild, but the AssemblyMetadata will be correct. -->
<BuildTime Condition="'$(BuildTime)' == ''">$([System.DateTime]::UtcNow.ToString(`yyyy-MM-dd HH:mm:ssZ`))</BuildTime>
</PropertyGroup>

<ItemGroup>
<AssemblyMetadata Condition="'$(BuildTime)' != ''" Include="BuildTime" Value="$(BuildTime)" />
</ItemGroup>

<ItemGroup Condition="$(UseCustomCodeAnalyzers)">
<PackageReference Include="codecracker.CSharp" Version="1.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Menees.Analyzers" Version="2.0.5">
<PackageReference Include="Menees.Analyzers" Version="3.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.1">
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.376">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
10 changes: 5 additions & 5 deletions src/WirePeep.Common/CommonOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ public sealed class CommonOptions
#region Private Data Members

private LogFileNameFormat logFileNameFormat = LogFileNameFormat.UtcNow;
private string logFolder;
private string? logFolder;

#endregion

#region Constructors

public CommonOptions(ISettingsNode settingsNode)
public CommonOptions(ISettingsNode? settingsNode)
{
if (settingsNode != null)
{
Expand Down Expand Up @@ -52,7 +52,7 @@ public LogFileNameFormat LogFileNameFormat
}
}

public string LogFolder
public string? LogFolder
{
get
{
Expand All @@ -76,9 +76,9 @@ public void Save(ISettingsNode settingsNode)
settingsNode.SetValue(nameof(this.LogFolder), this.logFolder);
}

public string GetFullLogFileName(DateTime utcNow)
public string? GetFullLogFileName(DateTime utcNow)
{
string result = null;
string? result = null;

if (Directory.Exists(this.LogFolder))
{
Expand Down
14 changes: 7 additions & 7 deletions src/WirePeep.Common/Location.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,19 @@ public Location(PeerGroup peerGroup, string name, IPAddress address, Guid? id =

#region Internal Methods

internal static Location TryLoad(ISettingsNode settingsNode, IReadOnlyDictionary<Guid, PeerGroup> idToGroupMap)
internal static Location? TryLoad(ISettingsNode? settingsNode, IReadOnlyDictionary<Guid, PeerGroup> idToGroupMap)
{
Location result = null;
Location? result = null;

if (settingsNode != null && Guid.TryParse(settingsNode.NodeName, out Guid id))
{
Guid peerGroupId = settingsNode.GetValue("PeerGroupId", Guid.Empty);
string name = settingsNode.GetValue(nameof(Name), null);
string addressText = settingsNode.GetValue(nameof(Address), null);
string? name = settingsNode.GetValueN(nameof(Name), null);
string? addressText = settingsNode.GetValueN(nameof(Address), null);

if (IPAddress.TryParse(addressText, out IPAddress address)
&& !string.IsNullOrEmpty(name)
&& idToGroupMap.TryGetValue(peerGroupId, out PeerGroup peerGroup))
if (IPAddress.TryParse(addressText, out IPAddress? address)
&& name.IsNotEmpty()
&& idToGroupMap.TryGetValue(peerGroupId, out PeerGroup? peerGroup))
{
result = new Location(peerGroup, name, address, id);
}
Expand Down
2 changes: 1 addition & 1 deletion src/WirePeep.Common/LocationState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ internal LocationState(Location location)
{
ConnectionState priorConnection = this.Connection;

using (Pinger pinger = new Pinger(peerGroup.Wait))
using (Pinger pinger = new(peerGroup.Wait))
{
TimeSpan roundtripTime = TimeSpan.Zero;
if (simulateConnection != null)
Expand Down
14 changes: 7 additions & 7 deletions src/WirePeep.Common/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ public sealed class Logger
private const string LengthColumn = "Length";
private const string PeerGroupColumn = nameof(PeerGroup);

private readonly HashSet<Entry> headers = new HashSet<Entry>();
private StreamWriter writer;
private readonly HashSet<Entry> headers = new();
private StreamWriter? writer;
private int batchDepth;

#endregion

#region Constructors

public Logger(string fileName, bool isSimple)
public Logger(string? fileName, bool isSimple)
{
this.FileName = fileName;
this.FileName = fileName ?? string.Empty;
this.IsSimple = isSimple;
}

Expand Down Expand Up @@ -69,7 +69,7 @@ public void AddSimpleEntry(string peerGroupName, DateTime startedUtc, DateTime?
{
this.TryAddHeader(
Entry.Simple, PeerGroupColumn, "LocalStart", LengthColumn, "LocalEnd", SincePreviousColumn, CommentColumn, "UtcStart", "UtcEnd");
TimeSpan? length = endedUtc != null ? ConvertUtility.RoundToSeconds(endedUtc.Value - startedUtc) : (TimeSpan?)null;
TimeSpan? length = endedUtc != null ? ConvertUtility.RoundToSeconds(endedUtc.Value - startedUtc) : null;
this.AddValues(peerGroupName, startedUtc.ToLocalTime(), length, endedUtc?.ToLocalTime(), sincePrevious, comment, startedUtc, endedUtc);
}
}
Expand Down Expand Up @@ -166,7 +166,7 @@ private void TryAddHeader(Entry entry, params string[] headerValues)
{
if (!this.IsSimple)
{
List<string> values = new List<string>(headerValues.Length);
List<string> values = new(headerValues.Length);
values.Add("//" + entry);
values.AddRange(headerValues);
headerValues = values.ToArray();
Expand All @@ -176,7 +176,7 @@ private void TryAddHeader(Entry entry, params string[] headerValues)
}
}

private void AddValues(params object[] values)
private void AddValues(params object?[] values)
{
// this.writer can be null if no log file name was provided (e.g., no log folder is configured).
if (this.writer != null)
Expand Down
11 changes: 4 additions & 7 deletions src/WirePeep.Common/PeerGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,17 @@ public PeerGroup(string name, TimeSpan fail, TimeSpan poll, TimeSpan wait, Guid?

#region Internal Methods

internal static PeerGroup TryLoad(ISettingsNode settingsNode)
internal static PeerGroup? TryLoad(ISettingsNode? settingsNode)
{
PeerGroup result = null;
PeerGroup? result = null;

if (settingsNode != null && Guid.TryParse(settingsNode.NodeName, out Guid id))
{
#pragma warning disable MEN010 // Avoid magic numbers. Default values are clear in context.
TimeSpan fail = settingsNode.GetValue(nameof(Fail), TimeSpan.FromSeconds(10));
TimeSpan poll = settingsNode.GetValue(nameof(Poll), TimeSpan.FromSeconds(5));
TimeSpan wait = settingsNode.GetValue(nameof(Wait), TimeSpan.FromMilliseconds(200));
#pragma warning restore MEN010 // Avoid magic numbers

string name = settingsNode.GetValue(nameof(Name), null);
if (!string.IsNullOrEmpty(name))
string? name = settingsNode.GetValueN(nameof(Name), null);
if (name.IsNotEmpty())
{
result = new PeerGroup(name, fail, poll, wait, id);
}
Expand Down
24 changes: 12 additions & 12 deletions src/WirePeep.Common/Pinger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public sealed class Pinger : IDisposable

private readonly Ping ping;
private readonly int waitMilliseconds;
private readonly PingOptions options;
private readonly PingOptions? options;

#endregion

Expand All @@ -42,13 +42,13 @@ private Pinger(TimeSpan wait, int ttl)

#region Public Methods

public static IPAddress GetAddressAtTtl(IPAddress searchAddress, int ttl, TimeSpan wait)
public static IPAddress? GetAddressAtTtl(IPAddress searchAddress, int ttl, TimeSpan wait)
{
IPAddress result = null;
IPAddress? result = null;

using (Pinger pinger = new Pinger(wait, ttl))
using (Pinger pinger = new(wait, ttl))
{
PingReply reply = pinger.TrySend(searchAddress);
PingReply? reply = pinger.TrySend(searchAddress);
if (reply?.Status == IPStatus.Success || reply?.Status == IPStatus.TtlExpired)
{
result = reply?.Address;
Expand All @@ -60,16 +60,16 @@ public static IPAddress GetAddressAtTtl(IPAddress searchAddress, int ttl, TimeSp

public bool? TryPing(IPAddress address)
{
PingReply reply = this.TrySend(address);
bool? result = reply != null ? reply.Status == IPStatus.Success : (bool?)null;
PingReply? reply = this.TrySend(address);
bool? result = reply != null ? reply.Status == IPStatus.Success : null;
return result;
}

public bool? TryPing(IPAddress address, out TimeSpan roundtripTime)
{
PingReply reply = this.TrySend(address);
PingReply? reply = this.TrySend(address);
roundtripTime = TimeSpan.FromMilliseconds(reply?.RoundtripTime ?? 0);
bool? result = reply != null ? reply.Status == IPStatus.Success : (bool?)null;
bool? result = reply != null ? reply.Status == IPStatus.Success : null;
return result;
}

Expand All @@ -82,9 +82,9 @@ public void Dispose()

#region Private Methods

private PingReply TrySend(IPAddress address)
private PingReply? TrySend(IPAddress address)
{
PingReply result = null;
PingReply? result = null;
try
{
result = this.ping.Send(address, this.waitMilliseconds, BufferContent, this.options);
Expand All @@ -103,7 +103,7 @@ private PingReply TrySend(IPAddress address)
// Technically, we could call NetworkInterface.GetIsNetworkAvailable() to check if
// Windows networking is available yet. But that polls all adapters to see if any are up.
// For efficiency, we might as well just use the exception state to indicate "Unavailable".
IDictionary<string, object> context = null;
IDictionary<string, object>? context = null;
if (ex.InnerException is Win32Exception win32)
{
context = new Dictionary<string, object> { { "Win32 Error", win32.NativeErrorCode } };
Expand Down
Loading

0 comments on commit 5cf68c5

Please sign in to comment.