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 WEBSOCKETSERVER_H 17 #define WEBSOCKETSERVER_H 18 19 #include <thread> 20 #include <csignal> 21 #include <mutex> 22 #include <string> 23 #include "libwebsockets.h" 24 25 class WebSocketServer { 26 public: 27 WebSocketServer& operator=(const WebSocketServer&) = delete; 28 WebSocketServer(const WebSocketServer&) = delete; 29 static WebSocketServer& GetInstance(); 30 void SetServerPort(int port); 31 void SetSid(const std::string curSid); 32 static int ProtocolCallback(struct lws* wsi, enum lws_callback_reasons reason, void* user, void* in, size_t len); 33 void StartWebsocketListening(); 34 void Run(); 35 size_t WriteData(unsigned char* data, size_t length); 36 enum class WebSocketState { INIT = -1, UNWRITEABLE = 0, WRITEABLE = 1 }; 37 static WebSocketState webSocketWritable; 38 static uint8_t* firstImageBuffer; 39 static uint64_t firstImagebufferSize; 40 std::mutex mutex; 41 42 private: 43 WebSocketServer(); 44 virtual ~WebSocketServer(); 45 static bool CheckSid(struct lws* wsi); 46 static void SignalHandler(int sig); 47 std::unique_ptr<std::thread> serverThread; 48 int serverPort; 49 const char* serverHostname = "127.0.0.1"; 50 int websocketMaxConn = 1024; 51 static lws* webSocket; 52 static std::atomic<bool> interrupted; 53 static const int MAX_PAYLOAD_SIZE = 6400000; 54 static const int WEBSOCKET_SERVER_TIMEOUT = 1000; 55 struct lws_protocols protocols[2]; 56 std::string sid; 57 static constexpr int sidMaxLength = 256; 58 }; 59 60 #endif // WEBSOCKETSERVER_H 61