2 * Copyright (C) 2016 Simon Fels <morphis@gravedo.de>
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.
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.
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/>.
18 #include "anbox/network/tcp_socket_connector.h"
19 #include "anbox/network/connection_context.h"
20 #include "anbox/network/socket_helper.h"
24 TcpSocketConnector::TcpSocketConnector(
25 const boost::asio::ip::address_v4& address, unsigned short port,
26 const std::shared_ptr<Runtime>& rt,
27 const std::shared_ptr<ConnectionCreator<boost::asio::ip::tcp>>&
32 connection_creator_(connection_creator),
33 acceptor_(rt->service(), boost::asio::ip::tcp::endpoint(address, port)) {
37 TcpSocketConnector::~TcpSocketConnector() noexcept { acceptor_.cancel(); }
39 void TcpSocketConnector::start_accept() {
41 std::make_shared<boost::asio::ip::tcp::socket>(runtime_->service());
43 acceptor_.async_accept(*socket,
44 [this, socket](boost::system::error_code const& err) {
45 on_new_connection(socket, err);
49 void TcpSocketConnector::on_new_connection(
50 std::shared_ptr<boost::asio::ip::tcp::socket> const& socket,
51 boost::system::error_code const& err) {
52 switch (err.value()) {
53 case boost::system::errc::success:
54 connection_creator_->create_connection_for(socket);
57 // Socket was closed so don't listen for any further incoming
58 // connection attempts.
64 } // namespace network