LLDB mainline
Transport.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
10#include "llvm/ADT/StringRef.h"
11#include <utility>
12
13using namespace lldb_protocol::mcp;
14using namespace llvm;
15
17 lldb::IOObjectSP out, LogCallback log_callback)
18 : JSONRPCTransport(loop, in, out), m_log_callback(std::move(log_callback)) {
19}
20
21void Transport::Log(StringRef message) {
23 m_log_callback(message);
24}
25
26llvm::Error Transport::ReplyWithParseError(StringRef raw_message,
27 StringRef reason) {
28 llvm::Expected<json::Value> value = json::parse(raw_message);
29 if (!value) {
30 // JSON-RPC forbids guessing an id, and malformed JSON carries none.
31 consumeError(value.takeError());
32 return llvm::Error::success();
33 }
34
35 const json::Object *object = value->getAsObject();
36 if (!object)
37 return llvm::Error::success();
38
39 // A message without an id is a notification, which takes no response.
40 const json::Value *raw_id = object->get("id");
41 if (!raw_id)
42 return llvm::Error::success();
43
44 Id id;
45 if (std::optional<StringRef> str = raw_id->getAsString())
46 id = str->str();
47 else if (std::optional<int64_t> num = raw_id->getAsInteger())
48 id = *num;
49 else
50 return llvm::Error::success();
51
52 return Send(Response{id, mcp::Error{eErrorCodeInvalidRequest, reason.str()}});
53}
llvm::Error Send(const typename ProtocolDescriptor::Evt &evt) override
void Log(llvm::StringRef message) override
Definition Transport.cpp:21
llvm::Error ReplyWithParseError(llvm::StringRef raw_message, llvm::StringRef reason) override
Sends an error response for a message that failed to parse, described by reason.
Definition Transport.cpp:26
Transport(lldb_private::MainLoop &loop, lldb::IOObjectSP in, lldb::IOObjectSP out, LogCallback log_callback={})
Definition Transport.cpp:16
MainLoopPosix MainLoop
Definition MainLoop.h:20
std::variant< int64_t, std::string > Id
A Request or Response 'id'.
Definition Protocol.h:36
@ eErrorCodeInvalidRequest
The JSON sent is not a valid Request object.
Definition Protocol.h:56
llvm::unique_function< void(llvm::StringRef message)> LogCallback
Generic logging callback, to allow the MCP server / client / transport layer to be independent of the...
Definition Transport.h:81
std::shared_ptr< lldb_private::IOObject > IOObjectSP
A response to a request, either an error or a result.
Definition Protocol.h:81