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