ZeroTierOne/windows/WinUI/ZeroTierNetwork.cs

175 lines
5.5 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
2015-10-23 22:36:42 +00:00
using Newtonsoft.Json;
namespace WinUI
{
[Serializable]
public class ZeroTierNetwork : ISerializable, IEquatable<ZeroTierNetwork>, IComparable<ZeroTierNetwork>
{
protected ZeroTierNetwork(SerializationInfo info, StreamingContext ctx)
{
try
{
NetworkId = info.GetString("nwid");
MacAddress = info.GetString("mac");
NetworkName = info.GetString("name");
NetworkStatus = info.GetString("status");
NetworkType = info.GetString("type");
MTU = info.GetInt32("mtu");
DHCP = info.GetBoolean("dhcp");
Bridge = info.GetBoolean("bridge");
BroadcastEnabled = info.GetBoolean("broadcastEnabled");
PortError = info.GetInt32("portError");
NetconfRevision = info.GetInt32("netconfRevision");
AssignedAddresses = (string[])info.GetValue("assignedAddresses", typeof(string[]));
Routes = (NetworkRoute[])info.GetValue("routes", typeof(NetworkRoute[]));
DeviceName = info.GetString("portDeviceName");
AllowManaged = info.GetBoolean("allowManaged");
AllowGlobal = info.GetBoolean("allowGlobal");
AllowDefault = info.GetBoolean("allowDefault");
}
catch { }
IsConnected = false;
}
public virtual void GetObjectData(SerializationInfo info, StreamingContext ctx)
{
info.AddValue("nwid", NetworkId);
info.AddValue("mac", MacAddress);
info.AddValue("name", NetworkName);
info.AddValue("status", NetworkStatus);
info.AddValue("type", NetworkType);
info.AddValue("mtu", MTU);
info.AddValue("dhcp", DHCP);
info.AddValue("bridge", Bridge);
info.AddValue("broadcastEnabled", BroadcastEnabled);
info.AddValue("portError", PortError);
info.AddValue("netconfRevision", NetconfRevision);
info.AddValue("assignedAddresses", AssignedAddresses);
info.AddValue("routes", Routes);
info.AddValue("portDeviceName", DeviceName);
info.AddValue("allowManaged", AllowManaged);
info.AddValue("allowGlobal", AllowGlobal);
info.AddValue("allowDefault", AllowDefault);
}
2015-10-23 22:36:42 +00:00
[JsonProperty("nwid")]
public string NetworkId { get; set; }
[JsonProperty("mac")]
public string MacAddress { get; set; }
[JsonProperty("name")]
public string NetworkName { get; set; }
[JsonProperty("status")]
public string NetworkStatus { get; set; }
[JsonProperty("type")]
public string NetworkType { get; set; }
[JsonProperty("mtu")]
public int MTU { get; set; }
[JsonProperty("dhcp")]
public bool DHCP { get; set; }
[JsonProperty("bridge")]
public bool Bridge { get; set ; }
[JsonProperty("broadcastEnabled")]
public bool BroadcastEnabled { get ; set; }
[JsonProperty("portError")]
public int PortError { get; set; }
[JsonProperty("netconfRevision")]
public int NetconfRevision { get; set; }
[JsonProperty("assignedAddresses")]
public string[] AssignedAddresses { get; set; }
2016-11-04 19:39:57 +00:00
[JsonProperty("routes")]
public NetworkRoute[] Routes { get; set; }
2015-10-23 22:36:42 +00:00
[JsonProperty("portDeviceName")]
public string DeviceName { get; set; }
2016-11-04 19:39:57 +00:00
[JsonProperty("allowManaged")]
public bool AllowManaged { get; set; }
[JsonProperty("allowGlobal")]
public bool AllowGlobal { get; set; }
[JsonProperty("allowDefault")]
public bool AllowDefault { get; set; }
public bool IsConnected { get; set; } = false;
public String Title
{
get
{
if (NetworkName != null && NetworkName.Length > 0)
{
return NetworkId + " (" + NetworkName + ")";
}
else
{
return NetworkId;
}
}
}
public bool Equals(ZeroTierNetwork network)
{
if (NetworkId == null || network == null)
return false;
return NetworkId.Equals(network.NetworkId);
}
public int CompareTo(ZeroTierNetwork network)
{
if (NetworkId == null || network == null)
return -1;
UInt64 thisNwid = UInt64.Parse(NetworkId, System.Globalization.NumberStyles.HexNumber);
UInt64 otherNwid = UInt64.Parse(network.NetworkId, System.Globalization.NumberStyles.HexNumber);
if (thisNwid > otherNwid)
{
return 1;
}
else if (thisNwid < otherNwid)
{
return -1;
}
else
{
return 0;
}
}
}
2016-11-14 22:56:36 +00:00
public class NetworkEqualityComparer : IEqualityComparer<ZeroTierNetwork>
{
public bool Equals(ZeroTierNetwork lhs, ZeroTierNetwork rhs)
{
return lhs.NetworkId.Equals(rhs.NetworkId);
}
public int GetHashCode(ZeroTierNetwork obj)
{
return obj.NetworkId.GetHashCode();
}
}
}