Greg Hartman | 92045f6 | 2017-10-31 17:12:24 -0700 | [diff] [blame] | 1 | /* |
| 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. Moreira | d843066 | 2018-07-22 23:52:33 -0700 | [diff] [blame] | 17 | #include "host/commands/virtual_usb_manager/vadb/virtual_adb_server.h" |
Greg Hartman | 92045f6 | 2017-10-31 17:12:24 -0700 | [diff] [blame] | 18 | |
| 19 | namespace vadb { |
| 20 | |
Greg Hartman | 153b106 | 2017-11-11 12:09:21 -0800 | [diff] [blame] | 21 | void VirtualADBServer::BeforeSelect(cvd::SharedFDSet* fd_read) const { |
Greg Hartman | 92045f6 | 2017-10-31 17:12:24 -0700 | [diff] [blame] | 22 | fd_read->Set(server_); |
Ryan Haining | dd0dcfe | 2018-05-16 13:55:20 -0700 | [diff] [blame] | 23 | for (const auto& client : clients_) { |
| 24 | client.BeforeSelect(fd_read); |
| 25 | } |
Greg Hartman | 92045f6 | 2017-10-31 17:12:24 -0700 | [diff] [blame] | 26 | } |
| 27 | |
Greg Hartman | 153b106 | 2017-11-11 12:09:21 -0800 | [diff] [blame] | 28 | void VirtualADBServer::AfterSelect(const cvd::SharedFDSet& fd_read) { |
Greg Hartman | 92045f6 | 2017-10-31 17:12:24 -0700 | [diff] [blame] | 29 | 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. |
| 44 | void VirtualADBServer::HandleIncomingConnection() { |
Greg Hartman | 153b106 | 2017-11-11 12:09:21 -0800 | [diff] [blame] | 45 | cvd::SharedFD client = cvd::SharedFD::Accept(*server_, nullptr, nullptr); |
Greg Hartman | 92045f6 | 2017-10-31 17:12:24 -0700 | [diff] [blame] | 46 | if (!client->IsOpen()) { |
| 47 | LOG(ERROR) << "Client connection failed: " << client->StrError(); |
| 48 | return; |
| 49 | } |
| 50 | |
Ryan Haining | dd0dcfe | 2018-05-16 13:55:20 -0700 | [diff] [blame] | 51 | clients_.emplace_back(&pool_, client, vhci_port_, usbip_name_); |
Greg Hartman | 92045f6 | 2017-10-31 17:12:24 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | } // namespace vadb |