TYPE3
[iec.git] / src / type3_AndroidCloud / anbox-master / src / anbox / network / published_socket_connector.cpp
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 #include "anbox/network/published_socket_connector.h"
19 #include "anbox/network/connection_context.h"
20 #include "anbox/network/socket_helper.h"
21 #include "anbox/logger.h"
22
23 namespace anbox {
24 namespace network {
25 PublishedSocketConnector::PublishedSocketConnector(
26     const std::string& socket_file, const std::shared_ptr<Runtime>& rt,
27     const std::shared_ptr<ConnectionCreator<
28         boost::asio::local::stream_protocol>>& connection_creator)
29     : socket_file_(remove_socket_if_stale(socket_file)),
30       runtime_(rt),
31       connection_creator_(connection_creator),
32       acceptor_(rt->service(), socket_file_) {
33   start_accept();
34 }
35
36 PublishedSocketConnector::~PublishedSocketConnector() noexcept {}
37
38 void PublishedSocketConnector::start_accept() {
39   auto socket = std::make_shared<boost::asio::local::stream_protocol::socket>(runtime_->service());
40
41   acceptor_.async_accept(*socket,
42                          [this, socket](boost::system::error_code const& err) {
43                            on_new_connection(socket, err);
44                          });
45 }
46
47 void PublishedSocketConnector::on_new_connection(std::shared_ptr<boost::asio::local::stream_protocol::socket> const& socket,
48                                                  boost::system::error_code const& err) {
49   if (!err)
50     connection_creator_->create_connection_for(socket);
51
52   if (err.value() == boost::asio::error::operation_aborted)
53     return;
54
55   start_accept();
56 }
57 }  // namespace network
58 }  // namespace anbox