Skip to content

Commit db4d986

Browse files
committedOct 27, 2015
Add Socket::Create factory method which uses socket protocol to find an appropriate implementation class.
http://reviews.llvm.org/D14085 llvm-svn: 251417
1 parent d5cc99c commit db4d986

File tree

5 files changed

+123
-44
lines changed

5 files changed

+123
-44
lines changed
 

‎lldb/include/lldb/Host/Socket.h

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#ifndef liblldb_Host_Socket_h_
1111
#define liblldb_Host_Socket_h_
1212

13+
#include <memory>
1314
#include <string>
1415

1516
#include "lldb/lldb-private.h"
@@ -53,6 +54,8 @@ class Socket : public IOObject
5354

5455
~Socket() override;
5556

57+
static std::unique_ptr<Socket> Create(const SocketProtocol protocol, bool child_processes_inherit, Error &error);
58+
5659
virtual Error Connect(llvm::StringRef name) = 0;
5760
virtual Error Listen(llvm::StringRef name, int backlog) = 0;
5861
virtual Error Accept(llvm::StringRef name, bool child_processes_inherit, Socket *&socket) = 0;

‎lldb/include/lldb/Host/common/UDPSocket.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ namespace lldb_private
1717
class UDPSocket: public Socket
1818
{
1919
public:
20-
static Error Connect(llvm::StringRef name, bool child_processes_inherit, Socket *&send_socket, Socket *&recv_socket);
20+
UDPSocket(bool child_processes_inherit, Error &error);
2121

22+
static Error Connect(llvm::StringRef name, bool child_processes_inherit, Socket *&send_socket, Socket *&recv_socket);
2223
private:
2324
UDPSocket(NativeSocket socket);
24-
UDPSocket(bool child_processes_inherit, Error &error);
2525

2626
size_t Send(const void *buf, const size_t num_bytes) override;
2727
Error Connect(llvm::StringRef name) override;

‎lldb/source/Host/common/Socket.cpp

+41-21
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,49 @@ Socket::~Socket()
8686
Close();
8787
}
8888

89+
std::unique_ptr<Socket> Socket::Create(const SocketProtocol protocol, bool child_processes_inherit, Error &error)
90+
{
91+
error.Clear();
92+
93+
std::unique_ptr<Socket> socket_up;
94+
switch (protocol)
95+
{
96+
case ProtocolTcp:
97+
socket_up.reset(new TCPSocket(child_processes_inherit, error));
98+
break;
99+
case ProtocolUdp:
100+
socket_up.reset(new UDPSocket(child_processes_inherit, error));
101+
break;
102+
case ProtocolUnixDomain:
103+
#ifndef LLDB_DISABLE_POSIX
104+
socket_up.reset(new DomainSocket(child_processes_inherit, error));
105+
#else
106+
error.SetErrorString("Unix domain sockets are not supported on this platform.");
107+
#endif
108+
break;
109+
case ProtocolUnixAbstract:
110+
#ifdef __linux__
111+
socket_up.reset(new AbstractSocket(child_processes_inherit, error));
112+
#else
113+
error.SetErrorString("Abstract domain sockets are not supported on this platform.");
114+
#endif
115+
break;
116+
}
117+
118+
if (error.Fail())
119+
socket_up.reset();
120+
121+
return socket_up;
122+
}
123+
89124
Error Socket::TcpConnect(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&socket)
90125
{
91126
Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION));
92127
if (log)
93128
log->Printf ("Socket::%s (host/port = %s)", __FUNCTION__, host_and_port.data());
94129

95130
Error error;
96-
std::unique_ptr<TCPSocket> connect_socket(new TCPSocket(child_processes_inherit, error));
131+
std::unique_ptr<Socket> connect_socket(Create(ProtocolTcp, child_processes_inherit, error));
97132
if (error.Fail())
98133
return error;
99134

