aboutsummaryrefslogtreecommitdiff
path: root/lldb/source/Plugins/Protocol/MCP/ProtocolServerMCP.h
blob: b325a3681bccb3e03fa49329ffbfb6367cde7e73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//===- ProtocolServerMCP.h ------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLDB_PLUGINS_PROTOCOL_MCP_PROTOCOLSERVERMCP_H
#define LLDB_PLUGINS_PROTOCOL_MCP_PROTOCOLSERVERMCP_H

#include "lldb/Core/ProtocolServer.h"
#include "lldb/Host/MainLoop.h"
#include "lldb/Host/Socket.h"
#include "lldb/Protocol/MCP/Server.h"
#include "lldb/Protocol/MCP/Transport.h"
#include <map>
#include <memory>
#include <thread>
#include <tuple>
#include <vector>

namespace lldb_private::mcp {

class ProtocolServerMCP : public ProtocolServer {
  using ReadHandleUP = MainLoopBase::ReadHandleUP;
  using TransportUP = std::unique_ptr<lldb_protocol::mcp::MCPTransport>;
  using ServerUP = std::unique_ptr<lldb_protocol::mcp::Server>;

public:
  ProtocolServerMCP();
  virtual ~ProtocolServerMCP() override;

  virtual llvm::Error Start(ProtocolServer::Connection connection) override;
  virtual llvm::Error Stop() override;

  static void Initialize();
  static void Terminate();

  static llvm::StringRef GetPluginNameStatic() { return "MCP"; }
  static llvm::StringRef GetPluginDescriptionStatic();

  static lldb::ProtocolServerUP CreateInstance();

  llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }

  Socket *GetSocket() const override { return m_listener.get(); }

protected:
  // This adds tools and resource providers that
  // are specific to this server. Overridable by the unit tests.
  virtual void Extend(lldb_protocol::mcp::Server &server) const;

private:
  void AcceptCallback(std::unique_ptr<Socket> socket);

  bool m_running = false;

  lldb_protocol::mcp::ServerInfoHandle m_server_info_handle;
  lldb_private::MainLoop m_loop;
  std::thread m_loop_thread;
  std::mutex m_mutex;
  size_t m_client_count = 0;

  std::unique_ptr<Socket> m_listener;

  std::vector<ReadHandleUP> m_listen_handlers;
  std::map<lldb_protocol::mcp::MCPTransport *,
           std::tuple<ServerUP, ReadHandleUP, TransportUP>>
      m_instances;
};
} // namespace lldb_private::mcp

#endif