From 90e4552415861afd15f7363175fd07b6510ee44d Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Sat, 16 Nov 2019 02:04:47 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20EndPointExtensions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GetHost() returns the hostname of the endpoint which will either be the value of DnsEndPoint.Host or IPEndPoint.Address GetPort() returns the port of the endpoint. --- X10D/EndPointExtensions.cs | 42 ++++++++++++++++++++++++++++++++++++++ X10D/X10D.csproj | 1 + 2 files changed, 43 insertions(+) create mode 100644 X10D/EndPointExtensions.cs diff --git a/X10D/EndPointExtensions.cs b/X10D/EndPointExtensions.cs new file mode 100644 index 0000000..99017ba --- /dev/null +++ b/X10D/EndPointExtensions.cs @@ -0,0 +1,42 @@ +namespace X10D +{ + #region Using Directives + + using System; + using System.Net; + + #endregion + + /// + /// A set of extension methods for and derived types. + /// + public static class EndPointExtensions + { + /// + /// Gets the endpoint hostname. + /// + /// The endpoint whose hostname to get. + /// Returns a representing the hostname, which may be an IP or a DNS, or empty + /// string on failure. + public static string GetHost(this EndPoint endPoint) => + endPoint switch + { + IPEndPoint ip => ip.Address.ToString(), + DnsEndPoint dns => dns.Host, + _ => String.Empty + }; + + /// + /// Gets the endpoint port. + /// + /// The endpoint whose port to get. + /// Returns an representing the port, or 0 on failure. + public static int GetPort(this EndPoint endPoint) => + endPoint switch + { + IPEndPoint ip => ip.Port, + DnsEndPoint dns => dns.Port, + _ => 0 + }; + } +} diff --git a/X10D/X10D.csproj b/X10D/X10D.csproj index 841cc56..4c89f47 100644 --- a/X10D/X10D.csproj +++ b/X10D/X10D.csproj @@ -50,6 +50,7 @@ +