1 /**
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef RUNTIME_TOOLING_INSPECTOR_SERVER_ENDPOINT_INL_H
17 #define RUNTIME_TOOLING_INSPECTOR_SERVER_ENDPOINT_INL_H
18
19 #ifndef CONFIG
20 #error Define CONFIG before including this header
21 #endif
22
23 #include "server_endpoint.h"
24
25 #include "utils/logger.h"
26
27 #include <functional>
28 #include <optional>
29 #include <utility>
30
31 namespace panda::tooling::inspector {
32 template <> // NOLINTNEXTLINE(misc-definitions-in-headers)
ServerEndpoint()33 ServerEndpoint<CONFIG>::ServerEndpoint() noexcept
34 {
35 this->endpoint_.set_validate_handler([this](auto hdl) {
36 if (Pin(hdl)) {
37 return true;
38 }
39
40 this->endpoint_.get_con_from_hdl(hdl)->set_body("Another debug session is in progress");
41 return false;
42 });
43
44 this->endpoint_.set_close_handler([this](auto hdl) { Unpin(hdl); });
45 }
46
47 template <> // NOLINTNEXTLINE(misc-definitions-in-headers)
Call(const char * method,std::function<void (JsonObjectBuilder &)> && params)48 void ServerEndpoint<CONFIG>::Call(const char *method, std::function<void(JsonObjectBuilder &)> &¶ms)
49 {
50 Endpoint<websocketpp::server<CONFIG>>::Call(std::nullopt, method, std::move(params));
51 }
52
53 template <> // NOLINTNEXTLINE(misc-definitions-in-headers)
OnCall(const char * method,std::function<void (JsonObjectBuilder &,const JsonObject &)> && handler)54 void ServerEndpoint<CONFIG>::OnCall(const char *method,
55 std::function<void(JsonObjectBuilder &, const JsonObject &)> &&handler)
56 {
57 Endpoint<websocketpp::server<CONFIG>>::OnCall(method, [this, handler = std::move(handler)](auto id, auto ¶ms) {
58 if (!id) {
59 LOG(INFO, DEBUGGER) << "Invalid request: request has no \"id\"";
60 return;
61 }
62
63 Reply(*id, std::bind(std::ref(handler), std::placeholders::_1, std::cref(params)));
64 });
65 }
66 } // namespace panda::tooling::inspector
67
68 #endif // RUNTIME_TOOLING_INSPECTOR_SERVER_ENDPOINT_INL_H
69