network-resurrector/NetworkResurrector.WakeOnLa.../WolClient.cs

46 lines
1.3 KiB
C#

using Microsoft.Extensions.Logging;
using System;
using System.Net.Sockets;
namespace NetworkResurrector.WakeOnLan.Inhouse
{
internal class WolClient : UdpClient
{
private readonly ILogger<WolClient> _logger;
/// <summary>
/// Initializes a new instance of <see cref="WolClient"/>.
/// </summary>
public WolClient(ILogger<WolClient> logger) : base()
{
_logger = logger;
}
/// <summary>
/// Sets up the UDP client to broadcast packets.
/// </summary>
/// <returns><see langword="true"/> if the UDP client is set in
/// broadcast mode.</returns>
public bool SetClientInBrodcastMode()
{
bool broadcast = false;
if (this.Active)
{
try
{
this.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 0);
_logger.LogInformation("WolClient => SetClientInBrodcastMode succedded.");
broadcast = true;
}
catch (Exception ex)
{
broadcast = false;
_logger.LogError("WolClient => SetClientInBrodcastMode failed.", ex);
}
}
return broadcast;
}
}
}