forked from stellar/stellar-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPeerDoor.cpp
83 lines (74 loc) · 2.1 KB
/
PeerDoor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2014 Stellar Development Foundation and contributors. Licensed
// under the Apache License, Version 2.0. See the COPYING file at the root
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0
#include "PeerDoor.h"
#include "Peer.h"
#include "main/Application.h"
#include "main/Config.h"
#include "overlay/OverlayManager.h"
#include "overlay/TCPPeer.h"
#include "util/Logging.h"
#include <memory>
namespace stellar
{
using asio::ip::tcp;
using namespace std;
PeerDoor::PeerDoor(Application& app)
: mApp(app), mAcceptor(mApp.getClock().getIOContext())
{
}
void
PeerDoor::start()
{
if (!mApp.getConfig().RUN_STANDALONE)
{
tcp::endpoint endpoint(tcp::v4(), mApp.getConfig().PEER_PORT);
CLOG(INFO, "Overlay") << "Binding to endpoint " << endpoint;
mAcceptor.open(endpoint.protocol());
mAcceptor.set_option(asio::ip::tcp::acceptor::reuse_address(true));
mAcceptor.bind(endpoint);
mAcceptor.listen();
acceptNextPeer();
}
}
void
PeerDoor::close()
{
if (mAcceptor.is_open())
{
asio::error_code ec;
// ignore errors when closing
mAcceptor.close(ec);
}
}
void
PeerDoor::acceptNextPeer()
{
if (mApp.getOverlayManager().isShuttingDown())
{
return;
}
CLOG(DEBUG, "Overlay") << "PeerDoor acceptNextPeer()";
auto sock =
make_shared<TCPPeer::SocketType>(mApp.getClock().getIOContext());
mAcceptor.async_accept(sock->next_layer(),
[this, sock](asio::error_code const& ec) {
if (ec)
this->acceptNextPeer();
else
this->handleKnock(sock);
});
}
void
PeerDoor::handleKnock(shared_ptr<TCPPeer::SocketType> socket)
{
CLOG(DEBUG, "Overlay") << "PeerDoor handleKnock() @"
<< mApp.getConfig().PEER_PORT;
Peer::pointer peer = TCPPeer::accept(mApp, socket);
if (peer)
{
mApp.getOverlayManager().addInboundConnection(peer);
}
acceptNextPeer();
}
}