Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:44

0001 //===-- Communication.h -----------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_CORE_COMMUNICATION_H
0010 #define LLDB_CORE_COMMUNICATION_H
0011 
0012 #include "lldb/Utility/Timeout.h"
0013 #include "lldb/lldb-defines.h"
0014 #include "lldb/lldb-enumerations.h"
0015 #include "lldb/lldb-forward.h"
0016 #include "lldb/lldb-types.h"
0017 
0018 #include <mutex>
0019 #include <string>
0020 
0021 namespace lldb_private {
0022 class Connection;
0023 class ConstString;
0024 class Status;
0025 
0026 /// \class Communication Communication.h "lldb/Core/Communication.h" An
0027 /// abstract communications class.
0028 ///
0029 /// Communication is an class that handles data communication between two data
0030 /// sources. It uses a Connection class to do the real communication. This
0031 /// approach has a couple of advantages: it allows a single instance of this
0032 /// class to be used even though its connection can change. Connections could
0033 /// negotiate for different connections based on abilities like starting with
0034 /// Bluetooth and negotiating up to WiFi if available.
0035 ///
0036 /// When using this class, all reads and writes happen synchronously on the
0037 /// calling thread. There is also a ThreadedCommunication class that supports
0038 /// multi-threaded mode.
0039 class Communication {
0040 public:
0041   /// Construct the Communication object.
0042   Communication();
0043 
0044   /// Destructor.
0045   ///
0046   /// The destructor is virtual since this class gets subclassed.
0047   virtual ~Communication();
0048 
0049   virtual void Clear();
0050 
0051   /// Connect using the current connection by passing \a url to its connect
0052   /// function. string.
0053   ///
0054   /// \param[in] url
0055   ///     A string that contains all information needed by the
0056   ///     subclass to connect to another client.
0057   ///
0058   /// \return
0059   ///     \b True if the connect succeeded, \b false otherwise. The
0060   ///     internal error object should be filled in with an
0061   ///     appropriate value based on the result of this function.
0062   ///
0063   /// \see Status& Communication::GetError ();
0064   /// \see bool Connection::Connect (const char *url);
0065   lldb::ConnectionStatus Connect(const char *url, Status *error_ptr);
0066 
0067   /// Disconnect the communications connection if one is currently connected.
0068   ///
0069   /// \return
0070   ///     \b True if the disconnect succeeded, \b false otherwise. The
0071   ///     internal error object should be filled in with an
0072   ///     appropriate value based on the result of this function.
0073   ///
0074   /// \see Status& Communication::GetError ();
0075   /// \see bool Connection::Disconnect ();
0076   virtual lldb::ConnectionStatus Disconnect(Status *error_ptr = nullptr);
0077 
0078   /// Check if the connection is valid.
0079   ///
0080   /// \return
0081   ///     \b True if this object is currently connected, \b false
0082   ///     otherwise.
0083   bool IsConnected() const;
0084 
0085   bool HasConnection() const;
0086 
0087   lldb_private::Connection *GetConnection() { return m_connection_sp.get(); }
0088 
0089   /// Read bytes from the current connection.
0090   ///
0091   /// If no read thread is running, this function call the connection's
0092   /// Connection::Read(...) function to get any available.
0093   ///
0094   /// \param[in] dst
0095   ///     A destination buffer that must be at least \a dst_len bytes
0096   ///     long.
0097   ///
0098   /// \param[in] dst_len
0099   ///     The number of bytes to attempt to read, and also the max
0100   ///     number of bytes that can be placed into \a dst.
0101   ///
0102   /// \param[in] timeout
0103   ///     A timeout value or std::nullopt for no timeout.
0104   ///
0105   /// \return
0106   ///     The number of bytes actually read.
0107   ///
0108   /// \see size_t Connection::Read (void *, size_t);
0109   virtual size_t Read(void *dst, size_t dst_len,
0110                       const Timeout<std::micro> &timeout,
0111                       lldb::ConnectionStatus &status, Status *error_ptr);
0112 
0113   /// The actual write function that attempts to write to the communications
0114   /// protocol.
0115   ///
0116   /// Subclasses must override this function.
0117   ///
0118   /// \param[in] src
0119   ///     A source buffer that must be at least \a src_len bytes
0120   ///     long.
0121   ///
0122   /// \param[in] src_len
0123   ///     The number of bytes to attempt to write, and also the
0124   ///     number of bytes are currently available in \a src.
0125   ///
0126   /// \return
0127   ///     The number of bytes actually Written.
0128   size_t Write(const void *src, size_t src_len, lldb::ConnectionStatus &status,
0129                Status *error_ptr);
0130 
0131   /// Repeatedly attempt writing until either \a src_len bytes are written
0132   /// or a permanent failure occurs.
0133   ///
0134   /// \param[in] src
0135   ///     A source buffer that must be at least \a src_len bytes
0136   ///     long.
0137   ///
0138   /// \param[in] src_len
0139   ///     The number of bytes to attempt to write, and also the
0140   ///     number of bytes are currently available in \a src.
0141   ///
0142   /// \return
0143   ///     The number of bytes actually Written.
0144   size_t WriteAll(const void *src, size_t src_len,
0145                   lldb::ConnectionStatus &status, Status *error_ptr);
0146 
0147   /// Sets the connection that it to be used by this class.
0148   ///
0149   /// By making a communication class that uses different connections it
0150   /// allows a single communication interface to negotiate and change its
0151   /// connection without any interruption to the client. It also allows the
0152   /// Communication class to be subclassed for packet based communication.
0153   ///
0154   /// \param[in] connection
0155   ///     A connection that this class will own and destroy.
0156   ///
0157   /// \see
0158   ///     class Connection
0159   virtual void SetConnection(std::unique_ptr<Connection> connection);
0160 
0161   static std::string ConnectionStatusAsString(lldb::ConnectionStatus status);
0162 
0163   bool GetCloseOnEOF() const { return m_close_on_eof; }
0164 
0165   void SetCloseOnEOF(bool b) { m_close_on_eof = b; }
0166 
0167 protected:
0168   lldb::ConnectionSP m_connection_sp; ///< The connection that is current in use
0169                                       ///by this communications class.
0170   std::mutex
0171       m_write_mutex; ///< Don't let multiple threads write at the same time...
0172   bool m_close_on_eof;
0173 
0174   size_t ReadFromConnection(void *dst, size_t dst_len,
0175                             const Timeout<std::micro> &timeout,
0176                             lldb::ConnectionStatus &status, Status *error_ptr);
0177 
0178 private:
0179   Communication(const Communication &) = delete;
0180   const Communication &operator=(const Communication &) = delete;
0181 };
0182 
0183 } // namespace lldb_private
0184 
0185 #endif // LLDB_CORE_COMMUNICATION_H