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#include "LocalSocket.h" 17 18#include <sys/ioctl.h> 19#include <sys/socket.h> 20#include <sys/types.h> 21#include <sys/un.h> 22 23#include "PreviewerEngineLog.h" 24 25LocalSocket::LocalSocket() : socketHandle(-1) {} 26 27LocalSocket::~LocalSocket() 28{ 29 DisconnectFromServer(); 30} 31 32bool LocalSocket::ConnectToServer(std::string name, OpenMode openMode, TransMode transMode) 33{ 34 (void)openMode; 35 (void)transMode; 36 struct sockaddr_un un; 37 un.sun_family = AF_UNIX; 38 std::size_t length = name.copy(un.sun_path, name.size()); 39 un.sun_path[length] = '\0'; 40 socketHandle = socket(AF_UNIX, SOCK_STREAM, 0); 41 if (socketHandle < 0) { 42 ELOG("Request socket failed"); 43 return false; 44 } 45 struct sockaddr* sockun = reinterpret_cast<struct sockaddr*>(&un); 46 if (connect(socketHandle, sockun, sizeof(un)) < 0) { 47 ELOG("connect socket failed"); 48 return false; 49 } 50 51 return true; 52} 53 54std::string LocalSocket::GetTracePipeName(std::string baseName) const 55{ 56 return std::string("/tmp/") + baseName; 57} 58 59std::string LocalSocket::GetCommandPipeName(std::string baseName) const 60{ 61 return std::string("/tmp/") + baseName + "_commandPipe"; 62} 63 64std::string LocalSocket::GetImagePipeName(std::string baseName) const 65{ 66 return std::string("/tmp/") + baseName + "_imagePipe"; 67} 68 69void LocalSocket::DisconnectFromServer() 70{ 71 shutdown(socketHandle, SHUT_RDWR); 72} 73 74int64_t LocalSocket::ReadData(char* data, size_t length) const 75{ 76 if (length > UINT32_MAX) { 77 ELOG("LocalSocket::ReadData length must < %d", UINT32_MAX); 78 return -1; 79 } 80 81 int32_t bytes_read; 82 ioctl(socketHandle, FIONREAD, &bytes_read); 83 84 if (bytes_read <= 0) { 85 return 0; 86 } 87 88 int32_t readSize = recv(socketHandle, data, length, 0); 89 if (readSize == 0) { 90 ELOG("LocalSocket::ReadData Server is shut down"); 91 } 92 93 if (readSize < 0) { 94 ELOG("LocalSocket::ReadData ReadFile failed"); 95 } 96 97 return readSize; 98} 99 100size_t LocalSocket::WriteData(const void* data, size_t length) const 101{ 102 if (length > UINT32_MAX) { 103 ELOG("LocalSocket::WriteData length must < %d", UINT32_MAX); 104 return 0; 105 } 106 std::string str((const char*)data); 107 ssize_t writeSize = send(socketHandle, str.c_str(), length, 0); 108 if (writeSize == 0) { 109 ELOG("LocalSocket::WriteData Server is shut down"); 110 } 111 112 if (writeSize < 0) { 113 ELOG("LocalSocket::WriteData ReadFile failed"); 114 } 115 116 return writeSize; 117} 118 119const LocalSocket& LocalSocket::operator>>(std::string& data) const 120{ 121 char c = '\255'; 122 while (c != '\0' && ReadData(&c, 1) > 0) { 123 data.push_back(c); 124 } 125 return *this; 126} 127 128const LocalSocket& LocalSocket::operator<<(const std::string data) const 129{ 130 WriteData(data.c_str(), data.length() + 1); 131 return *this; 132}