blob: 5c574f7c5185a90d53bd5bc26ede5d93c4aabffd [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#pragma once
17
18#include <list>
19#include <string>
20
21#include "common/libs/fs/shared_fd.h"
Jorge E. Moreirad8430662018-07-22 23:52:33 -070022#include "host/commands/virtual_usb_manager/usbip/client.h"
23#include "host/commands/virtual_usb_manager/usbip/device_pool.h"
Greg Hartman92045f62017-10-31 17:12:24 -070024
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.
Greg Hartman153b1062017-11-11 12:09:21 -080039 void BeforeSelect(cvd::SharedFDSet* fd_read) const;
Greg Hartman92045f62017-10-31 17:12:24 -070040
41 // AfterSelect is Called right after Select() to detect and respond to changes
42 // on affected SharedFDs.
Greg Hartman153b1062017-11-11 12:09:21 -080043 void AfterSelect(const cvd::SharedFDSet& fd_read);
Greg Hartman92045f62017-10-31 17:12:24 -070044
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_;
Greg Hartman153b1062017-11-11 12:09:21 -080055 cvd::SharedFD server_;
Greg Hartman92045f62017-10-31 17:12:24 -070056 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