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 #include "ws_server.h"
17
18 #include <unistd.h>
19
20 #include "common/log_wrapper.h"
21 #include "websocket/server/websocket_server.h"
22
23 namespace OHOS::ArkCompiler::Toolchain {
24 std::shared_mutex g_mutex;
25
26 // defined in .cpp file for WebSocketServer forward declaration
WsServer(const DebugInfo & debugInfo,const std::function<void (std::string &&)> & onMessage)27 WsServer::WsServer(const DebugInfo& debugInfo, const std::function<void(std::string&&)>& onMessage)
28 : debugInfo_(debugInfo), wsOnMessage_(onMessage)
29 {}
30
31 WsServer::~WsServer() = default;
32
RunServer()33 void WsServer::RunServer()
34 {
35 {
36 std::lock_guard<std::mutex> lock(wsMutex_);
37 if (terminateExecution_) {
38 LOGE("WsServer has been terminated unexpectedly");
39 return;
40 }
41 webSocket_ = std::make_unique<WebSocketServer>();
42 #if !defined(OHOS_PLATFORM)
43 LOGI("WsSever Runsever: Init tcp websocket %{public}d", debugInfo_.port);
44 if (!webSocket_->InitTcpWebSocket(debugInfo_.port)) {
45 return;
46 }
47 #else
48 int runSeverInOldProcess = -2;
49 if (debugInfo_.socketfd == runSeverInOldProcess) {
50 int appPid = getprocpid();
51 std::string pidStr = std::to_string(appPid);
52 std::string instanceIdStr("");
53
54 if (debugInfo_.instanceId != 0) {
55 instanceIdStr = std::to_string(debugInfo_.instanceId);
56 }
57 std::string sockName = pidStr + instanceIdStr + debugInfo_.componentName;
58 LOGI("WsServer RunServer fport localabstract: %{public}d%{public}s%{public}s",
59 appPid, instanceIdStr.c_str(), debugInfo_.componentName.c_str());
60 if (!webSocket_->InitUnixWebSocket(sockName)) {
61 return;
62 }
63 } else {
64 LOGI("WsServer RunServer fport ark: %{public}d", debugInfo_.socketfd);
65 if (!webSocket_->InitUnixWebSocket(debugInfo_.socketfd)) {
66 return;
67 }
68 }
69 #endif
70 }
71 ContinueRunserver();
72 }
ContinueRunserver()73 void WsServer::ContinueRunserver()
74 {
75 while (!terminateExecution_) {
76 #if !defined(OHOS_PLATFORM)
77 if (!webSocket_->AcceptNewConnection()) {
78 return;
79 }
80 #else
81 int runSeverInOldProcess = -2;
82 if (debugInfo_.socketfd == runSeverInOldProcess) {
83 if (!webSocket_->AcceptNewConnection()) {
84 return;
85 }
86 } else {
87 if (!webSocket_->ConnectUnixWebSocketBySocketpair()) {
88 return;
89 }
90 }
91 #endif
92 while (webSocket_->IsConnected()) {
93 std::string message = webSocket_->Decode();
94 if (!message.empty() && webSocket_->IsDecodeDisconnectMsg(message)) {
95 LOGI("WsServer receiving disconnect msg: %{public}s", message.c_str());
96 NotifyDisconnectEvent();
97 } else if (!message.empty()) {
98 LOGI("WsServer OnMessage: %{public}s", message.c_str());
99 wsOnMessage_(std::move(message));
100 }
101 }
102 }
103 }
104
StopServer()105 void WsServer::StopServer()
106 {
107 LOGI("WsServer StopServer");
108 {
109 std::lock_guard<std::mutex> lock(wsMutex_);
110 terminateExecution_ = true;
111 if (webSocket_ != nullptr) {
112 webSocket_->Close();
113 }
114 }
115 pthread_join(tid_, nullptr);
116 if (webSocket_ != nullptr) {
117 webSocket_.reset();
118 }
119 }
120
SendReply(const std::string & message) const121 void WsServer::SendReply(const std::string& message) const
122 {
123 std::unique_lock<std::shared_mutex> lock(g_mutex);
124 if (webSocket_ == nullptr) {
125 LOGE("WsServer SendReply websocket has been closed unexpectedly");
126 return;
127 }
128 LOGI("WsServer SendReply: %{public}s", message.c_str());
129 if (!webSocket_->SendReply(message)) {
130 LOGE("WsServer SendReply send fail");
131 NotifyDisconnectEvent();
132 }
133 }
134
NotifyDisconnectEvent() const135 void WsServer::NotifyDisconnectEvent() const
136 {
137 std::string message = "{\"id\":0, \"method\":\"Debugger.clientDisconnect\", \"params\":{}}";
138 wsOnMessage_(std::move(message));
139 }
140 } // namespace OHOS::ArkCompiler::Toolchain
141