ln.types/net/Endpoint.cs

57 lines
1.5 KiB
C#

// /**
// * File: Endpoint.cs
// * Author: haraldwolff
// *
// * This file and it's content is copyrighted by the Author and / or copyright holder.
// * Any use wihtout proper permission is illegal and may lead to legal actions.
// *
// *
// **/
using System;
using System.Net;
using ln.types.odb.values;
namespace ln.types.net
{
public class Endpoint
{
public IPv6 Address { get; }
public int Port { get; }
private Endpoint() { }
public Endpoint(IPv6 address, int port)
{
Address = address;
Port = port;
}
public Endpoint(System.Net.EndPoint netEndpoint)
{
if (netEndpoint is IPEndPoint ipep)
{
Port = ipep.Port;
Address = ipep.Address;
return;
}
throw new ArgumentOutOfRangeException(nameof(netEndpoint));
}
public override int GetHashCode()
{
return Address.GetHashCode() ^ Port;
}
public override bool Equals(object obj)
{
if (obj is Endpoint)
{
Endpoint you = obj as Endpoint;
return Address.Equals(you.Address) && Port.Equals(you.Port);
}
return false;
}
public static implicit operator Endpoint(EndPoint endPoint) => new Endpoint(endPoint);
public static implicit operator EndPoint(Endpoint endPoint) => new IPEndPoint(endPoint.Address, endPoint.Port);
}
}