[Lldb-commits] [lldb] [lldb][NFC] Moved FindSchemeByProtocol() from Acceptor to Socket (PR #104439)

Dmitry Vasilyev via lldb-commits lldb-commits at lists.llvm.org
Thu Aug 15 07:07:02 PDT 2024


https://github.com/slydiman created https://github.com/llvm/llvm-project/pull/104439

This is the prerequisite for #104238.

>From e4dce57456f354c30e8a9f896354aeb12075d58b Mon Sep 17 00:00:00 2001
From: Dmitry Vasilyev <dvassiliev at accesssoftek.com>
Date: Thu, 15 Aug 2024 17:59:55 +0400
Subject: [PATCH] [lldb][NFC] Moved FindSchemeByProtocol() from Acceptor to
 Socket

This is the prerequisite for #104238.
---
 lldb/include/lldb/Host/Socket.h     |  4 +++
 lldb/source/Host/common/Socket.cpp  | 32 +++++++++++++++++++++++
 lldb/tools/lldb-server/Acceptor.cpp | 39 +++--------------------------
 3 files changed, 39 insertions(+), 36 deletions(-)

diff --git a/lldb/include/lldb/Host/Socket.h b/lldb/include/lldb/Host/Socket.h
index 4dad88e78faa11..573c881f727d8f 100644
--- a/lldb/include/lldb/Host/Socket.h
+++ b/lldb/include/lldb/Host/Socket.h
@@ -60,6 +60,10 @@ class Socket : public IOObject {
 
   ~Socket() override;
 
+  static const char *FindSchemeByProtocol(const SocketProtocol protocol);
+  static bool FindProtocolByScheme(const char *scheme,
+                                   SocketProtocol &protocol);
+
   static llvm::Error Initialize();
   static void Terminate();
 
diff --git a/lldb/source/Host/common/Socket.cpp b/lldb/source/Host/common/Socket.cpp
index f9911cf136cbd0..7364a12280cfdd 100644
--- a/lldb/source/Host/common/Socket.cpp
+++ b/lldb/source/Host/common/Socket.cpp
@@ -70,6 +70,38 @@ static bool IsInterrupted() {
 #endif
 }
 
+struct SocketScheme {
+  const char *m_scheme;
+  const Socket::SocketProtocol m_protocol;
+};
+
+static SocketScheme socket_schemes[] = {
+    {"tcp", Socket::ProtocolTcp},
+    {"udp", Socket::ProtocolUdp},
+    {"unix", Socket::ProtocolUnixDomain},
+    {"unix-abstract", Socket::ProtocolUnixAbstract},
+};
+
+const char *
+Socket::FindSchemeByProtocol(const Socket::SocketProtocol protocol) {
+  for (auto s : socket_schemes) {
+    if (s.m_protocol == protocol)
+      return s.m_scheme;
+  }
+  return nullptr;
+}
+
+bool Socket::FindProtocolByScheme(const char *scheme,
+                                  Socket::SocketProtocol &protocol) {
+  for (auto s : socket_schemes) {
+    if (!strcmp(s.m_scheme, scheme)) {
+      protocol = s.m_protocol;
+      return true;
+    }
+  }
+  return false;
+}
+
 Socket::Socket(SocketProtocol protocol, bool should_close,
                bool child_processes_inherit)
     : IOObject(eFDTypeSocket), m_protocol(protocol),
diff --git a/lldb/tools/lldb-server/Acceptor.cpp b/lldb/tools/lldb-server/Acceptor.cpp
index 2037f1e0f62b9a..cf9a55c6c07da5 100644
--- a/lldb/tools/lldb-server/Acceptor.cpp
+++ b/lldb/tools/lldb-server/Acceptor.cpp
@@ -22,40 +22,6 @@ using namespace lldb_private;
 using namespace lldb_private::lldb_server;
 using namespace llvm;
 
-namespace {
-
-struct SocketScheme {
-  const char *m_scheme;
-  const Socket::SocketProtocol m_protocol;
-};
-
-SocketScheme socket_schemes[] = {
-    {"tcp", Socket::ProtocolTcp},
-    {"udp", Socket::ProtocolUdp},
-    {"unix", Socket::ProtocolUnixDomain},
-    {"unix-abstract", Socket::ProtocolUnixAbstract},
-};
-
-bool FindProtocolByScheme(const char *scheme,
-                          Socket::SocketProtocol &protocol) {
-  for (auto s : socket_schemes) {
-    if (!strcmp(s.m_scheme, scheme)) {
-      protocol = s.m_protocol;
-      return true;
-    }
-  }
-  return false;
-}
-
-const char *FindSchemeByProtocol(const Socket::SocketProtocol protocol) {
-  for (auto s : socket_schemes) {
-    if (s.m_protocol == protocol)
-      return s.m_scheme;
-  }
-  return nullptr;
-}
-}
-
 Status Acceptor::Listen(int backlog) {
   return m_listener_socket_up->Listen(StringRef(m_name), backlog);
 }
@@ -74,7 +40,7 @@ Socket::SocketProtocol Acceptor::GetSocketProtocol() const {
 }
 
 const char *Acceptor::GetSocketScheme() const {
-  return FindSchemeByProtocol(GetSocketProtocol());
+  return Socket::FindSchemeByProtocol(GetSocketProtocol());
 }
 
 std::string Acceptor::GetLocalSocketId() const { return m_local_socket_id(); }
@@ -87,7 +53,8 @@ std::unique_ptr<Acceptor> Acceptor::Create(StringRef name,
   Socket::SocketProtocol socket_protocol = Socket::ProtocolUnixDomain;
   // Try to match socket name as URL - e.g., tcp://localhost:5555
   if (std::optional<URI> res = URI::Parse(name)) {
-    if (!FindProtocolByScheme(res->scheme.str().c_str(), socket_protocol))
+    if (!Socket::FindProtocolByScheme(res->scheme.str().c_str(),
+                                      socket_protocol))
       error.SetErrorStringWithFormat("Unknown protocol scheme \"%s\"",
                                      res->scheme.str().c_str());
     else



More information about the lldb-commits mailing list