• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 #pragma once
17 
18 #include <string>
19 #include <thread>
20 #include <vector>
21 
22 #include "common/libs/fs/shared_fd.h"
23 
24 namespace cuttlefish {
25 
26 /**
27  * Reads from fd until it is closed or errors, storing all data in buf.
28  *
29  * On a successful read, returns the number of bytes read.
30  *
31  * If a read error is encountered, returns -1. buf will contain any data read
32  * up until that point and errno will be set.
33  */
34 ssize_t ReadAll(SharedFD fd, std::string* buf);
35 
36 /**
37  * Reads from fd until reading buf->size() bytes or errors.
38  *
39  * On a successful read, returns buf->size().
40  *
41  * If a read error is encountered, returns -1. buf will contain any data read
42  * up until that point and errno will be set.
43  */
44 ssize_t ReadExact(SharedFD fd, std::string* buf);
45 
46 /**
47  * Reads from fd until reading buf->size() bytes or errors.
48  *
49  * On a successful read, returns buf->size().
50  *
51  * If a read error is encountered, returns -1. buf will contain any data read
52  * up until that point and errno will be set.
53  */
54 ssize_t ReadExact(SharedFD fd, std::vector<char>* buf);
55 
56 /**
57  * Reads from fd until reading `size` bytes or errors.
58  *
59  * On a successful read, returns buf->size().
60  *
61  * If a read error is encountered, returns -1. buf will contain any data read
62  * up until that point and errno will be set.
63  */
64 ssize_t ReadExact(SharedFD fd, char* buf, size_t size);
65 
66 /*
67  * Reads from fd until reading `sizeof(T)` bytes or errors.
68  *
69  * On a successful read, returns `sizeof(T)`.
70  *
71  * If a read error is encountered, returns -1. buf will contain any data read
72  * up until that point and errno will be set.
73  */
74 template<typename T>
ReadExactBinary(SharedFD fd,T * binary_data)75 ssize_t ReadExactBinary(SharedFD fd, T* binary_data) {
76   return ReadExact(fd, (char*) binary_data, sizeof(*binary_data));
77 }
78 
79 /**
80  * Writes to fd until writing all bytes in buf.
81  *
82  * On a successful write, returns buf.size().
83  *
84  * If a write error is encountered, returns -1. Some data may have already been
85  * written to fd at that point.
86  */
87 ssize_t WriteAll(SharedFD fd, const std::string& buf);
88 
89 /**
90  * Writes to fd until writing all bytes in buf.
91  *
92  * On a successful write, returns buf.size().
93  *
94  * If a write error is encountered, returns -1. Some data may have already been
95  * written to fd at that point.
96  */
97 ssize_t WriteAll(SharedFD fd, const std::vector<char>& buf);
98 
99 /**
100  * Writes to fd until `size` bytes are written from `buf`.
101  *
102  * On a successful write, returns `size`.
103  *
104  * If a write error is encountered, returns -1. Some data may have already been
105  * written to fd at that point.
106  */
107 ssize_t WriteAll(SharedFD fd, const char* buf, size_t size);
108 
109 /**
110  * Writes to fd until `sizeof(T)` bytes are written from binary_data.
111  *
112  * On a successful write, returns `sizeof(T)`.
113  *
114  * If a write error is encountered, returns -1. Some data may have already been
115  * written to fd at that point.
116  */
117 template<typename T>
WriteAllBinary(SharedFD fd,const T * binary_data)118 ssize_t WriteAllBinary(SharedFD fd, const T* binary_data) {
119   return WriteAll(fd, (const char*) binary_data, sizeof(*binary_data));
120 }
121 
122 /**
123  * Sends contents of msg through sock, checking for socket error conditions
124  *
125  * On successful Send, returns true
126  *
127  * If a Send error is encountered, returns false. Some data may have already
128  * been written to 'sock' at that point.
129  */
130 bool SendAll(SharedFD sock, const std::string& msg);
131 
132 /**
133  * Receives 'count' bytes from sock, checking for socket error conditions
134  *
135  * On successful Recv, returns a string containing the received data
136  *
137  * If a Recv error is encountered, returns the empty string
138  */
139 std::string RecvAll(SharedFD sock, const size_t count);
140 
141 } // namespace cuttlefish
142