• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2022-2025 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 PANDA_TOOLING_INSPECTOR_CONNECTION_SERVER_ENDPOINT_BASE_H
17 #define PANDA_TOOLING_INSPECTOR_CONNECTION_SERVER_ENDPOINT_BASE_H
18 
19 #include <functional>
20 
21 #include "connection/endpoint_base.h"
22 #include "connection/server.h"
23 
24 namespace ark::tooling::inspector {
25 // Base class for server endpoints implementations.
26 // Provides callbacks to be executed during client connections handling.
27 // NOLINTNEXTLINE(fuchsia-multiple-inheritance)
28 class ServerEndpointBase : public EndpointBase, public Server {
29 public:
30     using MethodResponse = Expected<std::unique_ptr<JsonSerializable>, JRPCError>;
31     using Handler = std::function<MethodResponse(const std::string &, const JsonObject &params)>;
32 
33 public:
OnValidate(std::function<void ()> && handler)34     void OnValidate(std::function<void()> &&handler) override
35     {
36         onValidate_ = std::move(handler);
37     }
38 
OnOpen(std::function<void ()> && handler)39     void OnOpen(std::function<void()> &&handler) override
40     {
41         onOpen_ = std::move(handler);
42     }
43 
OnFail(std::function<void ()> && handler)44     void OnFail(std::function<void()> &&handler) override
45     {
46         onFail_ = std::move(handler);
47     }
48 
49     using Server::Call;
50     void Call(const std::string &sessionId, const char *method,
51               std::function<void(JsonObjectBuilder &)> &&params) override;
52 
53     void OnCall(const char *method, Handler &&handler) override;
54 
55 protected:
56     std::function<void()> onValidate_ = []() {};  // NOLINT(misc-non-private-member-variables-in-classes)
57     std::function<void()> onOpen_ = []() {};      // NOLINT(misc-non-private-member-variables-in-classes)
58     std::function<void()> onFail_ = []() {};      // NOLINT(misc-non-private-member-variables-in-classes)
59 };
60 }  // namespace ark::tooling::inspector
61 
62 #endif  // PANDA_TOOLING_INSPECTOR_CONNECTION_SERVER_ENDPOINT_BASE_H
63