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