1 //===-- GDBRemoteCommunicationServer.h --------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef liblldb_GDBRemoteCommunicationServer_h_
11 #define liblldb_GDBRemoteCommunicationServer_h_
12 
13 // C Includes
14 // C++ Includes
15 #include <functional>
16 #include <map>
17 
18 // Other libraries and framework includes
19 // Project includes
20 #include "GDBRemoteCommunication.h"
21 #include "lldb/lldb-private-forward.h"
22 
23 class StringExtractorGDBRemote;
24 
25 namespace lldb_private {
26 namespace process_gdb_remote {
27 
28 class ProcessGDBRemote;
29 
30 class GDBRemoteCommunicationServer : public GDBRemoteCommunication {
31 public:
32   using PortMap = std::map<uint16_t, lldb::pid_t>;
33   using PacketHandler =
34       std::function<PacketResult(StringExtractorGDBRemote &packet, Error &error,
35                                  bool &interrupt, bool &quit)>;
36 
37   GDBRemoteCommunicationServer(const char *comm_name,
38                                const char *listener_name);
39 
40   ~GDBRemoteCommunicationServer() override;
41 
42   void
43   RegisterPacketHandler(StringExtractorGDBRemote::ServerPacketType packet_type,
44                         PacketHandler handler);
45 
46   PacketResult GetPacketAndSendResponse(Timeout<std::micro> timeout,
47                                         Error &error, bool &interrupt,
48                                         bool &quit);
49 
50   // After connecting, do a little handshake with the client to make sure
51   // we are at least communicating
52   bool HandshakeWithClient();
53 
54 protected:
55   std::map<StringExtractorGDBRemote::ServerPacketType, PacketHandler>
56       m_packet_handlers;
57   bool m_exit_now; // use in asynchronous handling to indicate process should
58                    // exit.
59 
60   PacketResult SendUnimplementedResponse(const char *packet);
61 
62   PacketResult SendErrorResponse(uint8_t error);
63 
64   PacketResult SendIllFormedResponse(const StringExtractorGDBRemote &packet,
65                                      const char *error_message);
66 
67   PacketResult SendOKResponse();
68 
69 private:
70   DISALLOW_COPY_AND_ASSIGN(GDBRemoteCommunicationServer);
71 };
72 
73 } // namespace process_gdb_remote
74 } // namespace lldb_private
75 
76 #endif // liblldb_GDBRemoteCommunicationServer_h_
77