Android-cuttlefish cvd tool
shared_buf.h
Go to the documentation of this file.
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 <string_view>
20#include <thread>
21#include <vector>
22
24
25namespace cuttlefish {
26
36ssize_t ReadAll(SharedFD fd, std::string* buf);
37
51ssize_t ReadExact(SharedFD fd, std::string* buf);
52
66ssize_t ReadExact(SharedFD fd, std::vector<char>* buf);
67
81ssize_t ReadExact(SharedFD fd, char* buf, size_t size);
82
83/*
84 * Reads from fd until reading `sizeof(T)` bytes or errors.
85 *
86 * On a successful read, returns `sizeof(T)`.
87 *
88 * If a read error is encountered, returns -1. buf will contain any data read
89 * up until that point and errno will be set.
90 */
91template<typename T>
92ssize_t ReadExactBinary(SharedFD fd, T* binary_data) {
93 return ReadExact(fd, (char*) binary_data, sizeof(*binary_data));
94}
95
110ssize_t WriteAll(SharedFD fd, std::string_view buf);
111
126ssize_t WriteAll(SharedFD fd, const std::vector<char>& buf);
127
142ssize_t WriteAll(SharedFD fd, const char* buf, size_t size);
143
158template<typename T>
159ssize_t WriteAllBinary(SharedFD fd, const T* binary_data) {
160 return WriteAll(fd, (const char*) binary_data, sizeof(*binary_data));
161}
162
171bool SendAll(SharedFD sock, std::string_view msg);
172
180std::string RecvAll(SharedFD sock, size_t count);
181
182} // namespace cuttlefish
Definition: shared_fd.h:129
uint32_t size
Definition: io.h:2
Definition: alloc_utils.cpp:23
bool SendAll(SharedFD sock, std::string_view msg)
Definition: shared_buf.cc:100
ssize_t WriteAll(SharedFD fd, const char *buf, size_t size)
Definition: shared_buf.cc:34
ssize_t WriteAllBinary(SharedFD fd, const T *binary_data)
Definition: shared_buf.h:159
ssize_t ReadAll(SharedFD fd, std::string *buf)
Definition: shared_buf.cc:68
ssize_t ReadExact(SharedFD fd, char *buf, size_t size)
Definition: shared_buf.cc:51
std::string RecvAll(SharedFD sock, const size_t count)
Definition: shared_buf.cc:116
ssize_t ReadExactBinary(SharedFD fd, T *binary_data)
Definition: shared_buf.h:92