aboutsummaryrefslogtreecommitdiff
path: root/include/lldb/Core/ConnectionFileDescriptor.h
blob: 15598c9b13358e6bf1708141b0983245ad03e3c7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//===-- ConnectionFileDescriptor.h ------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef liblldb_ConnectionFileDescriptor_h_
#define liblldb_ConnectionFileDescriptor_h_

// C Includes
#ifndef _WIN32
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#endif

// C++ Includes
#include <memory>

// Other libraries and framework includes
// Project includes
#include "lldb/Core/Connection.h"
#include "lldb/Host/Mutex.h"
#include "lldb/Host/Predicate.h"

namespace lldb_private {

class SocketAddress;

class ConnectionFileDescriptor :
    public Connection
{
public:

    ConnectionFileDescriptor ();

    ConnectionFileDescriptor (int fd, bool owns_fd);

    virtual
    ~ConnectionFileDescriptor ();

    virtual bool
    IsConnected () const;

    virtual lldb::ConnectionStatus
    Connect (const char *s, Error *error_ptr);

    virtual lldb::ConnectionStatus
    Disconnect (Error *error_ptr);

    virtual size_t
    Read (void *dst, 
          size_t dst_len, 
          uint32_t timeout_usec,
          lldb::ConnectionStatus &status, 
          Error *error_ptr);

    virtual size_t
    Write (const void *src, 
           size_t src_len, 
           lldb::ConnectionStatus &status, 
           Error *error_ptr);

    // If the read file descriptor is a socket, then return
    // the port number that is being used by the socket.
    uint16_t
    GetReadPort () const;
    
    // If the write file descriptor is a socket, then return
    // the port number that is being used by the socket.
    uint16_t
    GetWritePort () const;

    uint16_t
    GetBoundPort (uint32_t timeout_sec);

protected:

    typedef enum
    {
        eFDTypeFile,        // Other FD requireing read/write
        eFDTypeSocket,      // Socket requiring send/recv
        eFDTypeSocketUDP    // Unconnected UDP socket requiring sendto/recvfrom
    } FDType;
    
    void
    OpenCommandPipe ();
    
    void
    CloseCommandPipe ();

    lldb::ConnectionStatus
    BytesAvailable (uint32_t timeout_usec, Error *error_ptr);
    
    lldb::ConnectionStatus
    SocketListen (const char *host_and_port, Error *error_ptr);

    lldb::ConnectionStatus
    ConnectTCP (const char *host_and_port, Error *error_ptr);
    
    lldb::ConnectionStatus
    ConnectUDP (const char *args, Error *error_ptr);
    
    lldb::ConnectionStatus
    NamedSocketAccept (const char *socket_name, Error *error_ptr);

    lldb::ConnectionStatus
    NamedSocketConnect (const char *socket_name, Error *error_ptr);
    
    lldb::ConnectionStatus
    Close (int& fd, FDType type, Error *error);
    
    int m_fd_send;
    int m_fd_recv;
    FDType m_fd_send_type;
    FDType m_fd_recv_type;
    std::unique_ptr<SocketAddress> m_udp_send_sockaddr;
    uint32_t m_socket_timeout_usec;
    int m_pipe_read;            // A pipe that we select on the reading end of along with
    int m_pipe_write;           // m_fd_recv so we can force ourselves out of the select.
    Mutex m_mutex;
    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
    bool m_should_close_fd;     // True if this class should close the file descriptor when it goes away.
    bool m_shutting_down;       // This marks that we are shutting down so if we get woken up from BytesAvailable
                                // to disconnect, we won't try to read again.
    
    static uint16_t
    GetSocketPort (int fd);

    static int
    GetSocketOption(int fd, int level, int option_name, int &option_value);

    static int
    SetSocketOption(int fd, int level, int option_name, int option_value);

    bool
    SetSocketReceiveTimeout (uint32_t timeout_usec);

private:
    DISALLOW_COPY_AND_ASSIGN (ConnectionFileDescriptor);
};

} // namespace lldb_private

#endif  // liblldb_ConnectionFileDescriptor_h_