Skip to content

Commit 899a3df

Browse files
authored
[lldb][NFC] Moved FindSchemeByProtocol() from Acceptor to Socket (#104439)
This is the prerequisite for #104238.
1 parent 39a0ded commit 899a3df

File tree

3 files changed

+39
-36
lines changed

3 files changed

+39
-36
lines changed

lldb/include/lldb/Host/Socket.h

+4
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ class Socket : public IOObject {
6060

6161
~Socket() override;
6262

63+
static const char *FindSchemeByProtocol(const SocketProtocol protocol);
64+
static bool FindProtocolByScheme(const char *scheme,
65+
SocketProtocol &protocol);
66+
6367
static llvm::Error Initialize();
6468
static void Terminate();
6569

lldb/source/Host/common/Socket.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,38 @@ static bool IsInterrupted() {
7070
#endif
7171
}
7272

73+
struct SocketScheme {
74+
const char *m_scheme;
75+
const Socket::SocketProtocol m_protocol;
76+
};
77+
78+
static SocketScheme socket_schemes[] = {
79+
{"tcp", Socket::ProtocolTcp},
80+
{"udp", Socket::ProtocolUdp},
81+
{"unix", Socket::ProtocolUnixDomain},
82+
{"unix-abstract", Socket::ProtocolUnixAbstract},
83+
};
84+
85+
const char *
86+
Socket::FindSchemeByProtocol(const Socket::SocketProtocol protocol) {
87+
for (auto s : socket_schemes) {
88+
if (s.m_protocol == protocol)
89+
return s.m_scheme;
90+
}
91+
return nullptr;
92+
}
93+
94+
bool Socket::FindProtocolByScheme(const char *scheme,
95+
Socket::SocketProtocol &protocol) {
96+
for (auto s : socket_schemes) {
97+
if (!strcmp(s.m_scheme, scheme)) {
98+
protocol = s.m_protocol;
99+
return true;
100+
}
101+
}
102+
return false;
103+
}
104+
73105
Socket::Socket(SocketProtocol protocol, bool should_close,
74106
bool child_processes_inherit)
75107
: IOObject(eFDTypeSocket), m_protocol(protocol),

lldb/tools/lldb-server/Acceptor.cpp

+3-36
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,6 @@ using namespace lldb_private;
2222
using namespace lldb_private::lldb_server;
2323
using namespace llvm;
2424

25-
namespace {
26-
27-
struct SocketScheme {
28-
const char *m_scheme;
29-
const Socket::SocketProtocol m_protocol;
30-
};
31-
32-
SocketScheme socket_schemes[] = {
33-
{"tcp", Socket::ProtocolTcp},
34-
{"udp", Socket::ProtocolUdp},
35-
{"unix", Socket::ProtocolUnixDomain},
36-
{"unix-abstract", Socket::ProtocolUnixAbstract},
37-
};
38-
39-
bool FindProtocolByScheme(const char *scheme,
40-
Socket::SocketProtocol &protocol) {
41-
for (auto s : socket_schemes) {
42-
if (!strcmp(s.m_scheme, scheme)) {
43-
protocol = s.m_protocol;
44-
return true;
45-
}
46-
}
47-
return false;
48-
}
49-
50-
const char *FindSchemeByProtocol(const Socket::SocketProtocol protocol) {
51-
for (auto s : socket_schemes) {
52-
if (s.m_protocol == protocol)
53-
return s.m_scheme;
54-
}
55-
return nullptr;
56-
}
57-
}
58-
5925
Status Acceptor::Listen(int backlog) {
6026
return m_listener_socket_up->Listen(StringRef(m_name), backlog);
6127
}
@@ -74,7 +40,7 @@ Socket::SocketProtocol Acceptor::GetSocketProtocol() const {
7440
}
7541

7642
const char *Acceptor::GetSocketScheme() const {
77-
return FindSchemeByProtocol(GetSocketProtocol());
43+
return Socket::FindSchemeByProtocol(GetSocketProtocol());
7844
}
7945

8046
std::string Acceptor::GetLocalSocketId() const { return m_local_socket_id(); }
@@ -87,7 +53,8 @@ std::unique_ptr<Acceptor> Acceptor::Create(StringRef name,
8753
Socket::SocketProtocol socket_protocol = Socket::ProtocolUnixDomain;
8854
// Try to match socket name as URL - e.g., tcp://localhost:5555
8955
if (std::optional<URI> res = URI::Parse(name)) {
90-
if (!FindProtocolByScheme(res->scheme.str().c_str(), socket_protocol))
56+
if (!Socket::FindProtocolByScheme(res->scheme.str().c_str(),
57+
socket_protocol))
9158
error.SetErrorStringWithFormat("Unknown protocol scheme \"%s\"",
9259
res->scheme.str().c_str());
9360
else

0 commit comments

Comments
 (0)