llvm-project
131 строка · 4.1 Кб
1//===-- Acceptor.cpp --------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "Acceptor.h"
10
11#include "llvm/ADT/StringRef.h"
12#include "llvm/Support/ScopedPrinter.h"
13
14#include "lldb/Host/ConnectionFileDescriptor.h"
15#include "lldb/Host/common/TCPSocket.h"
16#include "lldb/Utility/StreamString.h"
17#include "lldb/Utility/UriParser.h"
18#include <optional>
19
20using namespace lldb;
21using namespace lldb_private;
22using namespace lldb_private::lldb_server;
23using namespace llvm;
24
25namespace {
26
27struct SocketScheme {
28const char *m_scheme;
29const Socket::SocketProtocol m_protocol;
30};
31
32SocketScheme socket_schemes[] = {
33{"tcp", Socket::ProtocolTcp},
34{"udp", Socket::ProtocolUdp},
35{"unix", Socket::ProtocolUnixDomain},
36{"unix-abstract", Socket::ProtocolUnixAbstract},
37};
38
39bool FindProtocolByScheme(const char *scheme,
40Socket::SocketProtocol &protocol) {
41for (auto s : socket_schemes) {
42if (!strcmp(s.m_scheme, scheme)) {
43protocol = s.m_protocol;
44return true;
45}
46}
47return false;
48}
49
50const char *FindSchemeByProtocol(const Socket::SocketProtocol protocol) {
51for (auto s : socket_schemes) {
52if (s.m_protocol == protocol)
53return s.m_scheme;
54}
55return nullptr;
56}
57}
58
59Status Acceptor::Listen(int backlog) {
60return m_listener_socket_up->Listen(StringRef(m_name), backlog);
61}
62
63Status Acceptor::Accept(const bool child_processes_inherit, Connection *&conn) {
64Socket *conn_socket = nullptr;
65auto error = m_listener_socket_up->Accept(conn_socket);
66if (error.Success())
67conn = new ConnectionFileDescriptor(conn_socket);
68
69return error;
70}
71
72Socket::SocketProtocol Acceptor::GetSocketProtocol() const {
73return m_listener_socket_up->GetSocketProtocol();
74}
75
76const char *Acceptor::GetSocketScheme() const {
77return FindSchemeByProtocol(GetSocketProtocol());
78}
79
80std::string Acceptor::GetLocalSocketId() const { return m_local_socket_id(); }
81
82std::unique_ptr<Acceptor> Acceptor::Create(StringRef name,
83const bool child_processes_inherit,
84Status &error) {
85error.Clear();
86
87Socket::SocketProtocol socket_protocol = Socket::ProtocolUnixDomain;
88// Try to match socket name as URL - e.g., tcp://localhost:5555
89if (std::optional<URI> res = URI::Parse(name)) {
90if (!FindProtocolByScheme(res->scheme.str().c_str(), socket_protocol))
91error.SetErrorStringWithFormat("Unknown protocol scheme \"%s\"",
92res->scheme.str().c_str());
93else
94name = name.drop_front(res->scheme.size() + strlen("://"));
95} else {
96// Try to match socket name as $host:port - e.g., localhost:5555
97if (!llvm::errorToBool(Socket::DecodeHostAndPort(name).takeError()))
98socket_protocol = Socket::ProtocolTcp;
99}
100
101if (error.Fail())
102return std::unique_ptr<Acceptor>();
103
104std::unique_ptr<Socket> listener_socket_up =
105Socket::Create(socket_protocol, child_processes_inherit, error);
106
107LocalSocketIdFunc local_socket_id;
108if (error.Success()) {
109if (listener_socket_up->GetSocketProtocol() == Socket::ProtocolTcp) {
110TCPSocket *tcp_socket =
111static_cast<TCPSocket *>(listener_socket_up.get());
112local_socket_id = [tcp_socket]() {
113auto local_port = tcp_socket->GetLocalPortNumber();
114return (local_port != 0) ? llvm::to_string(local_port) : "";
115};
116} else {
117const std::string socket_name = std::string(name);
118local_socket_id = [socket_name]() { return socket_name; };
119}
120
121return std::unique_ptr<Acceptor>(
122new Acceptor(std::move(listener_socket_up), name, local_socket_id));
123}
124
125return std::unique_ptr<Acceptor>();
126}
127
128Acceptor::Acceptor(std::unique_ptr<Socket> &&listener_socket, StringRef name,
129const LocalSocketIdFunc &local_socket_id)
130: m_listener_socket_up(std::move(listener_socket)), m_name(name.str()),
131m_local_socket_id(local_socket_id) {}
132