1 /* 2 * Copyright (c) 2025 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 #ifndef COMMUNICATIONNETSTACK_WEBSOCKET_EXEC_COMMON_H 17 #define COMMUNICATIONNETSTACK_WEBSOCKET_EXEC_COMMON_H 18 19 #include <map> 20 #include <cstdint> 21 #include <string> 22 #include "libwebsockets.h" 23 #include "netstack_log.h" 24 25 namespace OHOS::NetStack::Websocket { 26 struct WebSocketConnection { 27 std::string clientIP; 28 uint32_t clientPort; 29 }; 30 31 class UserData { 32 public: 33 struct SendData { SendDataSendData34 SendData(void *paraData, size_t paraLength, lws_write_protocol paraProtocol) 35 : data(paraData), length(paraLength), protocol(paraProtocol) 36 { 37 } 38 39 SendData() = delete; 40 41 ~SendData() = default; 42 43 void *data; 44 size_t length; 45 lws_write_protocol protocol; 46 }; 47 UserData(lws_context * context)48 explicit UserData(lws_context *context) 49 : closeStatus(LWS_CLOSE_STATUS_NOSTATUS), openStatus(0), closed_(false), threadStop_(false), context_(context) 50 { 51 } 52 IsClosed()53 bool IsClosed() 54 { 55 std::lock_guard<std::mutex> lock(mutex_); 56 return closed_; 57 } 58 IsThreadStop()59 bool IsThreadStop() 60 { 61 return threadStop_.load(); 62 } 63 SetThreadStop(bool threadStop)64 void SetThreadStop(bool threadStop) 65 { 66 threadStop_.store(threadStop); 67 } 68 Close(lws_close_status status,const std::string & reason)69 void Close(lws_close_status status, const std::string &reason) 70 { 71 std::lock_guard<std::mutex> lock(mutex_); 72 closeStatus = status; 73 closeReason = reason; 74 closed_ = true; 75 } 76 Push(void * data,size_t length,lws_write_protocol protocol)77 void Push(void *data, size_t length, lws_write_protocol protocol) 78 { 79 std::lock_guard<std::mutex> lock(mutex_); 80 dataQueue_.emplace(data, length, protocol); 81 } 82 Pop()83 SendData Pop() 84 { 85 std::lock_guard<std::mutex> lock(mutex_); 86 if (dataQueue_.empty()) { 87 return {nullptr, 0, LWS_WRITE_TEXT}; 88 } 89 SendData data = dataQueue_.front(); 90 dataQueue_.pop(); 91 return data; 92 } 93 SetContext(lws_context * context)94 void SetContext(lws_context *context) 95 { 96 context_ = context; 97 } 98 GetContext()99 lws_context *GetContext() 100 { 101 return context_; 102 } 103 IsEmpty()104 bool IsEmpty() 105 { 106 std::lock_guard<std::mutex> lock(mutex_); 107 if (dataQueue_.empty()) { 108 return true; 109 } 110 return false; 111 } 112 SetLws(lws * wsi)113 void SetLws(lws *wsi) 114 { 115 std::lock_guard<std::mutex> lock(mutexForLws_); 116 if (wsi == nullptr) { 117 NETSTACK_LOGD("set wsi nullptr"); 118 } 119 wsi_ = wsi; 120 } 121 TriggerWritable()122 void TriggerWritable() 123 { 124 std::lock_guard<std::mutex> lock(mutexForLws_); 125 if (wsi_ == nullptr) { 126 NETSTACK_LOGE("wsi is nullptr, can not trigger"); 127 return; 128 } 129 lws_callback_on_writable(wsi_); 130 } 131 132 std::map<std::string, std::string> header; 133 134 lws_close_status closeStatus; 135 136 std::string closeReason; 137 138 uint32_t openStatus; 139 140 std::string openMessage; 141 142 lws_retry_bo_t retry_policy { 143 .secs_since_valid_ping = 30, 144 .secs_since_valid_hangup = 60, 145 .jitter_percent = 20, 146 }; 147 148 private: 149 volatile bool closed_; 150 151 std::atomic_bool threadStop_; 152 153 std::mutex mutex_; 154 155 std::mutex mutexForLws_; 156 157 lws_context *context_; 158 159 std::queue<SendData> dataQueue_; 160 161 lws *wsi_ = nullptr; 162 }; 163 } // namespace OHOS::NetStack::Websocket 164 #endif