blob: 43dae7590618f057d7f6721227e44f198cb2ad87 [file] [log] [blame]
Greg Hartman92045f62017-10-31 17:12:24 -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
Jorge E. Moreirad8430662018-07-22 23:52:33 -070017#include "host/commands/virtual_usb_manager/vadb/virtual_adb_server.h"
Greg Hartman92045f62017-10-31 17:12:24 -070018
19namespace vadb {
20
Greg Hartman153b1062017-11-11 12:09:21 -080021void VirtualADBServer::BeforeSelect(cvd::SharedFDSet* fd_read) const {
Greg Hartman92045f62017-10-31 17:12:24 -070022 fd_read->Set(server_);
Ryan Hainingdd0dcfe2018-05-16 13:55:20 -070023 for (const auto& client : clients_) {
24 client.BeforeSelect(fd_read);
25 }
Greg Hartman92045f62017-10-31 17:12:24 -070026}
27
Greg Hartman153b1062017-11-11 12:09:21 -080028void VirtualADBServer::AfterSelect(const cvd::SharedFDSet& fd_read) {
Greg Hartman92045f62017-10-31 17:12:24 -070029 if (fd_read.IsSet(server_)) HandleIncomingConnection();
30
31 for (auto iter = clients_.begin(); iter != clients_.end();) {
32 if (!iter->AfterSelect(fd_read)) {
33 // If client conversation failed, hang up.
34 iter = clients_.erase(iter);
35 continue;
36 }
37 ++iter;
38 }
39}
40
41// Accept new QEmu connection. Add it to client pool.
42// Typically we will have no more than one QEmu connection, but the nature
43// of server requires proper handling nonetheless.
44void VirtualADBServer::HandleIncomingConnection() {
Greg Hartman153b1062017-11-11 12:09:21 -080045 cvd::SharedFD client = cvd::SharedFD::Accept(*server_, nullptr, nullptr);
Greg Hartman92045f62017-10-31 17:12:24 -070046 if (!client->IsOpen()) {
47 LOG(ERROR) << "Client connection failed: " << client->StrError();
48 return;
49 }
50
Ryan Hainingdd0dcfe2018-05-16 13:55:20 -070051 clients_.emplace_back(&pool_, client, vhci_port_, usbip_name_);
Greg Hartman92045f62017-10-31 17:12:24 -070052}
53
54} // namespace vadb