Vanara/System/Network/NetworkConnection.cs

71 lines
3.3 KiB
C#
Raw Normal View History

using System;
2018-04-05 16:17:33 -04:00
using System.Runtime.InteropServices;
2018-07-21 16:14:26 -04:00
using Vanara.PInvoke.NetListMgr;
namespace Vanara.Network
{
2018-04-05 16:17:33 -04:00
/// <summary>Represents a single network connection. Wraps <see cref="INetworkConnection"/>.</summary>
public class NetworkConnection : IDisposable
{
2018-04-05 16:17:33 -04:00
private INetworkConnection conn;
2018-07-21 16:14:26 -04:00
private INetworkConnectionCost cost;
internal NetworkConnection(INetworkConnection networkConnection)
{
2018-07-21 16:14:26 -04:00
conn = networkConnection ?? throw new ArgumentNullException(nameof(networkConnection));
}
2018-04-05 16:17:33 -04:00
/// <summary>Returns the ID of the network adapter used by this connection. There may multiple connections using the same adapter ID.</summary>
/// <returns>A GUID that specifies the adapter ID of the TCP/IP interface used by this network connection.</returns>
public Guid AdapterId => conn.GetAdapterId();
2018-04-05 16:17:33 -04:00
/// <summary>Returns the Connection ID associated with this network connection.</summary>
/// <returns>A GUID that specifies the Connection ID associated with this network connection.</returns>
public Guid ConnectionId => conn.GetConnectionId();
2018-04-05 16:17:33 -04:00
/// <summary>Returns the connectivity state of the network.</summary>
/// <returns>A NLM_CONNECTIVITY enumeration value that contains a bitmask that specifies the connectivity of this network connection.</returns>
public NLM_CONNECTIVITY Connectivity => conn.GetConnectivity();
2018-07-21 16:14:26 -04:00
/// <summary>Gets the network cost associated with a connection.</summary>
/// <value>The cost.</value>
public NLM_CONNECTION_COST Cost => (cost ?? (cost = (INetworkConnectionCost)conn)).GetCost();
/// <summary>Gets the data plan status.</summary>
/// <value>The data plan status.</value>
public NLM_DATAPLAN_STATUS DataPlanStatus => (cost ?? (cost = (INetworkConnectionCost)conn)).GetDataPlanStatus();
2018-04-05 16:17:33 -04:00
/// <summary>Returns the type of network connection.</summary>
/// <returns>An NLM_DOMAIN_TYPE enumeration value that specifies the domain type of the network.</returns>
public NLM_DOMAIN_TYPE DomainType => conn.GetDomainType();
2018-04-05 16:17:33 -04:00
/// <summary>Specifies if the associated network connection has any network connectivity.</summary>
/// <value>If TRUE, this network connection has connectivity; if FALSE, it does not.</value>
public bool IsConnected => conn.IsConnected;
2018-04-05 16:17:33 -04:00
/// <summary>Specifies if the associated network connection has internet connectivity.</summary>
/// <value>If TRUE, this network connection has connectivity to the internet; if FALSE, it does not.</value>
public bool IsConnectedToInternet => conn.IsConnectedToInternet;
2018-04-05 16:17:33 -04:00
2018-07-21 16:14:26 -04:00
/// <summary>Returns the associated network for the connection.</summary>
/// <returns>An instance that specifies the network profile associated with the connection.</returns>
public NetworkProfile Network => new NetworkProfile(conn.GetNetwork());
/// <summary>Returns a <see cref="string"/> that represents this instance.</summary>
/// <returns>A <see cref="string"/> that represents this instance.</returns>
public override string ToString() => $"{ConnectionId} : {Connectivity}";
2018-04-05 16:17:33 -04:00
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
void IDisposable.Dispose()
{
if (conn == null) return;
2018-07-21 16:14:26 -04:00
if (cost != null)
{
Marshal.FinalReleaseComObject(cost);
cost = null;
}
2018-04-05 16:17:33 -04:00
Marshal.FinalReleaseComObject(conn);
conn = null;
}
}
}