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 <atomic>
20 #include <functional>
21 #include <memory>
22 #include <mutex>
23 #ifdef WINDOWS_PLATFORM
24 #include <pthread.h>
25 #endif
26 #include <string>
27 
28 namespace OHOS::ArkCompiler::Toolchain {
29 class WebSocketServer;
30 
31 struct DebugInfo {
32     int socketfd {-2};
33     std::string componentName {};
34     int32_t instanceId {0};
35     int port {-1};
36 };
37 
38 class WsServer {
39 public:
40     WsServer(const DebugInfo& debugInfo, const std::function<void(std::string&&)>& onMessage);
41     ~WsServer();
42     void RunServer();
43     void ContinueRunserver();
44     void StopServer();
45     void SendReply(const std::string& message) const;
46     void NotifyDisconnectEvent() const;
47 
48     pthread_t tid_ {0};
49 
50 private:
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