|
| 1 | +//===-- JSONTransport.h ---------------------------------------------------===// |
| 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 | +// Transport layer for encoding and decoding JSON protocol messages. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#ifndef LLDB_HOST_JSONTRANSPORT_H |
| 14 | +#define LLDB_HOST_JSONTRANSPORT_H |
| 15 | + |
| 16 | +#include "lldb/lldb-forward.h" |
| 17 | +#include "llvm/ADT/StringRef.h" |
| 18 | +#include "llvm/Support/Error.h" |
| 19 | +#include "llvm/Support/FormatVariadic.h" |
| 20 | +#include "llvm/Support/JSON.h" |
| 21 | +#include <chrono> |
| 22 | +#include <system_error> |
| 23 | + |
| 24 | +namespace lldb_private { |
| 25 | + |
| 26 | +class TransportEOFError : public llvm::ErrorInfo<TransportEOFError> { |
| 27 | +public: |
| 28 | + static char ID; |
| 29 | + |
| 30 | + TransportEOFError() = default; |
| 31 | + |
| 32 | + void log(llvm::raw_ostream &OS) const override { |
| 33 | + OS << "transport end of file reached"; |
| 34 | + } |
| 35 | + std::error_code convertToErrorCode() const override { |
| 36 | + return llvm::inconvertibleErrorCode(); |
| 37 | + } |
| 38 | +}; |
| 39 | + |
| 40 | +class TransportTimeoutError : public llvm::ErrorInfo<TransportTimeoutError> { |
| 41 | +public: |
| 42 | + static char ID; |
| 43 | + |
| 44 | + TransportTimeoutError() = default; |
| 45 | + |
| 46 | + void log(llvm::raw_ostream &OS) const override { |
| 47 | + OS << "transport operation timed out"; |
| 48 | + } |
| 49 | + std::error_code convertToErrorCode() const override { |
| 50 | + return std::make_error_code(std::errc::timed_out); |
| 51 | + } |
| 52 | +}; |
| 53 | + |
| 54 | +class TransportInvalidError : public llvm::ErrorInfo<TransportInvalidError> { |
| 55 | +public: |
| 56 | + static char ID; |
| 57 | + |
| 58 | + TransportInvalidError() = default; |
| 59 | + |
| 60 | + void log(llvm::raw_ostream &OS) const override { |
| 61 | + OS << "transport IO object invalid"; |
| 62 | + } |
| 63 | + std::error_code convertToErrorCode() const override { |
| 64 | + return std::make_error_code(std::errc::not_connected); |
| 65 | + } |
| 66 | +}; |
| 67 | + |
| 68 | +/// A transport class that uses JSON for communication. |
| 69 | +class JSONTransport { |
| 70 | +public: |
| 71 | + JSONTransport(lldb::IOObjectSP input, lldb::IOObjectSP output); |
| 72 | + virtual ~JSONTransport() = default; |
| 73 | + |
| 74 | + /// Transport is not copyable. |
| 75 | + /// @{ |
| 76 | + JSONTransport(const JSONTransport &rhs) = delete; |
| 77 | + void operator=(const JSONTransport &rhs) = delete; |
| 78 | + /// @} |
| 79 | + |
| 80 | + /// Writes a message to the output stream. |
| 81 | + template <typename T> llvm::Error Write(const T &t) { |
| 82 | + const std::string message = llvm::formatv("{0}", toJSON(t)).str(); |
| 83 | + return WriteImpl(message); |
| 84 | + } |
| 85 | + |
| 86 | + /// Reads the next message from the input stream. |
| 87 | + template <typename T> |
| 88 | + llvm::Expected<T> Read(const std::chrono::microseconds &timeout) { |
| 89 | + llvm::Expected<std::string> message = ReadImpl(timeout); |
| 90 | + if (!message) |
| 91 | + return message.takeError(); |
| 92 | + return llvm::json::parse<T>(/*JSON=*/*message); |
| 93 | + } |
| 94 | + |
| 95 | +protected: |
| 96 | + virtual void Log(llvm::StringRef message); |
| 97 | + |
| 98 | + virtual llvm::Error WriteImpl(const std::string &message) = 0; |
| 99 | + virtual llvm::Expected<std::string> |
| 100 | + ReadImpl(const std::chrono::microseconds &timeout) = 0; |
| 101 | + |
| 102 | + lldb::IOObjectSP m_input; |
| 103 | + lldb::IOObjectSP m_output; |
| 104 | +}; |
| 105 | + |
| 106 | +/// A transport class for JSON with a HTTP header. |
| 107 | +class HTTPDelimitedJSONTransport : public JSONTransport { |
| 108 | +public: |
| 109 | + HTTPDelimitedJSONTransport(lldb::IOObjectSP input, lldb::IOObjectSP output) |
| 110 | + : JSONTransport(input, output) {} |
| 111 | + virtual ~HTTPDelimitedJSONTransport() = default; |
| 112 | + |
| 113 | +protected: |
| 114 | + virtual llvm::Error WriteImpl(const std::string &message) override; |
| 115 | + virtual llvm::Expected<std::string> |
| 116 | + ReadImpl(const std::chrono::microseconds &timeout) override; |
| 117 | + |
| 118 | + // FIXME: Support any header. |
| 119 | + static constexpr llvm::StringLiteral kHeaderContentLength = |
| 120 | + "Content-Length: "; |
| 121 | + static constexpr llvm::StringLiteral kHeaderSeparator = "\r\n\r\n"; |
| 122 | +}; |
| 123 | + |
| 124 | +/// A transport class for JSON RPC. |
| 125 | +class JSONRPCTransport : public JSONTransport { |
| 126 | +public: |
| 127 | + JSONRPCTransport(lldb::IOObjectSP input, lldb::IOObjectSP output) |
| 128 | + : JSONTransport(input, output) {} |
| 129 | + virtual ~JSONRPCTransport() = default; |
| 130 | + |
| 131 | +protected: |
| 132 | + virtual llvm::Error WriteImpl(const std::string &message) override; |
| 133 | + virtual llvm::Expected<std::string> |
| 134 | + ReadImpl(const std::chrono::microseconds &timeout) override; |
| 135 | + |
| 136 | + static constexpr llvm::StringLiteral kMessageSeparator = "\n"; |
| 137 | +}; |
| 138 | + |
| 139 | +} // namespace lldb_private |
| 140 | + |
| 141 | +#endif |
0 commit comments