You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by aa...@apache.org on 2023/02/23 15:15:02 UTC

[rocketmq-clients] 22/28: Polish code

This is an automated email from the ASF dual-hosted git repository.

aaronai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-clients.git

commit b2ee3b311306d08a5e11c72642bb43db92463beb
Author: Aaron Ai <ya...@gmail.com>
AuthorDate: Tue Feb 21 14:27:01 2023 +0800

    Polish code
---
 csharp/rocketmq-client-csharp/Endpoints.cs | 53 +++++++++++++++++++++++-------
 csharp/tests/EndpointsTest.cs              | 18 ++++++++++
 2 files changed, 59 insertions(+), 12 deletions(-)

diff --git a/csharp/rocketmq-client-csharp/Endpoints.cs b/csharp/rocketmq-client-csharp/Endpoints.cs
index 54d8f0d2..27130a33 100644
--- a/csharp/rocketmq-client-csharp/Endpoints.cs
+++ b/csharp/rocketmq-client-csharp/Endpoints.cs
@@ -24,6 +24,10 @@ namespace Org.Apache.Rocketmq
 {
     public class Endpoints : IEquatable<Endpoints>
     {
+        private const string HttpPrefix = "http://";
+        private const string HttpsPrefix = "https://";
+        private const int DefaultPort = 80;
+
         private static readonly AddressListEqualityComparer AddressListComparer = new();
         private const string EndpointSeparator = ":";
         private List<Address> Addresses { get; }
@@ -72,16 +76,43 @@ namespace Org.Apache.Rocketmq
             }
         }
 
+        // TODO: Multiple addresses has not been supported yet.
         public Endpoints(string endpoints)
         {
-            // TODO
-            var strs = endpoints.Split(EndpointSeparator);
+            if (endpoints.StartsWith(HttpPrefix))
+            {
+                endpoints = endpoints[HttpPrefix.Length..];
+            }
+
+            if (endpoints.StartsWith(HttpsPrefix))
+            {
+                endpoints = endpoints[HttpsPrefix.Length..];
+            }
+
+            var index = endpoints.IndexOf(EndpointSeparator, StringComparison.Ordinal);
+            var port = index > 0 ? int.Parse(endpoints[(1 + index)..]) : DefaultPort;
+            var host = index > 0 ? endpoints.Substring(0, index) : endpoints;
+
+            var uriHostNameType = Uri.CheckHostName(host);
             Scheme = AddressScheme.DomainName;
-            string host = strs[0];
-            int port = int.Parse(strs[1]);
-            Address address = new Address(host, port);
-            var addresses = new List<Address>();
-            addresses.Add(address);
+            switch (uriHostNameType)
+            {
+                case UriHostNameType.IPv4:
+                    Scheme = AddressScheme.Ipv4;
+                    break;
+                case UriHostNameType.IPv6:
+                    Scheme = AddressScheme.Ipv6;
+                    break;
+                case UriHostNameType.Dns:
+                case UriHostNameType.Basic:
+                case UriHostNameType.Unknown:
+                default:
+                    Scheme = AddressScheme.DomainName;
+                    break;
+            }
+
+            var address = new Address(host, port);
+            var addresses = new List<Address> { address };
             Addresses = addresses;
         }
 
@@ -90,22 +121,20 @@ namespace Org.Apache.Rocketmq
             return GrpcTarget;
         }
 
+        // TODO: Support non-TLS and multiple addresses.
         public string GrpcTarget
         {
-            // TODO
             get
             {
                 foreach (var address in Addresses)
                 {
-                    var target = "https://" + address.Host + ":" + address.Port;
-                    // Console.WriteLine(target);
-                    return "https://" + address.Host + ":" + address.Port;
+                    return HttpsPrefix + address.Host + EndpointSeparator + address.Port;
                 }
 
                 return "";
             }
         }
-        
+
         public bool Equals(Endpoints other)
         {
             if (ReferenceEquals(null, other))
diff --git a/csharp/tests/EndpointsTest.cs b/csharp/tests/EndpointsTest.cs
new file mode 100644
index 00000000..e68274b8
--- /dev/null
+++ b/csharp/tests/EndpointsTest.cs
@@ -0,0 +1,18 @@
+using System;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using Org.Apache.Rocketmq;
+
+namespace tests
+{
+    [TestClass]
+    public class EndpointsTest
+    {
+        [TestMethod]
+        public void testConstructor()
+        {
+            Console.WriteLine(Uri.CheckHostName("127.0.0.1"));
+            Console.WriteLine(Uri.CheckHostName("1050:0000:0000:0000:0005:0600:300c:326b"));
+            Console.WriteLine(Uri.CheckHostName("baidu.com"));
+        }
+    }
+}
\ No newline at end of file