| 1 | //===-- FifoFiles.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_TOOLS_LLDB_DAP_FIFOFILES_H |
| 10 | #define LLDB_TOOLS_LLDB_DAP_FIFOFILES_H |
| 11 | |
| 12 | #include "llvm/Support/Error.h" |
| 13 | #include "llvm/Support/JSON.h" |
| 14 | |
| 15 | #include <chrono> |
| 16 | |
| 17 | namespace lldb_dap { |
| 18 | |
| 19 | /// Struct that controls the life of a fifo file in the filesystem. |
| 20 | /// |
| 21 | /// The file is destroyed when the destructor is invoked. |
| 22 | struct FifoFile { |
| 23 | FifoFile(llvm::StringRef path); |
| 24 | |
| 25 | ~FifoFile(); |
| 26 | |
| 27 | std::string m_path; |
| 28 | }; |
| 29 | |
| 30 | /// Create a fifo file in the filesystem. |
| 31 | /// |
| 32 | /// \param[in] path |
| 33 | /// The path for the fifo file. |
| 34 | /// |
| 35 | /// \return |
| 36 | /// A \a std::shared_ptr<FifoFile> if the file could be created, or an |
| 37 | /// \a llvm::Error in case of failures. |
| 38 | llvm::Expected<std::shared_ptr<FifoFile>> CreateFifoFile(llvm::StringRef path); |
| 39 | |
| 40 | class FifoFileIO { |
| 41 | public: |
| 42 | /// \param[in] fifo_file |
| 43 | /// The path to an input fifo file that exists in the file system. |
| 44 | /// |
| 45 | /// \param[in] other_endpoint_name |
| 46 | /// A human readable name for the other endpoint that will communicate |
| 47 | /// using this file. This is used for error messages. |
| 48 | FifoFileIO(llvm::StringRef fifo_file, llvm::StringRef other_endpoint_name); |
| 49 | |
| 50 | /// Read the next JSON object from the underlying input fifo file. |
| 51 | /// |
| 52 | /// The JSON object is expected to be a single line delimited with \a |
| 53 | /// std::endl. |
| 54 | /// |
| 55 | /// \return |
| 56 | /// An \a llvm::Error object indicating the success or failure of this |
| 57 | /// operation. Failures arise if the timeout is hit, the next line of text |
| 58 | /// from the fifo file is not a valid JSON object, or is it impossible to |
| 59 | /// read from the file. |
| 60 | llvm::Expected<llvm::json::Value> ReadJSON(std::chrono::milliseconds timeout); |
| 61 | |
| 62 | /// Serialize a JSON object and write it to the underlying output fifo file. |
| 63 | /// |
| 64 | /// \param[in] json |
| 65 | /// The JSON object to send. It will be printed as a single line delimited |
| 66 | /// with \a std::endl. |
| 67 | /// |
| 68 | /// \param[in] timeout |
| 69 | /// A timeout for how long we should until for the data to be consumed. |
| 70 | /// |
| 71 | /// \return |
| 72 | /// An \a llvm::Error object indicating whether the data was consumed by |
| 73 | /// a reader or not. |
| 74 | llvm::Error SendJSON( |
| 75 | const llvm::json::Value &json, |
| 76 | std::chrono::milliseconds timeout = std::chrono::milliseconds(20000)); |
| 77 | |
| 78 | private: |
| 79 | std::string m_fifo_file; |
| 80 | std::string m_other_endpoint_name; |
| 81 | }; |
| 82 | |
| 83 | } // namespace lldb_dap |
| 84 | |
| 85 | #endif // LLDB_TOOLS_LLDB_DAP_FIFOFILES_H |
| 86 | |