Skip to content

[lldb][NFC] Moved FindSchemeByProtocol() from Acceptor to Socket #104439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lldb/include/lldb/Host/Socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
32 changes: 32 additions & 0 deletions lldb/source/Host/common/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
39 changes: 3 additions & 36 deletions lldb/tools/lldb-server/Acceptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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(); }
Expand All @@ -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
Expand Down
Loading