• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ARKCOMPILER_TOOLCHAIN_INSPECTOR_WS_SERVER_H
17 #define ARKCOMPILER_TOOLCHAIN_INSPECTOR_WS_SERVER_H
18 
19 #include <functional>
20 #include <iostream>
21 #include <memory>
22 #include <mutex>
23 #ifdef WINDOWS_PLATFORM
24 #include <pthread.h>
25 #endif
26 
27 #include "websocket/server/websocket_server.h"
28 
29 namespace OHOS::ArkCompiler::Toolchain {
30 struct DebugInfo {
31     int socketfd {-2};
32     std::string componentName {};
33     int32_t instanceId {0};
34     int port {-1};
35 };
36 
37 class WsServer {
38 public:
WsServer(const DebugInfo & debugInfo,const std::function<void (std::string &&)> & onMessage)39     WsServer(const DebugInfo& debugInfo, const std::function<void(std::string&&)>& onMessage)
40         : debugInfo_(debugInfo), wsOnMessage_(onMessage)
41     {}
42     ~WsServer() = default;
43     void RunServer();
44     void StopServer();
45     void SendReply(const std::string& message) const;
46 
47     pthread_t tid_ {0};
48 
49 private:
50     void NotifyDisconnectEvent() const;
51 
52     std::atomic<bool> terminateExecution_ { false };
53     std::mutex wsMutex_;
54     DebugInfo debugInfo_ {};
55     std::function<void(std::string&&)> wsOnMessage_ {};
56     std::unique_ptr<WebSocketServer> webSocket_ { nullptr };
57 };
58 } // namespace OHOS::ArkCompiler::Toolchain
59 
60 #endif // ARKCOMPILER_TOOLCHAIN_INSPECTOR_WS_SERVER_H
61