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