1 // Copyright 2021 Google LLC
2 //
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 #include "net_sockio.h"
16 
17 #include <sys/socket.h>
18 #include <unistd.h>
19 
20 namespace net
21 {
22 
close()23 int SockIO::close()
24 {
25     int ret = 0;
26     if (sockfd_ >= 0)
27     {
28         ret = ::close(sockfd_);
29         sockfd_ = -1;
30     }
31 
32     return ret;
33 }
34 
write(const void * buf,size_t len)35 int SockIO::write(const void* buf, size_t len)
36 {
37     return ::write(sockfd_, buf, len);
38 }
39 
recv(void * buf,size_t maxlen)40 int SockIO::recv(void* buf, size_t maxlen)
41 {
42     return ::recv(sockfd_, buf, maxlen, 0);
43 }
44 
~SockIO()45 SockIO::~SockIO()
46 {
47     SockIO::close();
48 }
49 
50 } // namespace net
51