ConnectionFileDescriptor.h revision 269024
1//===-- ConnectionFileDescriptor.h ------------------------------*- 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#ifndef liblldb_ConnectionFileDescriptor_h_
11#define liblldb_ConnectionFileDescriptor_h_
12
13// C Includes
14#ifndef _WIN32
15#include <sys/socket.h>
16#include <sys/types.h>
17#include <netinet/in.h>
18#endif
19
20// C++ Includes
21#include <memory>
22
23// Other libraries and framework includes
24// Project includes
25#include "lldb/Core/Connection.h"
26#include "lldb/Host/Mutex.h"
27#include "lldb/Host/Predicate.h"
28
29namespace lldb_private {
30
31class SocketAddress;
32
33class ConnectionFileDescriptor :
34    public Connection
35{
36public:
37
38    ConnectionFileDescriptor ();
39
40    ConnectionFileDescriptor (int fd, bool owns_fd);
41
42    virtual
43    ~ConnectionFileDescriptor ();
44
45    virtual bool
46    IsConnected () const;
47
48    virtual lldb::ConnectionStatus
49    Connect (const char *s, Error *error_ptr);
50
51    virtual lldb::ConnectionStatus
52    Disconnect (Error *error_ptr);
53
54    virtual size_t
55    Read (void *dst,
56          size_t dst_len,
57          uint32_t timeout_usec,
58          lldb::ConnectionStatus &status,
59          Error *error_ptr);
60
61    virtual size_t
62    Write (const void *src,
63           size_t src_len,
64           lldb::ConnectionStatus &status,
65           Error *error_ptr);
66
67    // If the read file descriptor is a socket, then return
68    // the port number that is being used by the socket.
69    uint16_t
70    GetReadPort () const;
71
72    // If the write file descriptor is a socket, then return
73    // the port number that is being used by the socket.
74    uint16_t
75    GetWritePort () const;
76
77    uint16_t
78    GetBoundPort (uint32_t timeout_sec);
79
80protected:
81
82    typedef enum
83    {
84        eFDTypeFile,        // Other FD requireing read/write
85        eFDTypeSocket,      // Socket requiring send/recv
86        eFDTypeSocketUDP    // Unconnected UDP socket requiring sendto/recvfrom
87    } FDType;
88
89    void
90    OpenCommandPipe ();
91
92    void
93    CloseCommandPipe ();
94
95    lldb::ConnectionStatus
96    BytesAvailable (uint32_t timeout_usec, Error *error_ptr);
97
98    lldb::ConnectionStatus
99    SocketListen (const char *host_and_port, Error *error_ptr);
100
101    lldb::ConnectionStatus
102    ConnectTCP (const char *host_and_port, Error *error_ptr);
103
104    lldb::ConnectionStatus
105    ConnectUDP (const char *args, Error *error_ptr);
106
107    lldb::ConnectionStatus
108    NamedSocketAccept (const char *socket_name, Error *error_ptr);
109
110    lldb::ConnectionStatus
111    NamedSocketConnect (const char *socket_name, Error *error_ptr);
112
113    lldb::ConnectionStatus
114    Close (int& fd, FDType type, Error *error);
115
116    int m_fd_send;
117    int m_fd_recv;
118    FDType m_fd_send_type;
119    FDType m_fd_recv_type;
120    std::unique_ptr<SocketAddress> m_udp_send_sockaddr;
121    uint32_t m_socket_timeout_usec;
122    int m_pipe_read;            // A pipe that we select on the reading end of along with
123    int m_pipe_write;           // m_fd_recv so we can force ourselves out of the select.
124    Mutex m_mutex;
125    Predicate<uint16_t> m_port_predicate; // Used when binding to port zero to wait for the thread that creates the socket, binds and listens to resolve the port number
126    bool m_should_close_fd;     // True if this class should close the file descriptor when it goes away.
127    bool m_shutting_down;       // This marks that we are shutting down so if we get woken up from BytesAvailable
128                                // to disconnect, we won't try to read again.
129
130    static uint16_t
131    GetSocketPort (int fd);
132
133    static int
134    GetSocketOption(int fd, int level, int option_name, int &option_value);
135
136    static int
137    SetSocketOption(int fd, int level, int option_name, int option_value);
138
139    bool
140    SetSocketReceiveTimeout (uint32_t timeout_usec);
141
142private:
143    DISALLOW_COPY_AND_ASSIGN (ConnectionFileDescriptor);
144};
145
146} // namespace lldb_private
147
148#endif  // liblldb_ConnectionFileDescriptor_h_
149