1e509ee18Sopenharmony_ci/*
2e509ee18Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3e509ee18Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e509ee18Sopenharmony_ci * you may not use this file except in compliance with the License.
5e509ee18Sopenharmony_ci * You may obtain a copy of the License at
6e509ee18Sopenharmony_ci *
7e509ee18Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e509ee18Sopenharmony_ci *
9e509ee18Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e509ee18Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e509ee18Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e509ee18Sopenharmony_ci * See the License for the specific language governing permissions and
13e509ee18Sopenharmony_ci * limitations under the License.
14e509ee18Sopenharmony_ci */
15e509ee18Sopenharmony_ci
16e509ee18Sopenharmony_ci#ifndef ARKCOMPILER_TOOLCHAIN_INSPECTOR_WS_SERVER_H
17e509ee18Sopenharmony_ci#define ARKCOMPILER_TOOLCHAIN_INSPECTOR_WS_SERVER_H
18e509ee18Sopenharmony_ci
19e509ee18Sopenharmony_ci#include <atomic>
20e509ee18Sopenharmony_ci#include <functional>
21e509ee18Sopenharmony_ci#include <memory>
22e509ee18Sopenharmony_ci#include <mutex>
23e509ee18Sopenharmony_ci#ifdef WINDOWS_PLATFORM
24e509ee18Sopenharmony_ci#include <pthread.h>
25e509ee18Sopenharmony_ci#endif
26e509ee18Sopenharmony_ci#include <string>
27e509ee18Sopenharmony_ci
28e509ee18Sopenharmony_cinamespace OHOS::ArkCompiler::Toolchain {
29e509ee18Sopenharmony_ciclass WebSocketServer;
30e509ee18Sopenharmony_ci
31e509ee18Sopenharmony_cistruct DebugInfo {
32e509ee18Sopenharmony_ci    int socketfd {-2};
33e509ee18Sopenharmony_ci    std::string componentName {};
34e509ee18Sopenharmony_ci    int32_t instanceId {0};
35e509ee18Sopenharmony_ci    int port {-1};
36e509ee18Sopenharmony_ci};
37e509ee18Sopenharmony_ci
38e509ee18Sopenharmony_ciclass WsServer {
39e509ee18Sopenharmony_cipublic:
40e509ee18Sopenharmony_ci    WsServer(const DebugInfo& debugInfo, const std::function<void(std::string&&)>& onMessage);
41e509ee18Sopenharmony_ci    ~WsServer();
42e509ee18Sopenharmony_ci    void RunServer();
43e509ee18Sopenharmony_ci    void ContinueRunserver();
44e509ee18Sopenharmony_ci    void StopServer();
45e509ee18Sopenharmony_ci    void SendReply(const std::string& message) const;
46e509ee18Sopenharmony_ci    void NotifyDisconnectEvent() const;
47e509ee18Sopenharmony_ci
48e509ee18Sopenharmony_ci    pthread_t tid_ {0};
49e509ee18Sopenharmony_ci
50e509ee18Sopenharmony_ciprivate:
51e509ee18Sopenharmony_ci
52e509ee18Sopenharmony_ci    std::atomic<bool> terminateExecution_ { false };
53e509ee18Sopenharmony_ci    std::mutex wsMutex_;
54e509ee18Sopenharmony_ci    DebugInfo debugInfo_ {};
55e509ee18Sopenharmony_ci    std::function<void(std::string&&)> wsOnMessage_ {};
56e509ee18Sopenharmony_ci    std::unique_ptr<WebSocketServer> webSocket_ { nullptr };
57e509ee18Sopenharmony_ci};
58e509ee18Sopenharmony_ci} // namespace OHOS::ArkCompiler::Toolchain
59e509ee18Sopenharmony_ci
60e509ee18Sopenharmony_ci#endif // ARKCOMPILER_TOOLCHAIN_INSPECTOR_WS_SERVER_H
61