new ln.types.net.Endpoint, IPv4 -> IPv6 implicit operator

pull/2/head
Harald Wolff 2019-10-22 19:41:44 +02:00
parent 8857b30487
commit 41a1ccfba3
3 changed files with 60 additions and 8 deletions

View File

@ -127,6 +127,7 @@
<Compile Include="collections\WeakKeyReferenceDictionary.cs" />
<Compile Include="odb\values\ODBByteBuffer.cs" />
<Compile Include="odb\ng\IdentityCache.cs" />
<Compile Include="net\Endpoint.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="odb\" />

56
net/Endpoint.cs 100644
View File

@ -0,0 +1,56 @@
// /**
// * 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);
}
}

View File

@ -110,14 +110,9 @@ namespace ln.types.net
}
public static implicit operator IPAddress(IPv4 ip)
{
return new IPAddress(ip.IPBytes);
}
public static implicit operator IPv4(IPAddress ip)
{
return new IPv4(ip.GetAddressBytes());
}
public static implicit operator IPAddress(IPv4 ip) => new IPAddress(ip.IPBytes);
public static implicit operator IPv4(IPAddress ip) => new IPv4(ip.GetAddressBytes());
public static implicit operator IPv6(IPv4 ip) => new IPv6(ip.IPBytes);
}
}