@@ -161,25 +196,21 @@ Error Socket::UdpConnect(llvm::StringRef host_and_port, bool child_processes_inh
161196
Error Socket::UnixDomainConnect(llvm::StringRef name, bool child_processes_inherit, Socket *&socket)
162197
{
163198
Error error;
164-
#ifndef LLDB_DISABLE_POSIX
165-
std::unique_ptr<DomainSocket> connect_socket(new DomainSocket(child_processes_inherit, error));
199+
std::unique_ptr<Socket> connect_socket(Create(ProtocolUnixDomain, child_processes_inherit, error));
166200
if (error.Fail())
167201
return error;
168202

169203
error = connect_socket->Connect(name);
170204
if (error.Success())
171205
socket = connect_socket.release();
172-
#else
173-
error.SetErrorString("Unix domain sockets are not supported on this platform.");
174-
#endif
206+
175207
return error;
176208
}
177209

178210
Error Socket::UnixDomainAccept(llvm::StringRef name, bool child_processes_inherit, Socket *&socket)
179211
{
180212
Error error;
181-
#ifndef LLDB_DISABLE_POSIX
182-
std::unique_ptr<DomainSocket> listen_socket(new DomainSocket(child_processes_inherit, error));
213+
std::unique_ptr<Socket> listen_socket(Create(ProtocolUnixDomain, child_processes_inherit, error));
183214
if (error.Fail())
184215
return error;
185216

@@ -188,36 +219,28 @@ Error Socket::UnixDomainAccept(llvm::StringRef name, bool child_processes_inheri
188219
return error;
189220

190221
error = listen_socket->Accept(name, child_processes_inherit, socket);
191-
#else
192-
error.SetErrorString("Unix domain sockets are not supported on this platform.");
193-
#endif
194222
return error;
195223
}
196224

197225
Error
198226
Socket::UnixAbstractConnect(llvm::StringRef name, bool child_processes_inherit, Socket *&socket)
199227
{
200228
Error error;
201-
#ifdef __linux__
202-
std::unique_ptr<Socket> connect_socket(new AbstractSocket(child_processes_inherit, error));
229+
std::unique_ptr<Socket> connect_socket(Create(ProtocolUnixAbstract, child_processes_inherit, error));
203230
if (error.Fail())
204231
return error;
205232

206233
error = connect_socket->Connect(name);
207234
if (error.Success())
208235
socket = connect_socket.release();
209-
#else
210-
error.SetErrorString("Abstract domain sockets are not supported on this platform.");
211-
#endif
212236
return error;
213237
}
214238

215239
Error
216240
Socket::UnixAbstractAccept(llvm::StringRef name, bool child_processes_inherit, Socket *&socket)
217241
{
218242
Error error;
219-
#ifdef __linux__
220-
std::unique_ptr<Socket> listen_socket(new AbstractSocket(child_processes_inherit, error));
243+
std::unique_ptr<Socket> listen_socket(Create(ProtocolUnixAbstract,child_processes_inherit, error));
221244
if (error.Fail())
222245
return error;
223246

@@ -226,9 +249,6 @@ Socket::UnixAbstractAccept(llvm::StringRef name, bool child_processes_inherit, S
226249
return error;
227250

228251
error = listen_socket->Accept(name, child_processes_inherit, socket);
229-
#else
230-
error.SetErrorString("Abstract domain sockets are not supported on this platform.");
231-
#endif
232252
return error;
233253
}
234254

‎lldb/tools/lldb-server/Acceptor.cpp

+73-19
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,44 @@
1414
#include "lldb/Core/StreamString.h"
1515
#include "lldb/Host/ConnectionFileDescriptor.h"
1616
#include "lldb/Host/common/TCPSocket.h"
17-
#include "lldb/Host/posix/DomainSocket.h"
17+
18+
#include "Utility/UriParser.h"
1819

1920
using namespace lldb;
2021
using namespace lldb_private;
2122
using namespace lldb_private::lldb_server;
2223
using namespace llvm;
2324

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+
2455
Error
2556
Acceptor::Listen(int backlog)
2657
{
@@ -58,32 +89,55 @@ Acceptor::Create(StringRef name, const bool child_processes_inherit, Error &erro
5889
{
5990
error.Clear();
6091

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))
6797
{
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("://"));
74102
}
75103
else
76104
{
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;
82111
}
83112

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;
84120
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+
85138
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+
}
87141

88142
return std::unique_ptr<Acceptor>();
89143
}

‎lldb/tools/lldb-server/Acceptor.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ class Acceptor
3939
static std::unique_ptr<Acceptor>
4040
Create(llvm::StringRef name, const bool child_processes_inherit, Error &error);
4141

42-
Socket::SocketProtocol GetSocketProtocol() const;
42+
Socket::SocketProtocol
43+
GetSocketProtocol() const;
4344

4445
// Returns either TCP port number as string or domain socket path.
4546
// Empty string is returned in case of error.
46-
std::string GetLocalSocketId() const;
47+
std::string
48+
GetLocalSocketId() const;
4749

4850
private:
4951
typedef std::function<std::string()> LocalSocketIdFunc;

0 commit comments

Comments
 (0)
Please sign in to comment.