blob: 45ba98278de677376a7b38c45248d7bb7160294e [file] [log] [blame]
Andreas Huber28d35912017-03-24 13:14:11 -07001/*
2 * Copyright (C) 2017 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
Steven Moreland0b8e3872019-06-25 08:54:34 -070017#pragma once
Andreas Huber28d35912017-03-24 13:14:11 -070018
Yifan Hong891989f2021-06-17 18:31:42 -070019#include <thread>
20
Andreas Huber28d35912017-03-24 13:14:11 -070021#include <android-base/macros.h>
Yifan Hong891989f2021-06-17 18:31:42 -070022#include <android-base/result.h>
23#include <android-base/unique_fd.h>
Andreas Huber28d35912017-03-24 13:14:11 -070024#include <utils/Errors.h>
25#include <utils/RefBase.h>
Yifan Hong891989f2021-06-17 18:31:42 -070026#include <ostream>
Andreas Huber28d35912017-03-24 13:14:11 -070027
Yifan Hong8a662472020-09-24 11:50:50 -070028#include "NullableOStream.h"
29
Andreas Huber28d35912017-03-24 13:14:11 -070030namespace android {
31namespace lshal {
32
Yifan Hong891989f2021-06-17 18:31:42 -070033/**
34 * Creates a pipe and spawns a thread that relays any data
Andreas Huber28d35912017-03-24 13:14:11 -070035 * written to the "write"-end of the pair to the specified output stream "os".
36 */
37struct PipeRelay {
Yifan Hong891989f2021-06-17 18:31:42 -070038 static android::base::Result<std::unique_ptr<PipeRelay>> create(
39 std::ostream& os, const NullableOStream<std::ostream>& err, const std::string& fqName);
Andreas Huber28d35912017-03-24 13:14:11 -070040 ~PipeRelay();
41
Andreas Huber28d35912017-03-24 13:14:11 -070042 // Returns the file descriptor corresponding to the "write"-end of the
43 // connection.
Yifan Hong891989f2021-06-17 18:31:42 -070044 android::base::borrowed_fd fd() const { return mWrite; }
Andreas Huber28d35912017-03-24 13:14:11 -070045
46private:
Yifan Hong891989f2021-06-17 18:31:42 -070047 PipeRelay() = default;
Andreas Huber28d35912017-03-24 13:14:11 -070048 DISALLOW_COPY_AND_ASSIGN(PipeRelay);
Yifan Hong891989f2021-06-17 18:31:42 -070049 static void thread(android::base::unique_fd rfd, android::base::unique_fd rfdTrigger,
50 std::ostream* out, const NullableOStream<std::ostream>* err,
51 std::string fqName);
52
53 android::base::unique_fd mWrite;
54 android::base::unique_fd mWriteTrigger;
55 std::unique_ptr<std::thread> mThread;
Andreas Huber28d35912017-03-24 13:14:11 -070056};
57
58} // namespace lshal
59} // namespace android