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