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 "socket.h"
17
18 #include <cerrno>
19 #include <sys/socket.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22 #include <iostream>
23
24 namespace OHOS {
25 namespace HiviewDFX {
Socket(int socketType)26 Socket::Socket(int socketType) : socketType(socketType) {}
27
SetType(uint32_t socketOption)28 void Socket::SetType(uint32_t socketOption)
29 {
30 socketType = socketOption;
31 }
32
GenerateFD()33 int Socket::GenerateFD()
34 {
35 int tmpFd = TEMP_FAILURE_RETRY(socket(AF_UNIX, socketType, 0));
36 int res = tmpFd;
37 if (tmpFd == 0) {
38 res = TEMP_FAILURE_RETRY(socket(AF_UNIX, socketType, 0));
39 close(tmpFd);
40 }
41 return res;
42 }
43
Create()44 int Socket::Create()
45 {
46 if (socketHandler != 0) {
47 return socketHandler;
48 }
49
50 int fd = TEMP_FAILURE_RETRY(socket(AF_UNIX, socketType, 0));
51 if (fd < 0) {
52 #ifdef DEBUG
53 std::cout << "Create socket failed: " << fd << std::endl;
54 #endif
55 return fd;
56 }
57
58 socketHandler = fd;
59 return socketHandler;
60 }
61
Poll()62 int Socket::Poll()
63 {
64 return -1;
65 }
66
Write(const char * data,unsigned int len)67 int Socket::Write(const char *data, unsigned int len)
68 {
69 if (data == nullptr) {
70 return -1;
71 }
72
73 return write(socketHandler, data, len);
74 }
75
WriteAll(const char * data,unsigned int len)76 int Socket::WriteAll(const char *data, unsigned int len)
77 {
78 const char *ptr = data;
79 int sizeLeft = static_cast<int>(len);
80 int midRes = 0;
81
82 if (data == nullptr) {
83 return -1;
84 }
85
86 while (sizeLeft > 0) {
87 midRes = Write(ptr, sizeLeft);
88 if (midRes < 0) {
89 break;
90 }
91 sizeLeft -= midRes;
92 ptr += midRes;
93 }
94
95 return (midRes < 0) ? midRes : len;
96 }
97
98
WriteV(const iovec * vec,unsigned int len)99 int Socket::WriteV(const iovec *vec, unsigned int len)
100 {
101 return TEMP_FAILURE_RETRY(::writev(socketHandler, vec, len));
102 }
103
Read(char * buffer,unsigned int len)104 int Socket::Read(char *buffer, unsigned int len)
105 {
106 return TEMP_FAILURE_RETRY(read(socketHandler, buffer, len));
107 }
108
Recv(void * buffer,unsigned int bufferLen,int flags)109 int Socket::Recv(void *buffer, unsigned int bufferLen, int flags)
110 {
111 return TEMP_FAILURE_RETRY(recv(socketHandler, buffer, bufferLen, flags));
112 }
113
setHandler(int handler)114 bool Socket::setHandler(int handler)
115 {
116 if (socketHandler > 0) {
117 return false;
118 }
119 socketHandler = handler;
120 return true;
121 }
122
closeHandler()123 bool Socket::closeHandler()
124 {
125 if (socketHandler > 0) {
126 close(socketHandler);
127 socketHandler = -1;
128 return true;
129 }
130 return false;
131 }
132
~Socket()133 Socket::~Socket()
134 {
135 close(socketHandler);
136 socketHandler = -1;
137 }
138 } // namespace HiviewDFX
139 } // namespace OHOS
140