Home | History | Annotate | Download | only in vadb
      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 
     17 #include "host/commands/virtual_usb_manager/vadb/virtual_adb_server.h"
     18 
     19 namespace vadb {
     20 
     21 void VirtualADBServer::BeforeSelect(cvd::SharedFDSet* fd_read) const {
     22   fd_read->Set(server_);
     23   for (const auto& client : clients_) {
     24     client.BeforeSelect(fd_read);
     25   }
     26 }
     27 
     28 void VirtualADBServer::AfterSelect(const cvd::SharedFDSet& fd_read) {
     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() {
     45   cvd::SharedFD client = cvd::SharedFD::Accept(*server_, nullptr, nullptr);
     46   if (!client->IsOpen()) {
     47     LOG(ERROR) << "Client connection failed: " << client->StrError();
     48     return;
     49   }
     50 
     51   clients_.emplace_back(&pool_, client, vhci_port_, usbip_name_);
     52 }
     53 
     54 }  // namespace vadb
     55