• 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 #define LOG_TAG "polled_socket"
18 
19 #include "polled_socket.h"
20 
21 #include <base/logging.h>
22 #include <fcntl.h>
23 #include <netdb.h>
24 #include <netinet/in.h>
25 #include <sys/socket.h>
26 #include <sys/types.h>
27 #include <sys/uio.h>
28 
29 #include "osi/include/log.h"
30 
31 namespace test_vendor_lib {
32 namespace net {
33 
PolledSocket(int file_descriptor)34 PolledSocket::PolledSocket(int file_descriptor) : file_descriptor_(file_descriptor) {}
35 
PolledSocket(PolledSocket && p)36 PolledSocket::PolledSocket(PolledSocket&& p) : file_descriptor_(p.file_descriptor_) {
37   p.file_descriptor_ = -1;
38 }
39 
~PolledSocket()40 PolledSocket::~PolledSocket() {
41   CleanUp();
42 }
43 
CleanUp()44 void PolledSocket::CleanUp() {
45   if (file_descriptor_ != -1) {
46     WHILE_EINTR(close(file_descriptor_));
47   }
48   file_descriptor_ = -1;
49 }
50 
TrySend(packets::PacketView<true> packet)51 size_t PolledSocket::TrySend(packets::PacketView<true> packet) {
52   if (file_descriptor_ == -1) {
53     return 0;
54   }
55   // Could skip this copy if the packet is guaranteed to be contiguous.
56   std::vector<uint8_t> copy;
57   copy.reserve(packet.size());
58   for (const auto&& c : packet) {
59     copy.push_back(c);
60   }
61   int ret = write(file_descriptor_, copy.data(), copy.size());
62   if (ret == -1) {
63     ALOGW("%s error %s", __func__, strerror(errno));
64     return 0;
65   } else {
66     return static_cast<size_t>(ret);
67   }
68 }
69 
70 /*
71 void PolledSocket::TrySendVector(
72   const std::vector<std::vector<uint8_t>&>& raw_vectors) {
73 if (file_descriptor_ < 0) {
74   return;
75 }
76 for (const std::vector<uint8_t>& v : raw_vectors) {
77   Send(v);
78 }
79   std::vector<struct iovec> iovecs;
80   for (auto v : raw_vectors) {
81     struct iovec one_iovec;
82     one_iovec.iov_base = v.data();
83     one_iovec.iov_base = v.size();
84     iovecs.push_back(one_iovec);
85   }
86   int ret = writev(file_descriptor_, iovecs.data(), iovecs.size());
87   if (ret == -1) {
88     return 0;
89   } else {
90     return static_cast<size_t>(ret);
91   }
92 }
93 */
94 
TryReceive(size_t num_bytes,uint8_t * data)95 size_t PolledSocket::TryReceive(size_t num_bytes, uint8_t* data) {
96   if (file_descriptor_ == -1) return 0;
97   int ret;
98   WHILE_EINTR(ret = read(file_descriptor_, data, num_bytes));
99   if (ret < 0) {
100     if (errno == EAGAIN) {
101       return 0;
102     } else {
103       ALOGW("%s error %s", __func__, strerror(errno));
104       CleanUp();
105       return 0;
106     }
107   }
108   return ret;
109 }
110 
111 }  // namespace net
112 }  // namespace test_vendor_lib
113