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