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