587ffbbd69cdb71cdb6f898a522f0c404125369c
[iec.git] / src / type3_AndroidCloud / anbox-master / src / anbox / network / stream_socket_transport.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_STREAM_SOCKET_TRANSPORT_H_
19 #define ANBOX_STREAM_SOCKET_TRANSPORT_H_
20
21 #include "anbox/common/fd.h"
22 #include "anbox/network/socket_messenger.h"
23 #include "anbox/runtime.h"
24
25 #include <memory>
26
27 #include <boost/asio.hpp>
28
29 namespace anbox {
30 namespace network {
31 class StreamSocketTransport {
32  public:
33   class Observer {
34    public:
35     Observer() = default;
36     virtual ~Observer() = default;
37
38     virtual void on_data_available() = 0;
39     virtual void on_disconnected() = 0;
40
41     Observer(Observer const&) = delete;
42     Observer& operator=(Observer const&) = delete;
43   };
44
45   StreamSocketTransport(const std::string& socket_path,
46                         const std::shared_ptr<Runtime>& rt);
47
48   void register_observer(std::shared_ptr<Observer> const& observer_);
49   void unregister_observer(std::shared_ptr<Observer> const& observer_);
50
51   void send_message(std::vector<uint8_t> const& buffer);
52   void read_next_message();
53
54  private:
55   void on_read_size(const boost::system::error_code& ec,
56                     std::size_t bytes_read);
57
58   std::shared_ptr<Observer> observer_;
59   std::shared_ptr<boost::asio::local::stream_protocol::socket> socket_;
60   SocketMessenger messenger_;
61 };
62 }  // namespace network
63 }  // namespace anbox
64
65 #endif  // STREAM_SOCKET_TRANSPORT_H_