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 "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<WebSocket>();
38 #if !defined(OHOS_PLATFORM)
39 LOGI("WsSever Runsever: Init tcp websocket %{public}d", port_);
40 if (!webSocket_->InitTcpWebSocket(port_)) {
41 return;
42 }
43 #else
44 int appPid = getpid();
45 std::string pidStr = std::to_string(appPid);
46 std::string instanceIdStr("");
47
48 if (instanceId_ != 0) {
49 instanceIdStr = std::to_string(instanceId_);
50 }
51 std::string sockName = pidStr + instanceIdStr + componentName_;
52 LOGI("WsServer RunServer: %{public}d%{public}s%{public}s", appPid, instanceIdStr.c_str(),
53 componentName_.c_str());
54 if (!webSocket_->InitUnixWebSocket(sockName)) {
55 return;
56 }
57 #endif
58 }
59 while (!terminateExecution_) {
60 #if !defined(OHOS_PLATFORM)
61 if (!webSocket_->ConnectTcpWebSocket()) {
62 return;
63 }
64 #else
65 if (!webSocket_->ConnectUnixWebSocket()) {
66 return;
67 }
68 #endif
69 while (webSocket_->IsConnected()) {
70 std::string message = webSocket_->Decode();
71 if (!message.empty()) {
72 LOGI("WsServer OnMessage: %{public}s", message.c_str());
73 wsOnMessage_(std::move(message));
74 }
75 }
76 }
77 }
78
StopServer()79 void WsServer::StopServer()
80 {
81 LOGI("WsServer StopServer");
82 {
83 std::lock_guard<std::mutex> lock(wsMutex_);
84 terminateExecution_ = true;
85 if (webSocket_ != nullptr) {
86 webSocket_->Close();
87 }
88 }
89 #if defined(OHOS_PLATFORM)
90 pthread_join(tid_, nullptr);
91 #endif
92 if (webSocket_ != nullptr) {
93 webSocket_.reset();
94 }
95 }
96
SendReply(const std::string & message) const97 void WsServer::SendReply(const std::string& message) const
98 {
99 std::unique_lock<std::shared_mutex> lock(g_mutex);
100 if (webSocket_ == nullptr) {
101 LOGE("WsServer SendReply websocket has been closed unexpectedly");
102 return;
103 }
104 LOGI("WsServer SendReply: %{public}s", message.c_str());
105 webSocket_->SendReply(message);
106 }
107 } // namespace OHOS::ArkCompiler::Toolchain
108