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#pragma once
17
18#include <list>
19#include <string>
20
21#include "common/libs/fs/shared_fd.h"
22#include "host/libs/usbip/client.h"
23#include "host/libs/usbip/device_pool.h"
24
25namespace vadb {
26namespace usbip {
27
28class Server final {
29 public:
30  Server(const std::string& name, const DevicePool& device_pool);
31  ~Server() = default;
32
33  // Initialize this instance of Server.
34  // Returns true, if initialization was successful.
35  bool Init();
36
37  // BeforeSelect is Called right before Select() to populate interesting
38  // SharedFDs.
39  void BeforeSelect(cvd::SharedFDSet* fd_read) const;
40
41  // AfterSelect is Called right after Select() to detect and respond to changes
42  // on affected SharedFDs.
43  void AfterSelect(const cvd::SharedFDSet& fd_read);
44
45 private:
46  // Create USBIP server socket.
47  // Returns true, if socket was successfully created.
48  bool CreateServerSocket();
49
50  // Handle new client connection.
51  // New clients will be appended to clients_ list.
52  void HandleIncomingConnection();
53
54  std::string name_;
55  cvd::SharedFD server_;
56  std::list<Client> clients_;
57
58  const DevicePool& device_pool_;
59
60  Server(const Server&) = delete;
61  Server& operator=(const Server&) = delete;
62};
63
64}  // namespace usbip
65}  // namespace vadb
66