void OverlayImpl::accept (bool proxyHandshake, socket_type&& socket)
{
    // An error getting an endpoint means the connection closed.
    // Just do nothing and the socket will be closed by the caller.
    boost::system::error_code ec;
    auto const local_endpoint_native (socket.local_endpoint (ec));
    if (ec)
        return;
    auto const remote_endpoint_native (socket.remote_endpoint (ec));
    if (ec)
        return;

    auto const local_endpoint (
        beast::IPAddressConversion::from_asio (local_endpoint_native));
    auto const remote_endpoint (
        beast::IPAddressConversion::from_asio (remote_endpoint_native));

    PeerFinder::Slot::ptr const slot (m_peerFinder->new_inbound_slot (
        local_endpoint, remote_endpoint));

    if (slot == nullptr)
        return;

    MultiSocket::Flag flags (
        MultiSocket::Flag::server_role | MultiSocket::Flag::ssl_required);

    if (proxyHandshake)
        flags = flags.with (MultiSocket::Flag::proxy);

    PeerImp::ptr const peer (boost::make_shared <PeerImp> (
        std::move (socket), remote_endpoint, *this, m_resourceManager,
            *m_peerFinder, slot, m_ssl_context, flags));

    {
        std::lock_guard <decltype(m_mutex)> lock (m_mutex);
        {
            std::pair <PeersBySlot::iterator, bool> const result (
                m_peers.emplace (slot, peer));
            assert (result.second);
        }
        ++m_child_count;

        // This has to happen while holding the lock,
        // otherwise the socket might not be canceled during a stop.
        peer->start ();
    }
}
Example #2
0
void
OverlayImpl::connect (beast::IP::Endpoint const& remote_endpoint)
{
    if (isStopping())
    {
        m_journal.debug <<
                        "Skipping " << remote_endpoint <<
                        " connect on stop";
        return;
    }

    PeerFinder::Slot::ptr const slot (
        m_peerFinder->new_outbound_slot (remote_endpoint));

    if (slot == nullptr)
        return;

    MultiSocket::Flag const flags (
        MultiSocket::Flag::client_role | MultiSocket::Flag::ssl);

    PeerImp::ptr const peer (std::make_shared <PeerImp> (
                                 remote_endpoint, m_io_service, *this, m_resourceManager,
                                 *m_peerFinder, slot, m_ssl_context, flags));

    {
        std::lock_guard <decltype(m_mutex)> lock (m_mutex);
        {
            std::pair <PeersBySlot::iterator, bool> const result (
                m_peers.emplace (slot, peer));
            assert (result.second);
        }
        ++m_child_count;

        // This has to happen while holding the lock,
        // otherwise the socket might not be canceled during a stop.
        peer->start ();
    }
}