Created
November 6, 2020 20:41
-
-
Save aarani/3f1588d594a05e7f4f0c4d4b27b573a9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| From 1fd2c9630aeec2abf571255e59716edc44edb101 Mon Sep 17 00:00:00 2001 | |
| From: Afshin Arani <[email protected]> | |
| Date: Sat, 7 Nov 2020 00:08:18 +0330 | |
| Subject: [PATCH] add an overload to throw exceptions instead of returning | |
| errors | |
| --- | |
| STUN/STUNClient.cs | 75 +++++++++++++++++++++++++++++++++++++++++-- | |
| STUN/STUNException.cs | 12 +++++++ | |
| 2 files changed, 84 insertions(+), 3 deletions(-) | |
| create mode 100644 STUN/STUNException.cs | |
| diff --git a/STUN/STUNClient.cs b/STUN/STUNClient.cs | |
| index 280a2cc..2a4fcb2 100644 | |
| --- a/STUN/STUNClient.cs | |
| +++ b/STUN/STUNClient.cs | |
| @@ -22,6 +22,33 @@ namespace STUN | |
| /// </summary> | |
| public static int ReceiveTimeout = 5000; | |
| + /// <param name="server">Server address</param> | |
| + /// <param name="queryType">Query type</param> | |
| + /// <param name="closeSocket"> | |
| + /// Set to true if created socket should closed after the query | |
| + /// else <see cref="STUNQueryResult.Socket"/> will leave open and can be used. | |
| + /// </param> | |
| + public static STUNQueryResult Query(IPEndPoint server, STUNQueryType queryType, bool closeSocket, | |
| + NATTypeDetectionRFC natTypeDetectionRfc = NATTypeDetectionRFC.Rfc3489) | |
| + { | |
| + var result = QueryInternal(server, queryType, closeSocket, natTypeDetectionRfc); | |
| + if (result.QueryError != STUNQueryError.Success) | |
| + throw new STUNException(result.QueryError, result.ServerError, result.ServerErrorPhrase); | |
| + return result; | |
| + } | |
| + | |
| + /// <param name="socket">A UDP <see cref="Socket"/> that will use for query. You can also use <see cref="UdpClient.Client"/></param> | |
| + /// <param name="server">Server address</param> | |
| + /// <param name="queryType">Query type</param> | |
| + public static STUNQueryResult Query(Socket socket, IPEndPoint server, STUNQueryType queryType, | |
| + NATTypeDetectionRFC natTypeDetectionRfc) | |
| + { | |
| + var result = QueryInternal(socket, server, queryType, natTypeDetectionRfc); | |
| + if (result.QueryError != STUNQueryError.Success) | |
| + throw new STUNException(result.QueryError, result.ServerError, result.ServerErrorPhrase); | |
| + return result; | |
| + } | |
| + | |
| /// <param name="server">Server address</param> | |
| /// <param name="queryType">Query type</param> | |
| /// <param name="closeSocket"> | |
| @@ -48,14 +75,56 @@ namespace STUN | |
| /// Set to true if created socket should closed after the query | |
| /// else <see cref="STUNQueryResult.Socket"/> will leave open and can be used. | |
| /// </param> | |
| - public static STUNQueryResult Query(IPEndPoint server, STUNQueryType queryType, bool closeSocket, | |
| + public static STUNQueryResult TryQuery(IPEndPoint server, STUNQueryType queryType, bool closeSocket, | |
| + NATTypeDetectionRFC natTypeDetectionRfc = NATTypeDetectionRFC.Rfc3489) | |
| + { | |
| + return QueryInternal(server, queryType, closeSocket, natTypeDetectionRfc); | |
| + } | |
| + | |
| + /// <param name="socket">A UDP <see cref="Socket"/> that will use for query. You can also use <see cref="UdpClient.Client"/></param> | |
| + /// <param name="server">Server address</param> | |
| + /// <param name="queryType">Query type</param> | |
| + public static STUNQueryResult TryQuery(Socket socket, IPEndPoint server, STUNQueryType queryType, | |
| + NATTypeDetectionRFC natTypeDetectionRfc) | |
| + { | |
| + return QueryInternal(socket, server, queryType, natTypeDetectionRfc); | |
| + } | |
| + | |
| + /// <param name="server">Server address</param> | |
| + /// <param name="queryType">Query type</param> | |
| + /// <param name="closeSocket"> | |
| + /// Set to true if created socket should closed after the query | |
| + /// else <see cref="STUNQueryResult.Socket"/> will leave open and can be used. | |
| + /// </param> | |
| + public static Task<STUNQueryResult> TryQueryAsync(IPEndPoint server, STUNQueryType queryType, bool closeSocket) | |
| + { | |
| + return Task.Run(() => TryQuery(server, queryType, closeSocket)); | |
| + } | |
| + | |
| + /// <param name="socket">A UDP <see cref="Socket"/> that will use for query. You can also use <see cref="UdpClient.Client"/></param> | |
| + /// <param name="server">Server address</param> | |
| + /// <param name="queryType">Query type</param> | |
| + public static Task<STUNQueryResult> TryQueryAsync(Socket socket, IPEndPoint server, STUNQueryType queryType, | |
| + NATTypeDetectionRFC natTypeDetectionRfc) | |
| + { | |
| + return Task.Run(() => TryQuery(socket, server, queryType, natTypeDetectionRfc)); | |
| + } | |
| + | |
| + | |
| + /// <param name="server">Server address</param> | |
| + /// <param name="queryType">Query type</param> | |
| + /// <param name="closeSocket"> | |
| + /// Set to true if created socket should closed after the query | |
| + /// else <see cref="STUNQueryResult.Socket"/> will leave open and can be used. | |
| + /// </param> | |
| + private static STUNQueryResult QueryInternal(IPEndPoint server, STUNQueryType queryType, bool closeSocket, | |
| NATTypeDetectionRFC natTypeDetectionRfc = NATTypeDetectionRFC.Rfc3489) | |
| { | |
| Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); | |
| IPEndPoint bindEndPoint = new IPEndPoint(IPAddress.Any, 0); | |
| socket.Bind(bindEndPoint); | |
| - var result = Query(socket, server, queryType, natTypeDetectionRfc); | |
| + var result = QueryInternal(socket, server, queryType, natTypeDetectionRfc); | |
| if (closeSocket) | |
| { | |
| @@ -70,7 +139,7 @@ namespace STUN | |
| /// <param name="server">Server address</param> | |
| /// <param name="queryType">Query type</param> | |
| /// <param name="natTypeDetectionRfc">Rfc algorithm type</param> | |
| - public static STUNQueryResult Query(Socket socket, IPEndPoint server, STUNQueryType queryType, | |
| + private static STUNQueryResult QueryInternal(Socket socket, IPEndPoint server, STUNQueryType queryType, | |
| NATTypeDetectionRFC natTypeDetectionRfc) | |
| { | |
| if (natTypeDetectionRfc == NATTypeDetectionRFC.Rfc3489) | |
| diff --git a/STUN/STUNException.cs b/STUN/STUNException.cs | |
| new file mode 100644 | |
| index 0000000..b3810c4 | |
| --- /dev/null | |
| +++ b/STUN/STUNException.cs | |
| @@ -0,0 +1,12 @@ | |
| +using System; | |
| + | |
| +namespace STUN | |
| +{ | |
| + public class STUNException : Exception | |
| + { | |
| + public STUNException(STUNQueryError queryError, STUNErrorCodes serverError, string serverErrorPhrase) | |
| + : base($"Stun Erorr: Error {queryError} {serverError} {serverErrorPhrase}") | |
| + { | |
| + } | |
| + } | |
| +} | |
| \ No newline at end of file | |
| -- | |
| 2.17.1.windows.2 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment