LLDB mainline
Communication.h
Go to the documentation of this file.
1//===-- Communication.h -----------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_CORE_COMMUNICATION_H
10#define LLDB_CORE_COMMUNICATION_H
11
13#include "lldb/lldb-defines.h"
15#include "lldb/lldb-forward.h"
16#include "lldb/lldb-types.h"
17
18#include <mutex>
19#include <string>
20
21namespace lldb_private {
22class Connection;
23class Status;
24
25/// \class Communication Communication.h "lldb/Core/Communication.h" An
26/// abstract communications class.
27///
28/// Communication is an class that handles data communication between two data
29/// sources. It uses a Connection class to do the real communication. This
30/// approach has a couple of advantages: it allows a single instance of this
31/// class to be used even though its connection can change. Connections could
32/// negotiate for different connections based on abilities like starting with
33/// Bluetooth and negotiating up to WiFi if available.
34///
35/// When using this class, all reads and writes happen synchronously on the
36/// calling thread. There is also a ThreadedCommunication class that supports
37/// multi-threaded mode.
39public:
40 /// Construct the Communication object.
42
43 /// Destructor.
44 ///
45 /// The destructor is virtual since this class gets subclassed.
46 virtual ~Communication();
47
48 virtual void Clear();
49
50 /// Connect using the current connection by passing \a url to its connect
51 /// function. string.
52 ///
53 /// \param[in] url
54 /// A string that contains all information needed by the
55 /// subclass to connect to another client.
56 ///
57 /// \return
58 /// \b True if the connect succeeded, \b false otherwise. The
59 /// internal error object should be filled in with an
60 /// appropriate value based on the result of this function.
61 ///
62 /// \see Status& Communication::GetError ();
63 /// \see bool Connection::Connect (const char *url);
64 lldb::ConnectionStatus Connect(const char *url, Status *error_ptr);
65
66 /// Disconnect the communications connection if one is currently connected.
67 ///
68 /// \return
69 /// \b True if the disconnect succeeded, \b false otherwise. The
70 /// internal error object should be filled in with an
71 /// appropriate value based on the result of this function.
72 ///
73 /// \see Status& Communication::GetError ();
74 /// \see bool Connection::Disconnect ();
75 virtual lldb::ConnectionStatus Disconnect(Status *error_ptr = nullptr);
76
77 /// Check if the connection is valid.
78 ///
79 /// \return
80 /// \b True if this object is currently connected, \b false
81 /// otherwise.
82 bool IsConnected() const;
83
84 bool HasConnection() const;
85
87
88 /// Read bytes from the current connection.
89 ///
90 /// If no read thread is running, this function call the connection's
91 /// Connection::Read(...) function to get any available.
92 ///
93 /// \param[in] dst
94 /// A destination buffer that must be at least \a dst_len bytes
95 /// long.
96 ///
97 /// \param[in] dst_len
98 /// The number of bytes to attempt to read, and also the max
99 /// number of bytes that can be placed into \a dst.
100 ///
101 /// \param[in] timeout
102 /// A timeout value or std::nullopt for no timeout.
103 ///
104 /// \return
105 /// The number of bytes actually read.
106 ///
107 /// \see size_t Connection::Read (void *, size_t);
108 virtual size_t Read(void *dst, size_t dst_len,
109 const Timeout<std::micro> &timeout,
110 lldb::ConnectionStatus &status, Status *error_ptr);
111
112 /// The actual write function that attempts to write to the communications
113 /// protocol.
114 ///
115 /// Subclasses must override this function.
116 ///
117 /// \param[in] src
118 /// A source buffer that must be at least \a src_len bytes
119 /// long.
120 ///
121 /// \param[in] src_len
122 /// The number of bytes to attempt to write, and also the
123 /// number of bytes are currently available in \a src.
124 ///
125 /// \return
126 /// The number of bytes actually Written.
127 size_t Write(const void *src, size_t src_len, lldb::ConnectionStatus &status,
128 Status *error_ptr);
129
130 /// Repeatedly attempt writing until either \a src_len bytes are written
131 /// or a permanent failure occurs.
132 ///
133 /// \param[in] src
134 /// A source buffer that must be at least \a src_len bytes
135 /// long.
136 ///
137 /// \param[in] src_len
138 /// The number of bytes to attempt to write, and also the
139 /// number of bytes are currently available in \a src.
140 ///
141 /// \return
142 /// The number of bytes actually Written.
143 size_t WriteAll(const void *src, size_t src_len,
144 lldb::ConnectionStatus &status, Status *error_ptr);
145
146 /// Sets the connection that it to be used by this class.
147 ///
148 /// By making a communication class that uses different connections it
149 /// allows a single communication interface to negotiate and change its
150 /// connection without any interruption to the client. It also allows the
151 /// Communication class to be subclassed for packet based communication.
152 ///
153 /// \param[in] connection
154 /// A connection that this class will own and destroy.
155 ///
156 /// \see
157 /// class Connection
158 virtual void SetConnection(std::unique_ptr<Connection> connection);
159
160 static std::string ConnectionStatusAsString(lldb::ConnectionStatus status);
161
162 bool GetCloseOnEOF() const { return m_close_on_eof; }
163
164 void SetCloseOnEOF(bool b) { m_close_on_eof = b; }
165
166protected:
167 lldb::ConnectionSP m_connection_sp; ///< The connection that is current in use
168 ///by this communications class.
169 std::mutex
170 m_write_mutex; ///< Don't let multiple threads write at the same time...
172
173 size_t ReadFromConnection(void *dst, size_t dst_len,
174 const Timeout<std::micro> &timeout,
175 lldb::ConnectionStatus &status, Status *error_ptr);
176
177private:
178 Communication(const Communication &) = delete;
179 const Communication &operator=(const Communication &) = delete;
180};
181
182} // namespace lldb_private
183
184#endif // LLDB_CORE_COMMUNICATION_H
size_t ReadFromConnection(void *dst, size_t dst_len, const Timeout< std::micro > &timeout, lldb::ConnectionStatus &status, Status *error_ptr)
virtual size_t Read(void *dst, size_t dst_len, const Timeout< std::micro > &timeout, lldb::ConnectionStatus &status, Status *error_ptr)
Read bytes from the current connection.
lldb::ConnectionSP m_connection_sp
The connection that is current in use by this communications class.
lldb::ConnectionStatus Connect(const char *url, Status *error_ptr)
Connect using the current connection by passing url to its connect function.
bool IsConnected() const
Check if the connection is valid.
virtual void SetConnection(std::unique_ptr< Connection > connection)
Sets the connection that it to be used by this class.
virtual ~Communication()
Destructor.
size_t WriteAll(const void *src, size_t src_len, lldb::ConnectionStatus &status, Status *error_ptr)
Repeatedly attempt writing until either src_len bytes are written or a permanent failure occurs.
Communication()
Construct the Communication object.
virtual lldb::ConnectionStatus Disconnect(Status *error_ptr=nullptr)
Disconnect the communications connection if one is currently connected.
static std::string ConnectionStatusAsString(lldb::ConnectionStatus status)
lldb_private::Connection * GetConnection()
const Communication & operator=(const Communication &)=delete
size_t Write(const void *src, size_t src_len, lldb::ConnectionStatus &status, Status *error_ptr)
The actual write function that attempts to write to the communications protocol.
Communication(const Communication &)=delete
std::mutex m_write_mutex
Don't let multiple threads write at the same time...
A communication connection class.
Definition Connection.h:41
An error handling class.
Definition Status.h:118
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::Connection > ConnectionSP
ConnectionStatus
Connection Status Types.