• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "polled_socket.h"
18 
19 #include <fcntl.h>
20 #include <netdb.h>
21 #include <netinet/in.h>
22 #include <sys/socket.h>
23 #include <sys/types.h>
24 #include <sys/uio.h>
25 
26 #include "os/log.h"
27 
28 namespace test_vendor_lib {
29 namespace net {
30 
PolledSocket(int file_descriptor)31 PolledSocket::PolledSocket(int file_descriptor) : file_descriptor_(file_descriptor) {}
32 
PolledSocket(PolledSocket && p)33 PolledSocket::PolledSocket(PolledSocket&& p) : file_descriptor_(p.file_descriptor_) {
34   p.file_descriptor_ = -1;
35 }
36 
~PolledSocket()37 PolledSocket::~PolledSocket() {
38   CleanUp();
39 }
40 
CleanUp()41 void PolledSocket::CleanUp() {
42   if (file_descriptor_ != -1) {
43     WHILE_EINTR(close(file_descriptor_));
44   }
45   file_descriptor_ = -1;
46 }
47 
TrySend(const std::vector<uint8_t> & packet)48 size_t PolledSocket::TrySend(const std::vector<uint8_t>& packet) {
49   if (file_descriptor_ == -1) {
50     return 0;
51   }
52   int ret = write(file_descriptor_, packet.data(), packet.size());
53   if (ret == -1) {
54     LOG_WARN("%s error %s", __func__, strerror(errno));
55     return 0;
56   } else {
57     return static_cast<size_t>(ret);
58   }
59 }
60 
61 /*
62 void PolledSocket::TrySendVector(
63   const std::vector<std::vector<uint8_t>&>& raw_vectors) {
64 if (file_descriptor_ < 0) {
65   return;
66 }
67 for (const std::vector<uint8_t>& v : raw_vectors) {
68   Send(v);
69 }
70   std::vector<struct iovec> iovecs;
71   for (auto v : raw_vectors) {
72     struct iovec one_iovec;
73     one_iovec.iov_base = v.data();
74     one_iovec.iov_base = v.size();
75     iovecs.push_back(one_iovec);
76   }
77   int ret = writev(file_descriptor_, iovecs.data(), iovecs.size());
78   if (ret == -1) {
79     return 0;
80   } else {
81     return static_cast<size_t>(ret);
82   }
83 }
84 */
85 
TryReceive(size_t num_bytes,uint8_t * data)86 size_t PolledSocket::TryReceive(size_t num_bytes, uint8_t* data) {
87   if (file_descriptor_ == -1) return 0;
88   int ret;
89   WHILE_EINTR(ret = read(file_descriptor_, data, num_bytes));
90   if (ret < 0) {
91     if (errno == EAGAIN) {
92       return 0;
93     } else {
94       LOG_WARN("%s error %s", __func__, strerror(errno));
95       CleanUp();
96       return 0;
97     }
98   }
99   return ret;
100 }
101 
102 }  // namespace net
103 }  // namespace test_vendor_lib
104