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 #include "seq_packet_socket_server.h" 17 18 #include <cstring> 19 #include <iostream> 20 #include <thread> 21 22 namespace OHOS { 23 namespace HiviewDFX { StartAcceptingConnection(AcceptingHandler onAccepted)24int SeqPacketSocketServer::StartAcceptingConnection(AcceptingHandler onAccepted) 25 { 26 int listeningStatus = Listen(maxListenNumber); 27 if (listeningStatus < 0) { 28 #ifdef DEBUG 29 std::cerr << "Socket listen failed: "; 30 HilogPrintError(listeningStatus); 31 #endif 32 return listeningStatus; 33 } 34 35 return AcceptingLoop(onAccepted); 36 } 37 AcceptingLoop(AcceptingHandler func)38int SeqPacketSocketServer::AcceptingLoop(AcceptingHandler func) 39 { 40 int acceptedSockedFd = 0; 41 while ((acceptedSockedFd = Accept()) > 0) { 42 std::unique_ptr<Socket> handler = std::make_unique<Socket>(SOCK_SEQPACKET); 43 handler->setHandler(acceptedSockedFd); 44 func(std::move(handler)); 45 } 46 int acceptError = errno; 47 #ifdef DEBUG 48 std::cerr << "Socket accept failed: "; 49 HilogPrintError(acceptError); 50 #endif 51 52 return acceptError; 53 } 54 } // namespace HiviewDFX 55 } // namespace OHOS 56