• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2024-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 #include "connection/ohos_ws/ohos_ws_server.h"
17 
18 #include <string_view>
19 
20 #include "os/thread.h"
21 #include "utils/logger.h"
22 
23 #include "inspector.h"
24 namespace ark::tooling::inspector {
25 [[maybe_unused]] static constexpr std::string_view G_ARK_TS_INSPECTOR_NAME = "ArkEtsDebugger";
26 
ParseMessage(const std::string & msg)27 bool OhosWsServer::ParseMessage(const std::string& msg)
28 {
29     if (endpoint_ == nullptr) {
30         return false;
31     }
32     if (!endpoint_->IsConnected() && !AcceptNewWsConnection()) {
33         LOG(WARNING, DEBUGGER) << "Inspector server is unable to establish a new connection, exiting";
34         return false;
35     }
36 
37     HandleMessage(msg);
38     return true;
39 }
40 
Start(uint32_t port)41 bool OhosWsServer::Start([[maybe_unused]] uint32_t port)
42 {
43     return true;
44 }
45 
InitEndPoint(std::shared_ptr<void> endPoint)46 void OhosWsServer::InitEndPoint(std::shared_ptr<void> endPoint)
47 {
48     if (endPoint == nullptr) {
49         LOG(ERROR, DEBUGGER) << "OhosWsServer::InitEndPoint endPoint is nullptr";
50         return;
51     }
52 
53     endpoint_ = std::static_pointer_cast<OHOS::ArkCompiler::Toolchain::WebSocketServer>(endPoint);
54 }
55 
Stop()56 bool OhosWsServer::Stop()
57 {
58     // Stop event loop before closing endpoint server.
59     Kill();
60     socketpairMode_ = false;
61     return true;
62 }
63 
StartForSocketpair(int socketfd)64 bool OhosWsServer::StartForSocketpair(int socketfd)
65 {
66     bool succeeded = endpoint_->InitUnixWebSocket(socketfd);
67     if (succeeded) {
68         LOG(INFO, DEBUGGER) << "Inspector server listening on " << socketfd;
69         socketpairMode_ = true;
70         return true;
71     }
72 
73     LOG(ERROR, DEBUGGER) << "Failed to bind Inspector server on " << socketfd;
74     return false;
75 }
76 
AcceptNewWsConnection()77 bool OhosWsServer::AcceptNewWsConnection()
78 {
79     if (socketpairMode_) {
80         return endpoint_->ConnectUnixWebSocketBySocketpair();
81     }
82     return endpoint_->AcceptNewConnection();
83 }
84 
85 }  // namespace ark::tooling::inspector
86