Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1 from imerzan/net8
Browse files Browse the repository at this point in the history
Upgrade to .NET8 / AOT
  • Loading branch information
imerzan authored Nov 17, 2023
2 parents cd5dbbb + f0ece11 commit 7954b76
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 33 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# WOLtool
.NET Core Wake-On-Lan console utility.
.NET8 AOT Wake-On-Lan console utility.

## Instructions:

1) Make sure the remote computer has Wake-On-Lan enabled in the System BIOS, and the remote network adapter supports WOL and has it enabled.
2) Startup WOLtool, provide MAC Address of the remote computer's network card.
- Be sure you have the [.NET6](https://dotnet.microsoft.com/download) or newer runtime installed.
- *(Optional)* You can run WOLtool from the command line: ```woltool <MacAddress1> <MacAddress2> <MacAddress3>...``` Multiple MAC Addresses are supported.
3) If Magic Packet broadcast is successful, you will receive a 'OK' message. As long as the remote computer is properly configured, it should wake ([See Considerations](https://github.com/imerzan/WOLtool#considerations)).

Expand Down
3 changes: 2 additions & 1 deletion WOLtool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<DebugType>none</DebugType>
<PublishAot>true</PublishAot>
</PropertyGroup>

</Project>
5 changes: 3 additions & 2 deletions src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ static int Main(string[] args)
while (true) // Continue prompting user until no more input is received
{
Console.Write("Enter MAC Address: ");
string mac = Console.ReadLine().Trim();
if (mac == String.Empty) break; // User is done, exit
string mac = Console.ReadLine()?.Trim();
if (string.IsNullOrEmpty(mac))
break; // User is done, exit
try
{
wol.Send(mac);
Expand Down
36 changes: 8 additions & 28 deletions src/WakeOnLan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace WOLtool
{
public class WakeOnLan : IDisposable
{
private readonly Socket _sock;
private static readonly IPEndPoint[] _endpoints = new IPEndPoint[]
private static readonly IReadOnlyList<IPEndPoint> _endpoints = new List<IPEndPoint>
{
new IPEndPoint(IPAddress.Broadcast, 7), // echo
new IPEndPoint(IPAddress.Broadcast, 9) // discard
Expand All @@ -36,7 +37,7 @@ public void Send(string macAddress)
}
catch (Exception ex)
{
throw new WakeOnLanException("ERROR broadcasting WakeOnLan Magic Packet.", ex);
throw new InvalidOperationException("ERROR broadcasting WakeOnLan Magic Packet.", ex);
}
}
public void Send(PhysicalAddress macAddress)
Expand All @@ -51,7 +52,7 @@ public void Send(PhysicalAddress macAddress)
}
catch (Exception ex)
{
throw new WakeOnLanException("ERROR broadcasting WakeOnLan Magic Packet.", ex);
throw new InvalidOperationException("ERROR broadcasting WakeOnLan Magic Packet.", ex);
}
}

Expand All @@ -68,7 +69,7 @@ public async Task SendAsync(string macAddress)
}
catch (Exception ex)
{
throw new WakeOnLanException("ERROR broadcasting WakeOnLan Magic Packet.", ex);
throw new InvalidOperationException("ERROR broadcasting WakeOnLan Magic Packet.", ex);
}
}
public async Task SendAsync(PhysicalAddress macAddress)
Expand All @@ -83,7 +84,7 @@ public async Task SendAsync(PhysicalAddress macAddress)
}
catch (Exception ex)
{
throw new WakeOnLanException("ERROR broadcasting WakeOnLan Magic Packet.", ex);
throw new InvalidOperationException("ERROR broadcasting WakeOnLan Magic Packet.", ex);
}
}

Expand All @@ -102,42 +103,21 @@ private static byte[] BuildMagicPacket(PhysicalAddress macAddress)
return magicPacket; // 102 Byte Magic Packet
}

// Public implementation of Dispose pattern callable by consumers.
#region IDisposable
private bool _disposed = false;
public void Dispose() => Dispose(true);

// Protected implementation of Dispose pattern.
protected virtual void Dispose(bool disposing)
{
if (_disposed)
{
return;
}

if (disposing)
{
// Dispose managed state (managed objects).
_sock?.Dispose();
}

_disposed = true;
}
}

public class WakeOnLanException : Exception
{
public WakeOnLanException()
{
}

public WakeOnLanException(string message)
: base(message)
{
}

public WakeOnLanException(string message, Exception inner)
: base(message, inner)
{
}
#endregion
}
}

0 comments on commit 7954b76

Please sign in to comment.