1 /* 2 * Copyright (c) 2021 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 HILOG_INPUT_SOCKET_SERVER_H 17 #define HILOG_INPUT_SOCKET_SERVER_H 18 19 #include <vector> 20 #include <thread> 21 22 #include "dgram_socket_server.h" 23 24 namespace OHOS { 25 namespace HiviewDFX { 26 #define MAX_SOCKET_PACKET_LEN 4096 27 28 class HilogInputSocketServer : public DgramSocketServer { 29 public: 30 31 #ifndef __RECV_MSG_WITH_UCRED_ 32 using HandlingFunc = std::function<void(std::vector<char>& data, int dataLen)>; 33 #else 34 using HandlingFunc = std::function<void(const ucred& credential, std::vector<char>& data, int dataLen)>; 35 #endif 36 enum class ServerThreadState { 37 JUST_STARTED, 38 ALREADY_STARTED, 39 CAN_NOT_START 40 }; 41 HilogInputSocketServer(HandlingFunc _packetHandler)42 explicit HilogInputSocketServer(HandlingFunc _packetHandler) 43 : DgramSocketServer(INPUT_SOCKET_NAME, MAX_SOCKET_PACKET_LEN), 44 m_packetHandler(_packetHandler), m_stopServer(false) 45 {} 46 47 ~HilogInputSocketServer(); 48 49 ServerThreadState RunServingThread(); 50 void StopServingThread(); 51 52 private: 53 void ServingThread(); 54 55 HandlingFunc m_packetHandler = nullptr; 56 std::thread m_serverThread; 57 std::atomic_bool m_stopServer; 58 }; 59 } // namespace HiviewDFX 60 } // namespace OHOS 61 #endif 62