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 <vector>
20
21#include "absl/strings/string_view.h"
22
24
25namespace cuttlefish {
26
40ssize_t ReadExact(SharedFD fd, std::string* buf);
41
55ssize_t ReadExact(SharedFD fd, std::vector<char>* buf);
56
70ssize_t ReadExact(SharedFD fd, char* buf, size_t size);
71
72/*
73 * Reads from fd until reading `sizeof(T)` bytes or errors.
74 *
75 * On a successful read, returns `sizeof(T)`.
76 *
77 * If a read error is encountered, returns -1. buf will contain any data read
78 * up until that point and errno will be set.
79 */
80template <typename T>
81ssize_t ReadExactBinary(SharedFD fd, T* binary_data) {
82 return ReadExact(fd, (char*)binary_data, sizeof(*binary_data));
83}
84
99ssize_t WriteAll(SharedFD fd, absl::string_view buf);
100
115ssize_t WriteAll(SharedFD fd, const std::vector<char>& buf);
116
131ssize_t WriteAll(SharedFD fd, const char* buf, size_t size);
132
147template <typename T>
148ssize_t WriteAllBinary(SharedFD fd, const T* binary_data) {
149 return WriteAll(fd, (const char*)binary_data, sizeof(*binary_data));
150}
151
160bool SendAll(SharedFD sock, const void* buf, size_t size,
161 int flags = MSG_NOSIGNAL);
162
171bool SendAll(SharedFD sock, absl::string_view msg, int flags = MSG_NOSIGNAL);
172
173} // namespace cuttlefish
Definition: shared_fd.h:125
uint32_t size
Definition: io.h:2
Definition: alloc_driver.h:21
ssize_t WriteAll(SharedFD fd, const char *buf, size_t size)
Definition: shared_buf.cc:28
ssize_t WriteAllBinary(SharedFD fd, const T *binary_data)
Definition: shared_buf.h:148
ssize_t ReadExact(SharedFD fd, char *buf, size_t size)
Definition: shared_buf.cc:45
ssize_t ReadExactBinary(SharedFD fd, T *binary_data)
Definition: shared_buf.h:81
bool SendAll(SharedFD sock, const void *buf, size_t size, int flags)
Definition: shared_buf.cc:78