1 | //===-- Transport.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 | // Debug Adapter Protocol transport layer for encoding and decoding protocol |
10 | // messages. |
11 | // |
12 | //===----------------------------------------------------------------------===// |
13 | |
14 | #ifndef LLDB_TOOLS_LLDB_DAP_TRANSPORT_H |
15 | #define LLDB_TOOLS_LLDB_DAP_TRANSPORT_H |
16 | |
17 | #include "DAPForward.h" |
18 | #include "Protocol/ProtocolBase.h" |
19 | #include "lldb/lldb-forward.h" |
20 | #include "llvm/ADT/StringRef.h" |
21 | #include "llvm/Support/Error.h" |
22 | #include <chrono> |
23 | #include <system_error> |
24 | |
25 | namespace lldb_dap { |
26 | |
27 | class EndOfFileError : public llvm::ErrorInfo<EndOfFileError> { |
28 | public: |
29 | static char ID; |
30 | |
31 | EndOfFileError() = default; |
32 | |
33 | void log(llvm::raw_ostream &OS) const override { |
34 | OS << "end of file reached" ; |
35 | } |
36 | std::error_code convertToErrorCode() const override { |
37 | return llvm::inconvertibleErrorCode(); |
38 | } |
39 | }; |
40 | |
41 | class TimeoutError : public llvm::ErrorInfo<TimeoutError> { |
42 | public: |
43 | static char ID; |
44 | |
45 | TimeoutError() = default; |
46 | |
47 | void log(llvm::raw_ostream &OS) const override { |
48 | OS << "operation timed out" ; |
49 | } |
50 | std::error_code convertToErrorCode() const override { |
51 | return std::make_error_code(e: std::errc::timed_out); |
52 | } |
53 | }; |
54 | |
55 | /// A transport class that performs the Debug Adapter Protocol communication |
56 | /// with the client. |
57 | class Transport { |
58 | public: |
59 | Transport(llvm::StringRef client_name, Log *log, lldb::IOObjectSP input, |
60 | lldb::IOObjectSP output); |
61 | ~Transport() = default; |
62 | |
63 | /// Transport is not copyable. |
64 | /// @{ |
65 | Transport(const Transport &rhs) = delete; |
66 | void operator=(const Transport &rhs) = delete; |
67 | /// @} |
68 | |
69 | /// Writes a Debug Adater Protocol message to the output stream. |
70 | llvm::Error Write(const protocol::Message &M); |
71 | |
72 | /// Reads the next Debug Adater Protocol message from the input stream. |
73 | /// |
74 | /// \param timeout[in] |
75 | /// A timeout to wait for reading the initial header. Once a message |
76 | /// header is recieved, this will block until the full message is |
77 | /// read. |
78 | /// |
79 | /// \returns Returns the next protocol message. |
80 | llvm::Expected<protocol::Message> |
81 | Read(const std::chrono::microseconds &timeout); |
82 | |
83 | /// Returns the name of this transport client, for example `stdin/stdout` or |
84 | /// `client_1`. |
85 | llvm::StringRef GetClientName() { return m_client_name; } |
86 | |
87 | private: |
88 | llvm::StringRef m_client_name; |
89 | Log *m_log; |
90 | lldb::IOObjectSP m_input; |
91 | lldb::IOObjectSP m_output; |
92 | }; |
93 | |
94 | } // namespace lldb_dap |
95 | |
96 | #endif |
97 | |