1 /*
2  * Copyright 2023 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 
17 #include "common/libs/transport/channel_sharedfd.h"
18 
19 #include <poll.h>
20 #include <vector>
21 
22 #include "common/libs/fs/shared_buf.h"
23 
24 namespace cuttlefish {
25 namespace transport {
26 
SharedFdChannel(SharedFD input,SharedFD output)27 SharedFdChannel::SharedFdChannel(SharedFD input, SharedFD output)
28     : input_(std::move(input)), output_(std::move(output)) {}
29 
SendRequest(RawMessage & message)30 Result<void> SharedFdChannel::SendRequest(RawMessage& message) {
31   return SendMessage(message, false);
32 }
33 
SendResponse(RawMessage & message)34 Result<void> SharedFdChannel::SendResponse(RawMessage& message) {
35   return SendMessage(message, true);
36 }
37 
ReceiveMessage()38 Result<ManagedMessage> SharedFdChannel::ReceiveMessage() {
39   struct RawMessage message_header;
40   auto read = ReadExactBinary(input_, &message_header);
41   CF_EXPECT(read == sizeof(RawMessage),
42             "Expected " << sizeof(RawMessage) << ", received " << read << "\n"
43                         << "Could not read message: " << input_->StrError());
44   LOG(DEBUG) << "Received message with id: " << message_header.command;
45 
46   auto message = CF_EXPECT(CreateMessage(message_header.command,
47                                          message_header.is_response,
48                                          message_header.payload_size));
49   auto message_bytes = reinterpret_cast<char*>(message->payload);
50   read = ReadExact(input_, message_bytes, message->payload_size);
51   CF_EXPECT(read == message->payload_size,
52             "Could not read message: " << input_->StrError());
53 
54   return message;
55 }
56 
WaitForMessage()57 Result<int> SharedFdChannel::WaitForMessage() {
58   std::vector<PollSharedFd> input_poll = {
59       {.fd = input_, .events = POLLIN},
60   };
61   const int poll_result = SharedFD::Poll(input_poll, -1);
62 
63   CF_EXPECT(poll_result >= 0,
64             "Cannot execute poll on input stream to wait for incoming message");
65 
66   return poll_result;
67 }
68 
SendMessage(RawMessage & message,bool response)69 Result<void> SharedFdChannel::SendMessage(RawMessage& message, bool response) {
70   message.is_response = response;
71   auto write_size = sizeof(RawMessage) + message.payload_size;
72   auto message_bytes = reinterpret_cast<const char*>(&message);
73   auto written = WriteAll(output_, message_bytes, write_size);
74   CF_EXPECT(written == write_size,
75             "Could not write message: " << output_->StrError());
76   return {};
77 }
78 
79 }  // namespace transport
80 }  // namespace cuttlefish