1 /*
2 * Copyright (C) 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 "host/libs/input_connector/input_connection.h"
18
19 #include "common/libs/fs/shared_buf.h"
20 #include "common/libs/fs/shared_fd.h"
21
22 namespace cuttlefish {
23 namespace {
24 class ServerInputConnection : public InputConnection {
25 public:
26 ServerInputConnection(SharedFD server);
27
28 Result<void> WriteEvents(const void* data, size_t len) override;
29
30 private:
31 SharedFD server_;
32 SharedFD client_;
33 std::mutex client_mtx_;
34 std::thread monitor_;
35
36 void MonitorLoop();
37 };
38
ServerInputConnection(SharedFD server)39 ServerInputConnection::ServerInputConnection(SharedFD server)
40 : server_(server), monitor_(std::thread([this]() { MonitorLoop(); })) {}
41
MonitorLoop()42 void ServerInputConnection::MonitorLoop() {
43 for (;;) {
44 client_ = SharedFD::Accept(*server_);
45 if (!client_->IsOpen()) {
46 LOG(ERROR) << "Failed to accept on input socket: " << client_->StrError();
47 continue;
48 }
49 do {
50 // Keep reading from the fd to detect when it closes.
51 char buf[128];
52 auto res = client_->Read(buf, sizeof(buf));
53 if (res < 0) {
54 LOG(ERROR) << "Failed to read from input client: "
55 << client_->StrError();
56 } else if (res > 0) {
57 LOG(VERBOSE) << "Received " << res << " bytes on input socket";
58 } else {
59 std::lock_guard<std::mutex> lock(client_mtx_);
60 client_->Close();
61 }
62 } while (client_->IsOpen());
63 }
64 }
65
WriteEvents(const void * data,size_t len)66 Result<void> ServerInputConnection::WriteEvents(const void* data, size_t len) {
67 std::lock_guard<std::mutex> lock(client_mtx_);
68 CF_EXPECT(client_->IsOpen(), "No input client connected");
69 auto res = WriteAll(client_, reinterpret_cast<const char*>(data), len);
70 CF_EXPECT(res == len, "Failed to write entire event buffer: wrote "
71 << res << " of " << len << "bytes");
72 return {};
73 }
74
75 } // namespace
76
NewServerInputConnection(SharedFD server_fd)77 std::unique_ptr<InputConnection> NewServerInputConnection(SharedFD server_fd) {
78 return std::unique_ptr<InputConnection>(new ServerInputConnection(server_fd));
79 }
80
81 } // namespace cuttlefish
82