Add Socket::Create factory method which uses socket protocol to find an appropriate implementation class.
http://reviews.llvm.org/D14085 llvm-svn: 251417
This commit is contained in:
parent
d5cc99c3dd
commit
db4d9865b4
|
@ -10,6 +10,7 @@
|
|||
#ifndef liblldb_Host_Socket_h_
|
||||
#define liblldb_Host_Socket_h_
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "lldb/lldb-private.h"
|
||||
|
@ -53,6 +54,8 @@ public:
|
|||
|
||||
~Socket() override;
|
||||
|
||||
static std::unique_ptr<Socket> Create(const SocketProtocol protocol, bool child_processes_inherit, Error &error);
|
||||
|
||||
virtual Error Connect(llvm::StringRef name) = 0;
|
||||
virtual Error Listen(llvm::StringRef name, int backlog) = 0;
|
||||
virtual Error Accept(llvm::StringRef name, bool child_processes_inherit, Socket *&socket) = 0;
|
||||
|
|
|
@ -17,11 +17,11 @@ namespace lldb_private
|
|||
class UDPSocket: public Socket
|
||||
{
|
||||
public:
|
||||
static Error Connect(llvm::StringRef name, bool child_processes_inherit, Socket *&send_socket, Socket *&recv_socket);
|
||||
UDPSocket(bool child_processes_inherit, Error &error);
|
||||
|
||||
static Error Connect(llvm::StringRef name, bool child_processes_inherit, Socket *&send_socket, Socket *&recv_socket);
|
||||
private:
|
||||
UDPSocket(NativeSocket socket);
|
||||
UDPSocket(bool child_processes_inherit, Error &error);
|
||||
|
||||
size_t Send(const void *buf, const size_t num_bytes) override;
|
||||
Error Connect(llvm::StringRef name) override;
|
||||
|
|
|
@ -86,6 +86,41 @@ Socket::~Socket()
|
|||
Close();
|
||||
}
|
||||
|
||||
std::unique_ptr<Socket> Socket::Create(const SocketProtocol protocol, bool child_processes_inherit, Error &error)
|
||||
{
|
||||
error.Clear();
|
||||
|
||||
std::unique_ptr<Socket> socket_up;
|
||||
switch (protocol)
|
||||
{
|
||||
case ProtocolTcp:
|
||||
socket_up.reset(new TCPSocket(child_processes_inherit, error));
|
||||
break;
|
||||
case ProtocolUdp:
|
||||
socket_up.reset(new UDPSocket(child_processes_inherit, error));
|
||||
break;
|
||||
case ProtocolUnixDomain:
|
||||
#ifndef LLDB_DISABLE_POSIX
|
||||
socket_up.reset(new DomainSocket(child_processes_inherit, error));
|
||||
#else
|
||||
error.SetErrorString("Unix domain sockets are not supported on this platform.");
|
||||
#endif
|
||||
break;
|
||||
case ProtocolUnixAbstract:
|
||||
#ifdef __linux__
|
||||
socket_up.reset(new AbstractSocket(child_processes_inherit, error));
|
||||
#else
|
||||
error.SetErrorString("Abstract domain sockets are not supported on this platform.");
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
|
||||
if (error.Fail())
|
||||
socket_up.reset();
|
||||
|
||||
return socket_up;
|
||||
}
|
||||
|
||||
Error Socket::TcpConnect(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&socket)
|
||||
{
|
||||
Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION));
|
||||
|
@ -93,7 +128,7 @@ Error Socket::TcpConnect(llvm::StringRef host_and_port, bool child_processes_inh
|
|||
log->Printf ("Socket::%s (host/port = %s)", __FUNCTION__, host_and_port.data());
|
||||
|
||||
Error error;
|
||||
std::unique_ptr<TCPSocket> connect_socket(new TCPSocket(child_processes_inherit, error));
|
||||
std::unique_ptr<Socket> connect_socket(Create(ProtocolTcp, child_processes_inherit, error));
|
||||
if (error.Fail())
|
||||
return error;
|
||||
|
||||
|
@ -161,25 +196,21 @@ Error Socket::UdpConnect(llvm::StringRef host_and_port, bool child_processes_inh
|
|||
Error Socket::UnixDomainConnect(llvm::StringRef name, bool child_processes_inherit, Socket *&socket)
|
||||
{
|
||||
Error error;
|
||||
#ifndef LLDB_DISABLE_POSIX
|
||||
std::unique_ptr<DomainSocket> connect_socket(new DomainSocket(child_processes_inherit, error));
|
||||
std::unique_ptr<Socket> connect_socket(Create(ProtocolUnixDomain, child_processes_inherit, error));
|
||||
if (error.Fail())
|
||||
return error;
|
||||
|
||||
error = connect_socket->Connect(name);
|
||||
if (error.Success())
|
||||
socket = connect_socket.release();
|
||||
#else
|
||||
error.SetErrorString("Unix domain sockets are not supported on this platform.");
|
||||
#endif
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
Error Socket::UnixDomainAccept(llvm::StringRef name, bool child_processes_inherit, Socket *&socket)
|
||||
{
|
||||
Error error;
|
||||
#ifndef LLDB_DISABLE_POSIX
|
||||
std::unique_ptr<DomainSocket> listen_socket(new DomainSocket(child_processes_inherit, error));
|
||||
std::unique_ptr<Socket> listen_socket(Create(ProtocolUnixDomain, child_processes_inherit, error));
|
||||
if (error.Fail())
|
||||
return error;
|
||||
|
||||
|
@ -188,9 +219,6 @@ Error Socket::UnixDomainAccept(llvm::StringRef name, bool child_processes_inheri
|
|||
return error;
|
||||
|
||||
error = listen_socket->Accept(name, child_processes_inherit, socket);
|
||||
#else
|
||||
error.SetErrorString("Unix domain sockets are not supported on this platform.");
|
||||
#endif
|
||||
return error;
|
||||
}
|
||||
|
||||
|
@ -198,17 +226,13 @@ Error
|
|||
Socket::UnixAbstractConnect(llvm::StringRef name, bool child_processes_inherit, Socket *&socket)
|
||||
{
|
||||
Error error;
|
||||
#ifdef __linux__
|
||||
std::unique_ptr<Socket> connect_socket(new AbstractSocket(child_processes_inherit, error));
|
||||
std::unique_ptr<Socket> connect_socket(Create(ProtocolUnixAbstract, child_processes_inherit, error));
|
||||
if (error.Fail())
|
||||
return error;
|
||||
|
||||
error = connect_socket->Connect(name);
|
||||
if (error.Success())
|
||||
socket = connect_socket.release();
|
||||
#else
|
||||
error.SetErrorString("Abstract domain sockets are not supported on this platform.");
|
||||
#endif
|
||||
return error;
|
||||
}
|
||||
|
||||
|
@ -216,8 +240,7 @@ Error
|
|||
Socket::UnixAbstractAccept(llvm::StringRef name, bool child_processes_inherit, Socket *&socket)
|
||||
{
|
||||
Error error;
|
||||
#ifdef __linux__
|
||||
std::unique_ptr<Socket> listen_socket(new AbstractSocket(child_processes_inherit, error));
|
||||
std::unique_ptr<Socket> listen_socket(Create(ProtocolUnixAbstract,child_processes_inherit, error));
|
||||
if (error.Fail())
|
||||
return error;
|
||||
|
||||
|
@ -226,9 +249,6 @@ Socket::UnixAbstractAccept(llvm::StringRef name, bool child_processes_inherit, S
|
|||
return error;
|
||||
|
||||
error = listen_socket->Accept(name, child_processes_inherit, socket);
|
||||
#else
|
||||
error.SetErrorString("Abstract domain sockets are not supported on this platform.");
|
||||
#endif
|
||||
return error;
|
||||
}
|
||||
|
||||
|
|
|
@ -14,13 +14,44 @@
|
|||
#include "lldb/Core/StreamString.h"
|
||||
#include "lldb/Host/ConnectionFileDescriptor.h"
|
||||
#include "lldb/Host/common/TCPSocket.h"
|
||||
#include "lldb/Host/posix/DomainSocket.h"
|
||||
|
||||
#include "Utility/UriParser.h"
|
||||
|
||||
using namespace lldb;
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Error
|
||||
Acceptor::Listen(int backlog)
|
||||
{
|
||||
|
@ -58,32 +89,55 @@ Acceptor::Create(StringRef name, const bool child_processes_inherit, Error &erro
|
|||
{
|
||||
error.Clear();
|
||||
|
||||
LocalSocketIdFunc local_socket_id;
|
||||
std::unique_ptr<Socket> listener_socket = nullptr;
|
||||
std::string host_str;
|
||||
std::string port_str;
|
||||
int32_t port = INT32_MIN;
|
||||
if (Socket::DecodeHostAndPort (name, host_str, port_str, port, &error))
|
||||
Socket::SocketProtocol socket_protocol = Socket::ProtocolUnixDomain;
|
||||
int port;
|
||||
std::string scheme, host, path;
|
||||
// Try to match socket name as URL - e.g., tcp://localhost:5555
|
||||
if (UriParser::Parse(name.str(), scheme, host, port, path))
|
||||
{
|
||||
auto tcp_socket = new TCPSocket(child_processes_inherit, error);
|
||||
local_socket_id = [tcp_socket]() {
|
||||
auto local_port = tcp_socket->GetLocalPortNumber();
|
||||
return (local_port != 0) ? std::to_string(local_port) : "";
|
||||
};
|
||||
listener_socket.reset(tcp_socket);
|
||||
if (!FindProtocolByScheme(scheme.c_str(), socket_protocol))
|
||||
error.SetErrorStringWithFormat("Unknown protocol scheme \"%s\"", scheme.c_str());
|
||||
else
|
||||
name = name.drop_front(scheme.size() + strlen("://"));
|
||||
}
|
||||
else
|
||||
{
|
||||
const std::string socket_name = name;
|
||||
local_socket_id = [socket_name](){
|
||||
return socket_name;
|
||||
};
|
||||
listener_socket.reset(new DomainSocket(child_processes_inherit, error));
|
||||
std::string host_str;
|
||||
std::string port_str;
|
||||
int32_t port = INT32_MIN;
|
||||
// Try to match socket name as $host:port - e.g., localhost:5555
|
||||
if (Socket::DecodeHostAndPort (name, host_str, port_str, port, nullptr))
|
||||
socket_protocol = Socket::ProtocolTcp;
|
||||
}
|
||||
|
||||
if (error.Fail())
|
||||
return std::unique_ptr<Acceptor>();
|
||||
|
||||
std::unique_ptr<Socket> listener_socket_up = Socket::Create(
|
||||
socket_protocol, child_processes_inherit, error);
|
||||
|
||||
LocalSocketIdFunc local_socket_id;
|
||||
if (error.Success())
|
||||
{
|
||||
if (listener_socket_up->GetSocketProtocol() == Socket::ProtocolTcp)
|
||||
{
|
||||
TCPSocket* tcp_socket = static_cast<TCPSocket*>(listener_socket_up.get());
|
||||
local_socket_id = [tcp_socket]() {
|
||||
auto local_port = tcp_socket->GetLocalPortNumber();
|
||||
return (local_port != 0) ? std::to_string(local_port) : "";
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
const std::string socket_name = name;
|
||||
local_socket_id = [socket_name](){
|
||||
return socket_name;
|
||||
};
|
||||
}
|
||||
|
||||
return std::unique_ptr<Acceptor>(
|
||||
new Acceptor(std::move(listener_socket), name, local_socket_id));
|
||||
new Acceptor(std::move(listener_socket_up), name, local_socket_id));
|
||||
}
|
||||
|
||||
return std::unique_ptr<Acceptor>();
|
||||
}
|
||||
|
|
|
@ -39,11 +39,13 @@ public:
|
|||
static std::unique_ptr<Acceptor>
|
||||
Create(llvm::StringRef name, const bool child_processes_inherit, Error &error);
|
||||
|
||||
Socket::SocketProtocol GetSocketProtocol() const;
|
||||
Socket::SocketProtocol
|
||||
GetSocketProtocol() const;
|
||||
|
||||
// Returns either TCP port number as string or domain socket path.
|
||||
// Empty string is returned in case of error.
|
||||
std::string GetLocalSocketId() const;
|
||||
std::string
|
||||
GetLocalSocketId() const;
|
||||
|
||||
private:
|
||||
typedef std::function<std::string()> LocalSocketIdFunc;
|
||||
|
|
Loading…
Reference in New Issue