Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame^] | 1 | //===-- Acceptor.cpp --------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "Acceptor.h" |
| 11 | |
| 12 | #include "llvm/ADT/StringRef.h" |
| 13 | |
| 14 | #include "lldb/Core/StreamString.h" |
| 15 | #include "lldb/Host/ConnectionFileDescriptor.h" |
| 16 | #include "lldb/Host/common/TCPSocket.h" |
| 17 | #include "lldb/Host/posix/DomainSocket.h" |
| 18 | |
| 19 | using namespace lldb; |
| 20 | using namespace lldb_private; |
| 21 | using namespace lldb_private::lldb_server; |
| 22 | using namespace llvm; |
| 23 | |
| 24 | Error |
| 25 | Acceptor::Listen(int backlog) |
| 26 | { |
| 27 | return m_listener_socket_up->Listen(StringRef(m_name.c_str()), |
| 28 | backlog); |
| 29 | } |
| 30 | |
| 31 | Error |
| 32 | Acceptor::Accept(const bool child_processes_inherit, Connection *&conn) |
| 33 | { |
| 34 | Socket* conn_socket = nullptr; |
| 35 | auto error = m_listener_socket_up->Accept(StringRef(m_name.c_str()), |
| 36 | child_processes_inherit, |
| 37 | conn_socket); |
| 38 | if (error.Success()) |
| 39 | conn = new ConnectionFileDescriptor(conn_socket); |
| 40 | |
| 41 | return error; |
| 42 | } |
| 43 | |
| 44 | Socket::SocketProtocol |
| 45 | Acceptor::GetSocketProtocol() const |
| 46 | { |
| 47 | return m_listener_socket_up->GetSocketProtocol(); |
| 48 | } |
| 49 | |
| 50 | std::string |
| 51 | Acceptor::GetLocalSocketId() const |
| 52 | { |
| 53 | return m_local_socket_id(); |
| 54 | } |
| 55 | |
| 56 | std::unique_ptr<Acceptor> |
| 57 | Acceptor::Create(StringRef name, const bool child_processes_inherit, Error &error) |
| 58 | { |
| 59 | error.Clear(); |
| 60 | |
| 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)) |
| 67 | { |
| 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); |
| 74 | } |
| 75 | else |
| 76 | { |
| 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)); |
| 82 | } |
| 83 | |
| 84 | if (error.Success()) |
| 85 | return std::unique_ptr<Acceptor>( |
| 86 | new Acceptor(std::move(listener_socket), name, local_socket_id)); |
| 87 | |
| 88 | return std::unique_ptr<Acceptor>(); |
| 89 | } |
| 90 | |
| 91 | Acceptor::Acceptor(std::unique_ptr<Socket> &&listener_socket, |
| 92 | StringRef name, |
| 93 | const LocalSocketIdFunc &local_socket_id) |
| 94 | : m_listener_socket_up(std::move(listener_socket)), |
| 95 | m_name(name.str()), |
| 96 | m_local_socket_id(local_socket_id) |
| 97 | { |
| 98 | } |