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