TYPE3
[iec.git] / src / type3_AndroidCloud / anbox-master / src / anbox / network / base_socket_messenger.h
1 /*
2  * Copyright © 2013-2014 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 3 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Kevin DuBois <kevin.dubois@canonical.com>
17  */
18
19 #ifndef ANBOX_NETWORK_BASE_SOCKET_MESSENGER_H_
20 #define ANBOX_NETWORK_BASE_SOCKET_MESSENGER_H_
21
22 #include "anbox/common/fd_sets.h"
23 #include "anbox/network/socket_messenger.h"
24
25 #include <boost/asio.hpp>
26 #include <mutex>
27
28 namespace anbox {
29 namespace network {
30 template <typename stream_protocol>
31 class BaseSocketMessenger : public SocketMessenger {
32  public:
33   BaseSocketMessenger(
34       std::shared_ptr<boost::asio::basic_stream_socket<stream_protocol>> const&
35           socket);
36   virtual ~BaseSocketMessenger();
37
38   Credentials creds() const override;
39   unsigned short local_port() const override;
40
41   void send(char const* data, size_t length) override;
42   ssize_t send_raw(char const* data, size_t length) override;
43   void async_receive_msg(AnboxReadHandler const& handle,
44                          boost::asio::mutable_buffers_1 const& buffer) override;
45   boost::system::error_code receive_msg(
46       boost::asio::mutable_buffers_1 const& buffer) override;
47   size_t available_bytes() override;
48
49   void set_no_delay() override;
50   void close() override;
51
52  protected:
53   BaseSocketMessenger();
54   void setup(std::shared_ptr<
55              boost::asio::basic_stream_socket<stream_protocol>> const& s);
56
57  private:
58   std::shared_ptr<boost::asio::basic_stream_socket<stream_protocol>> socket;
59   anbox::Fd socket_fd;
60   std::mutex message_lock;
61 };
62 }  // namespace network
63 }  // namespace anbox
64
65 #endif