1 //===-- Socket.h ------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_Host_Socket_h_
11 #define liblldb_Host_Socket_h_
16 #include "lldb/lldb-private.h"
18 #include "lldb/Host/IOObject.h"
19 #include "lldb/Host/Predicate.h"
20 #include "lldb/Host/SocketAddress.h"
21 #include "lldb/Utility/Status.h"
24 #include "lldb/Host/windows/windows.h"
33 namespace lldb_private {
36 typedef SOCKET NativeSocket;
38 typedef int NativeSocket;
41 class Socket : public IOObject {
50 static const NativeSocket kInvalidSocketValue;
54 static std::unique_ptr<Socket> Create(const SocketProtocol protocol,
55 bool child_processes_inherit,
58 virtual Status Connect(llvm::StringRef name) = 0;
59 virtual Status Listen(llvm::StringRef name, int backlog) = 0;
60 virtual Status Accept(Socket *&socket) = 0;
62 // Initialize a Tcp Socket object in listening mode. listen and accept are
64 // separately because the caller may wish to manipulate or query the socket
66 // initialized, but before entering a blocking accept.
67 static Status TcpListen(llvm::StringRef host_and_port,
68 bool child_processes_inherit, Socket *&socket,
69 Predicate<uint16_t> *predicate, int backlog = 5);
70 static Status TcpConnect(llvm::StringRef host_and_port,
71 bool child_processes_inherit, Socket *&socket);
72 static Status UdpConnect(llvm::StringRef host_and_port,
73 bool child_processes_inherit, Socket *&socket);
74 static Status UnixDomainConnect(llvm::StringRef host_and_port,
75 bool child_processes_inherit,
77 static Status UnixDomainAccept(llvm::StringRef host_and_port,
78 bool child_processes_inherit, Socket *&socket);
79 static Status UnixAbstractConnect(llvm::StringRef host_and_port,
80 bool child_processes_inherit,
82 static Status UnixAbstractAccept(llvm::StringRef host_and_port,
83 bool child_processes_inherit,
86 int GetOption(int level, int option_name, int &option_value);
87 int SetOption(int level, int option_name, int option_value);
89 NativeSocket GetNativeSocket() const { return m_socket; }
90 SocketProtocol GetSocketProtocol() const { return m_protocol; }
92 Status Read(void *buf, size_t &num_bytes) override;
93 Status Write(const void *buf, size_t &num_bytes) override;
95 virtual Status PreDisconnect();
96 Status Close() override;
98 bool IsValid() const override { return m_socket != kInvalidSocketValue; }
99 WaitableHandle GetWaitableHandle() override;
101 static bool DecodeHostAndPort(llvm::StringRef host_and_port,
102 std::string &host_str, std::string &port_str,
103 int32_t &port, Status *error_ptr);
106 Socket(SocketProtocol protocol, bool should_close,
107 bool m_child_process_inherit);
109 virtual size_t Send(const void *buf, const size_t num_bytes);
111 static void SetLastError(Status &error);
112 static NativeSocket CreateSocket(const int domain, const int type,
114 bool child_processes_inherit, Status &error);
115 static NativeSocket AcceptSocket(NativeSocket sockfd, struct sockaddr *addr,
117 bool child_processes_inherit, Status &error);
119 SocketProtocol m_protocol;
120 NativeSocket m_socket;
121 bool m_child_processes_inherit;
124 } // namespace lldb_private
126 #endif // liblldb_Host_Socket_h_