70 lines
1.9 KiB
C#
70 lines
1.9 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 System.Net.Sockets;
|
|
|
|
namespace ln.type
|
|
{
|
|
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 Socket CreateSocket(SocketType socketType)
|
|
{
|
|
Socket socket = new Socket(
|
|
IPv6.V4Space.Contains(Address) ? AddressFamily.InterNetwork : AddressFamily.InterNetworkV6,
|
|
socketType,
|
|
ProtocolType.Unspecified
|
|
|
|
);
|
|
socket.Bind(this);
|
|
return socket;
|
|
}
|
|
|
|
public static implicit operator Endpoint(EndPoint endPoint) => new Endpoint(endPoint);
|
|
public static implicit operator EndPoint(Endpoint endPoint) => new IPEndPoint(endPoint.Address, endPoint.Port);
|
|
}
|
|
}
|