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 <vector>
21
23
24namespace cuttlefish {
25
39ssize_t ReadExact(SharedFD fd, std::string* buf);
40
54ssize_t ReadExact(SharedFD fd, std::vector<char>* buf);
55
69ssize_t ReadExact(SharedFD fd, char* buf, size_t size);
70
71/*
72 * Reads from fd until reading `sizeof(T)` bytes or errors.
73 *
74 * On a successful read, returns `sizeof(T)`.
75 *
76 * If a read error is encountered, returns -1. buf will contain any data read
77 * up until that point and errno will be set.
78 */
79template <typename T>
80ssize_t ReadExactBinary(SharedFD fd, T* binary_data) {
81 return ReadExact(fd, (char*)binary_data, sizeof(*binary_data));
82}
83
98ssize_t WriteAll(SharedFD fd, std::string_view buf);
99
114ssize_t WriteAll(SharedFD fd, const std::vector<char>& buf);
115
130ssize_t WriteAll(SharedFD fd, const char* buf, size_t size);
131
146template <typename T>
147ssize_t WriteAllBinary(SharedFD fd, const T* binary_data) {
148 return WriteAll(fd, (const char*)binary_data, sizeof(*binary_data));
149}
150
159bool SendAll(SharedFD sock, std::string_view msg);
160
161} // namespace cuttlefish
Definition: shared_fd.h:124
uint32_t size
Definition: io.h:2
Definition: alloc_driver.h:20
bool SendAll(SharedFD sock, std::string_view msg)
Definition: shared_buf.cc:77
ssize_t WriteAll(SharedFD fd, const char *buf, size_t size)
Definition: shared_buf.cc:27
ssize_t WriteAllBinary(SharedFD fd, const T *binary_data)
Definition: shared_buf.h:147
ssize_t ReadExact(SharedFD fd, char *buf, size_t size)
Definition: shared_buf.cc:44
ssize_t ReadExactBinary(SharedFD fd, T *binary_data)
Definition: shared_buf.h:80