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#ifndef CLIENT_CONTEXT_H 17#define CLIENT_CONTEXT_H 18 19#include <atomic> 20#include <iostream> 21#include <map> 22#include <memory> 23#include <mutex> 24#include <queue> 25#include <signal.h> 26#include <string.h> 27#include <string> 28#include <thread> 29#include "netstack_log.h" 30 31namespace OHOS { 32namespace NetStack { 33namespace WebSocketClient { 34 35struct SendData { 36 SendData(char *paraData, size_t paraLength, lws_write_protocol paraProtocol) 37 : data(paraData), length(paraLength), protocol(paraProtocol) 38 { 39 } 40 41 SendData() = delete; 42 43 ~SendData() = default; 44 45 char *data; 46 size_t length; 47 lws_write_protocol protocol; 48}; 49 50class ClientContext { 51public: 52 ClientContext() : closeStatus(LWS_CLOSE_STATUS_NOSTATUS), openStatus(0), errorCode(0), closed_(false), 53 threadStop_(false), context_(nullptr), clientId(0) {} 54 55 bool IsClosed() 56 { 57 std::lock_guard<std::mutex> lock(mutex_); 58 return closed_; 59 } 60 61 bool IsThreadStop() 62 { 63 return threadStop_.load(); 64 } 65 66 void SetThreadStop(bool threadStop) 67 { 68 threadStop_.store(threadStop); 69 } 70 71 void Close(lws_close_status status, const std::string &reason) 72 { 73 NETSTACK_LOGD("ClientContext Close"); 74 std::lock_guard<std::mutex> lock(mutex_); 75 closeStatus = status; 76 closeReason = reason; 77 closed_ = true; 78 } 79 80 void Push(char *data, size_t length, lws_write_protocol protocol) 81 { 82 std::lock_guard<std::mutex> lock(mutex_); 83 dataQueue_.push(SendData(data, length, protocol)); 84 } 85 86 SendData Pop() 87 { 88 std::lock_guard<std::mutex> lock(mutex_); 89 if (dataQueue_.empty()) { 90 return {nullptr, 0, LWS_WRITE_TEXT}; 91 } 92 93 SendData data = dataQueue_.front(); 94 dataQueue_.pop(); 95 return data; 96 } 97 98 void SetContext(lws_context *context) 99 { 100 context_ = context; 101 } 102 103 lws_context *GetContext() 104 { 105 return context_; 106 } 107 108 void SetClientId(int id) 109 { 110 clientId = id; 111 } 112 113 int GetClientId() 114 { 115 return clientId; 116 } 117 118 std::map<std::string, std::string> header; 119 120 lws_close_status closeStatus; 121 122 std::string closeReason; 123 124 uint32_t openStatus; 125 126 uint32_t errorCode; 127 128 std::string openMessage; 129 130private: 131 bool closed_; 132 133 std::atomic_bool threadStop_; 134 135 std::mutex mutex_; 136 137 lws_context *context_; 138 139 std::queue<SendData> dataQueue_; 140 141 int clientId; 142}; 143}; // namespace WebSocketClient 144} // namespace NetStack 145} // namespace OHOS 146#endif 147