TYPE3
[iec.git] / src / type3_AndroidCloud / anbox-master / src / anbox / qemu / adb_message_processor.h
1 /*
2  * Copyright (C) 2016 Simon Fels <morphis@gravedo.de>
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 3, as published
6  * by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranties of
10  * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11  * PURPOSE.  See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program.  If not, see <http://www.gnu.org/licenses/>.
15  *
16  */
17
18 #ifndef ANBOX_QEMU_ADBD_MESSAGE_PROCESSOR_H_
19 #define ANBOX_QEMU_ADBD_MESSAGE_PROCESSOR_H_
20
21 #include "anbox/network/message_processor.h"
22 #include "anbox/network/socket_connection.h"
23 #include "anbox/network/socket_messenger.h"
24 #include "anbox/network/tcp_socket_connector.h"
25 #include "anbox/network/tcp_socket_messenger.h"
26 #include "anbox/runtime.h"
27
28 #include <boost/asio.hpp>
29
30 #include <mutex>
31
32 namespace anbox {
33 namespace qemu {
34 class AdbMessageProcessor : public network::MessageProcessor {
35  public:
36   AdbMessageProcessor(
37       const std::shared_ptr<Runtime> &rt,
38       const std::shared_ptr<network::SocketMessenger> &messenger);
39   ~AdbMessageProcessor();
40
41   bool process_data(const std::vector<std::uint8_t> &data) override;
42
43  private:
44   enum State {
45     waiting_for_guest_accept_command,
46     waiting_for_host_connection,
47     waiting_for_guest_start_command,
48     proxying_data,
49     closed_by_container,
50     closed_by_host,
51   };
52
53   void advance_state();
54
55   void wait_for_host_connection();
56   void on_host_connection(std::shared_ptr<boost::asio::basic_stream_socket<
57                               boost::asio::ip::tcp>> const &socket);
58   void read_next_host_message();
59   void on_host_read_size(const boost::system::error_code &error,
60                          std::size_t bytes_read);
61
62   std::shared_ptr<Runtime> runtime_;
63   State state_ = waiting_for_guest_accept_command;
64   std::string expected_command_;
65   std::shared_ptr<network::SocketMessenger> const messenger_;
66   std::vector<std::uint8_t> buffer_;
67   std::shared_ptr<network::TcpSocketConnector> host_connector_;
68   std::shared_ptr<network::TcpSocketMessenger> host_messenger_;
69   std::array<std::uint8_t, 8192> host_buffer_;
70   std::unique_lock<std::mutex> lock_;
71
72   static std::mutex active_instance_;
73 };
74 }  // namespace graphics
75 }  // namespace anbox
76
77 #endif