DebugServer2
Loading...
Searching...
No Matches
ProtocolInterpreter.h
1//
2// Copyright (c) 2014-present, Facebook, Inc.
3// All rights reserved.
4//
5// This source code is licensed under the University of Illinois/NCSA Open
6// Source License found in the LICENSE file in the root directory of this
7// source tree. An additional grant of patent rights can be found in the
8// PATENTS file in the same directory.
9//
10
11#pragma once
12
13#include "DebugServer2/GDBRemote/PacketProcessor.h"
14
15#include <string_view>
16
17namespace ds2 {
18namespace GDBRemote {
19
20class SessionBase;
21struct ProtocolHandler;
22
24public:
25 struct Handler {
26 typedef std::vector<Handler> Collection;
27 typedef void (ProtocolHandler::*Callback)(Handler const &,
28 std::string const &);
29 using CallbackType = Callback;
30
31 enum Mode { kModeEquals, kModeStartsWith };
32
33 Mode mode;
34 std::string command;
35 ProtocolHandler *handler;
36 Callback callback;
37
38 int compare(std::string_view command) const;
39 };
40
41private:
42 SessionBase *_session;
43 Handler::Collection _handlers;
44 std::vector<std::string> _lastCommands;
45
46public:
48
49public:
50 inline void setSession(SessionBase *session) { _session = session; }
51 inline SessionBase *session() const {
52 return const_cast<ProtocolInterpreter *>(this)->_session;
53 }
54
55public:
56 void onCommand(std::string_view command, std::string_view arguments);
57
58public:
59 bool registerHandler(Handler const &handler);
60
61 template <typename CallbackType>
62 inline bool
63 registerHandler(Handler::Mode const &mode, std::string const &command,
64 ProtocolHandler *handler, CallbackType const &callback) {
65 Handler _handler = {mode, command, handler, (Handler::Callback)callback};
66 return registerHandler(_handler);
67 }
68
69private:
70 Handler const *findHandler(std::string_view command,
71 size_t &commandLength) const;
72
73public:
74 void onPacketData(std::string const &data, bool valid) override;
75 void onInvalidData(std::string const &data) override;
76
77public:
78 inline std::vector<std::string> const &lastCommands() const {
79 return _lastCommands;
80 }
81 inline void clearLastCommands() { _lastCommands.clear(); }
82};
83
85} // namespace GDBRemote
86} // namespace ds2
Definition ProtocolInterpreter.h:23
Definition SessionBase.h:30
Definition PacketProcessor.h:51
Definition ProtocolInterpreter.h:84
Definition ProtocolInterpreter.h:25