• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
22 namespace OHOS::ArkCompiler::Toolchain {
23 std::shared_mutex g_sendMutex;
24 
RunServer()25 void ConnectServer::RunServer()
26 {
27     terminateExecution_ = false;
28     webSocket_ = std::make_unique<WebSocketServer>();
29     tid_ = pthread_self();
30 #if defined(OHOS_PLATFORM)
31     int runSeverInOldProcess = -2; // run sever in old process.
32     int appPid = getpid();
33     std::string pidStr = std::to_string(appPid);
34     std::string sockName = pidStr + bundleName_;
35     if (socketfd_ == runSeverInOldProcess) {
36         if (!webSocket_->InitUnixWebSocket(sockName)) {
37             return;
38         }
39     } else {
40         if (!webSocket_->InitUnixWebSocket(socketfd_)) {
41             return;
42         }
43     }
44 #endif
45     while (!terminateExecution_) {
46 #if defined(OHOS_PLATFORM)
47         if (socketfd_ == runSeverInOldProcess) {
48             if (!webSocket_->AcceptNewConnection()) {
49                 return;
50             }
51         } else {
52             if (!webSocket_->ConnectUnixWebSocketBySocketpair()) {
53                 return;
54             }
55         }
56 #endif
57         while (webSocket_->IsConnected()) {
58             std::string message = webSocket_->Decode();
59             if (!message.empty()) {
60                 wsOnMessage_(std::move(message));
61             }
62         }
63     }
64 }
65 
StopServer()66 void ConnectServer::StopServer()
67 {
68     LOGI("ConnectServer StopServer");
69     terminateExecution_ = true;
70     if (webSocket_ != nullptr) {
71         webSocket_->Close();
72         pthread_join(tid_, nullptr);
73         webSocket_.reset();
74     }
75 }
76 
SendMessage(const std::string & message) const77 void ConnectServer::SendMessage(const std::string& message) const
78 {
79     std::unique_lock<std::shared_mutex> lock(g_sendMutex);
80     if (webSocket_ == nullptr) {
81         LOGE("ConnectServer SendReply websocket has been closed unexpectedly");
82         return;
83     }
84     LOGI("ConnectServer SendReply: %{public}s", message.c_str());
85     webSocket_->SendReply(message);
86 }
87 } // namespace OHOS::ArkCompiler::Toolchain
88