|
14 | 14 | #include "lldb/Core/StreamString.h"
|
15 | 15 | #include "lldb/Host/ConnectionFileDescriptor.h"
|
16 | 16 | #include "lldb/Host/common/TCPSocket.h"
|
17 |
| -#include "lldb/Host/posix/DomainSocket.h" |
| 17 | + |
| 18 | +#include "Utility/UriParser.h" |
18 | 19 |
|
19 | 20 | using namespace lldb;
|
20 | 21 | using namespace lldb_private;
|
21 | 22 | using namespace lldb_private::lldb_server;
|
22 | 23 | using namespace llvm;
|
23 | 24 |
|
| 25 | +namespace { |
| 26 | + |
| 27 | +struct SocketScheme |
| 28 | +{ |
| 29 | + const char* m_scheme; |
| 30 | + const Socket::SocketProtocol m_protocol; |
| 31 | +}; |
| 32 | + |
| 33 | +SocketScheme socket_schemes[] = { |
| 34 | + {"tcp", Socket::ProtocolTcp}, |
| 35 | + {"udp", Socket::ProtocolUdp}, |
| 36 | + {"unix", Socket::ProtocolUnixDomain}, |
| 37 | + {"unix-abstract", Socket::ProtocolUnixAbstract}, |
| 38 | +}; |
| 39 | + |
| 40 | +bool FindProtocolByScheme(const char* scheme, Socket::SocketProtocol& protocol) |
| 41 | +{ |
| 42 | + for (auto s: socket_schemes) |
| 43 | + { |
| 44 | + if (!strcmp(s.m_scheme, scheme)) |
| 45 | + { |
| 46 | + protocol = s.m_protocol; |
| 47 | + return true; |
| 48 | + } |
| 49 | + } |
| 50 | + return false; |
| 51 | +} |
| 52 | + |
| 53 | +} |
| 54 | + |
24 | 55 | Error
|
25 | 56 | Acceptor::Listen(int backlog)
|
26 | 57 | {
|
@@ -58,32 +89,55 @@ Acceptor::Create(StringRef name, const bool child_processes_inherit, Error &erro
|
58 | 89 | {
|
59 | 90 | error.Clear();
|
60 | 91 |
|
61 |
| - LocalSocketIdFunc local_socket_id; |
62 |
| - std::unique_ptr<Socket> listener_socket = nullptr; |
63 |
| - std::string host_str; |
64 |
| - std::string port_str; |
65 |
| - int32_t port = INT32_MIN; |
66 |
| - if (Socket::DecodeHostAndPort (name, host_str, port_str, port, &error)) |
| 92 | + Socket::SocketProtocol socket_protocol = Socket::ProtocolUnixDomain; |
| 93 | + int port; |
| 94 | + std::string scheme, host, path; |
| 95 | + // Try to match socket name as URL - e.g., tcp://localhost:5555 |
| 96 | + if (UriParser::Parse(name.str(), scheme, host, port, path)) |
67 | 97 | {
|
68 |
| - auto tcp_socket = new TCPSocket(child_processes_inherit, error); |
69 |
| - local_socket_id = [tcp_socket]() { |
70 |
| - auto local_port = tcp_socket->GetLocalPortNumber(); |
71 |
| - return (local_port != 0) ? std::to_string(local_port) : ""; |
72 |
| - }; |
73 |
| - listener_socket.reset(tcp_socket); |
| 98 | + if (!FindProtocolByScheme(scheme.c_str(), socket_protocol)) |
| 99 | + error.SetErrorStringWithFormat("Unknown protocol scheme \"%s\"", scheme.c_str()); |
| 100 | + else |
| 101 | + name = name.drop_front(scheme.size() + strlen("://")); |
74 | 102 | }
|
75 | 103 | else
|
76 | 104 | {
|
77 |
| - const std::string socket_name = name; |
78 |
| - local_socket_id = [socket_name](){ |
79 |
| - return socket_name; |
80 |
| - }; |
81 |
| - listener_socket.reset(new DomainSocket(child_processes_inherit, error)); |
| 105 | + std::string host_str; |
| 106 | + std::string port_str; |
| 107 | + int32_t port = INT32_MIN; |
| 108 | + // Try to match socket name as $host:port - e.g., localhost:5555 |
| 109 | + if (Socket::DecodeHostAndPort (name, host_str, port_str, port, nullptr)) |
| 110 | + socket_protocol = Socket::ProtocolTcp; |
82 | 111 | }
|
83 | 112 |
|
| 113 | + if (error.Fail()) |
| 114 | + return std::unique_ptr<Acceptor>(); |
| 115 | + |
| 116 | + std::unique_ptr<Socket> listener_socket_up = Socket::Create( |
| 117 | + socket_protocol, child_processes_inherit, error); |
| 118 | + |
| 119 | + LocalSocketIdFunc local_socket_id; |
84 | 120 | if (error.Success())
|
| 121 | + { |
| 122 | + if (listener_socket_up->GetSocketProtocol() == Socket::ProtocolTcp) |
| 123 | + { |
| 124 | + TCPSocket* tcp_socket = static_cast<TCPSocket*>(listener_socket_up.get()); |
| 125 | + local_socket_id = [tcp_socket]() { |
| 126 | + auto local_port = tcp_socket->GetLocalPortNumber(); |
| 127 | + return (local_port != 0) ? std::to_string(local_port) : ""; |
| 128 | + }; |
| 129 | + } |
| 130 | + else |
| 131 | + { |
| 132 | + const std::string socket_name = name; |
| 133 | + local_socket_id = [socket_name](){ |
| 134 | + return socket_name; |
| 135 | + }; |
| 136 | + } |
| 137 | + |
85 | 138 | return std::unique_ptr<Acceptor>(
|
86 |
| - new Acceptor(std::move(listener_socket), name, local_socket_id)); |
| 139 | + new Acceptor(std::move(listener_socket_up), name, local_socket_id)); |
| 140 | + } |
87 | 141 |
|
88 | 142 | return std::unique_ptr<Acceptor>();
|
89 | 143 | }
|
|
0 commit comments