1 /*
2 * Copyright (c) 2023 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 "connect_server.h"
17 #include <mutex>
18 #include <unistd.h>
19 #include "common/log_wrapper.h"
20 #include "websocket/server/websocket_server.h"
21
22 namespace OHOS::ArkCompiler::Toolchain {
23 std::shared_mutex g_sendMutex;
24
25 // defined in .cpp file for WebSocketServer forward declaration
ConnectServer(int socketfd,std::function<void (std::string &&)> onMessage)26 ConnectServer::ConnectServer(int socketfd, std::function<void(std::string&&)> onMessage)
27 : socketfd_(socketfd), wsOnMessage_(std::move(onMessage))
28 {}
29
ConnectServer(const std::string & bundleName,std::function<void (std::string &&)> onMessage)30 ConnectServer::ConnectServer(const std::string& bundleName, std::function<void(std::string&&)> onMessage)
31 : bundleName_(bundleName), wsOnMessage_(std::move(onMessage))
32 {}
33
34 ConnectServer::~ConnectServer() = default;
35
RunServer()36 void ConnectServer::RunServer()
37 {
38 terminateExecution_ = false;
39 webSocket_ = std::make_unique<WebSocketServer>();
40 tid_ = pthread_self();
41 #if defined(OHOS_PLATFORM)
42 int runSeverInOldProcess = -2; // run sever in old process.
43 int appPid = getprocpid();
44 std::string pidStr = std::to_string(appPid);
45 std::string sockName = pidStr + bundleName_;
46 if (socketfd_ == runSeverInOldProcess) {
47 if (!webSocket_->InitUnixWebSocket(sockName)) {
48 return;
49 }
50 } else {
51 if (!webSocket_->InitUnixWebSocket(socketfd_)) {
52 return;
53 }
54 }
55 #endif
56 while (!terminateExecution_) {
57 #if defined(OHOS_PLATFORM)
58 if (socketfd_ == runSeverInOldProcess) {
59 if (!webSocket_->AcceptNewConnection()) {
60 return;
61 }
62 } else {
63 if (!webSocket_->ConnectUnixWebSocketBySocketpair()) {
64 return;
65 }
66 }
67 #endif
68 while (webSocket_->IsConnected()) {
69 std::string message = webSocket_->Decode();
70 if (!message.empty()) {
71 wsOnMessage_(std::move(message));
72 }
73 }
74 }
75 }
76
StopServer()77 void ConnectServer::StopServer()
78 {
79 LOGI("ConnectServer StopServer");
80 terminateExecution_ = true;
81 if (webSocket_ != nullptr) {
82 webSocket_->Close();
83 pthread_join(tid_, nullptr);
84 webSocket_.reset();
85 }
86 }
87
SendMessage(const std::string & message) const88 void ConnectServer::SendMessage(const std::string& message) const
89 {
90 std::unique_lock<std::shared_mutex> lock(g_sendMutex);
91 if (webSocket_ == nullptr) {
92 LOGE("ConnectServer SendReply websocket has been closed unexpectedly");
93 return;
94 }
95 LOGI("ConnectServer SendReply: %{public}s", message.c_str());
96 webSocket_->SendReply(message);
97 }
98 } // namespace OHOS::ArkCompiler::Toolchain
99