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