Cache the result of remote_endpoint at connect/accept time and use it

This commit is contained in:
Nik Bougalis
2014-03-05 12:57:15 -08:00
parent e8002a7a62
commit 9c465487ae
6 changed files with 29 additions and 45 deletions

View File

@@ -61,8 +61,9 @@ public:
mAcceptor.set_option (boost::asio::ip::tcp::acceptor::reuse_address (true));
mAcceptor.async_accept (new_connection->getRawSocket (),
boost::bind (&RPCDoorImp::handleConnect, this, new_connection,
boost::asio::placeholders::error));
new_connection->getRemoteEndpoint (),
boost::bind (&RPCDoorImp::handleConnect, this,
new_connection, boost::asio::placeholders::error));
}
//--------------------------------------------------------------------------
@@ -82,25 +83,19 @@ public:
//--------------------------------------------------------------------------
void handleConnect (RPCServerImp::pointer new_connection, boost::system::error_code const& error)
void handleConnect (RPCServerImp::pointer new_connection,
boost::system::error_code const& error)
{
bool delay = false;
if (!error)
{
// Restrict callers by IP
// VFALCO NOTE Prevent exceptions from being thrown at all.
try
std::string client_ip (
new_connection->getRemoteEndpoint ().address ().to_string ());
if (! isClientAllowed (client_ip))
{
if (! isClientAllowed (new_connection->getRemoteAddressText ()))
{
startListening ();
return;
}
}
catch (...)
{
// client may have disconnected
startListening ();
return;
}

View File

@@ -55,15 +55,9 @@ public:
*/
virtual void connected () = 0;
// VFALCO TODO Remove this since it exposes boost
// VFALCO TODO Remove these since they expose boost
virtual boost::asio::ip::tcp::socket& getRawSocket () = 0;
/** Retrieve the remote address as a string.
@return A std::string representing the remote address.
*/
// VFALCO TODO Replace the return type with a dedicated class.
virtual std::string getRemoteAddressText () = 0;
virtual boost::asio::ip::tcp::socket::endpoint_type& getRemoteEndpoint () = 0;
};
#endif

View File

@@ -219,8 +219,9 @@ public:
{
WriteLog (lsTRACE, RPCServer) << "handleRequest " << request;
return m_handler.processRequest (request, beast::IPAddressConversion::from_asio (
mSocket.PlainSocket ().remote_endpoint().address()));
return m_handler.processRequest (request,
beast::IPAddressConversion::from_asio (
m_remote_endpoint.address()));
}
//--------------------------------------------------------------------------
@@ -237,15 +238,9 @@ public:
return mSocket.PlainSocket ();
}
//--------------------------------------------------------------------------
std::string getRemoteAddressText ()
boost::asio::ip::tcp::socket::endpoint_type& getRemoteEndpoint ()
{
std::string address;
address = mSocket.PlainSocket ().remote_endpoint ().address ().to_string ();
return address;
return m_remote_endpoint;
}
private:
@@ -253,6 +248,7 @@ private:
boost::asio::io_service::strand mStrand;
AutoSocket mSocket;
AutoSocket::endpoint_type m_remote_endpoint;
boost::asio::streambuf mLineBuffer;
Blob mQueryVec;

View File

@@ -168,6 +168,7 @@ public:
/** New incoming peer from the specified socket */
PeerImp (
boost::shared_ptr <NativeSocketType> const& socket,
IP::Endpoint remoteAddress,
Peers& peers,
Resource::Manager& resourceManager,
PeerFinder::Manager& peerFinder,
@@ -177,6 +178,7 @@ public:
: m_shared_socket (socket)
, m_journal (LogPartition::getJournal <Peer> ())
, m_shortId (0)
, m_remoteAddress (remoteAddress)
, m_resourceManager (resourceManager)
, m_peerFinder (peerFinder)
, m_peers (peers)
@@ -202,6 +204,7 @@ public:
from inside constructors.
*/
PeerImp (
IP::Endpoint remoteAddress,
boost::asio::io_service& io_service,
Peers& peers,
Resource::Manager& resourceManager,
@@ -211,6 +214,7 @@ public:
MultiSocket::Flag flags)
: m_journal (LogPartition::getJournal <Peer> ())
, m_shortId (0)
, m_remoteAddress (remoteAddress)
, m_resourceManager (resourceManager)
, m_peerFinder (peerFinder)
, m_peers (peers)
@@ -274,10 +278,8 @@ public:
object and begins the process of connection establishment instead
of requiring the caller to construct a Peer and call connect.
*/
void connect (IP::Endpoint const& address)
void connect ()
{
m_remoteAddress = address;
m_journal.info << "Connecting to " << m_remoteAddress;
boost::system::error_code err;
@@ -295,7 +297,7 @@ public:
}
getNativeSocket ().async_connect (
IPAddressConversion::to_asio_endpoint (address),
IPAddressConversion::to_asio_endpoint (m_remoteAddress),
m_strand.wrap (boost::bind (&PeerImp::onConnect,
shared_from_this (), boost::asio::placeholders::error)));
}
@@ -425,8 +427,6 @@ public:
*/
void accept ()
{
m_remoteAddress = m_socket->remote_endpoint();
m_journal.info << "Accepted " << m_remoteAddress;
m_socket->set_verify_mode (boost::asio::ssl::verify_none);
@@ -776,8 +776,6 @@ private:
return;
}
m_remoteAddress = m_socket->remote_endpoint();
if (m_inbound)
m_usage = m_resourceManager.newInboundEndpoint (m_remoteAddress);
else

View File

@@ -206,7 +206,7 @@ public:
flags = flags.with (MultiSocket::Flag::proxy);
PeerImp::ptr const peer (boost::make_shared <PeerImp> (
socket, *this, m_resourceManager, *m_peerFinder,
socket, remote_endpoint, *this, m_resourceManager, *m_peerFinder,
slot, m_ssl_context, flags));
{
@@ -244,8 +244,8 @@ public:
MultiSocket::Flag::client_role | MultiSocket::Flag::ssl);
PeerImp::ptr const peer (boost::make_shared <PeerImp> (
m_io_service, *this, m_resourceManager, *m_peerFinder,
slot, m_ssl_context, flags));
remote_endpoint, m_io_service, *this, m_resourceManager,
*m_peerFinder, slot, m_ssl_context, flags));
{
std::lock_guard <decltype(m_mutex)> lock (m_mutex);
@@ -258,7 +258,7 @@ public:
// This has to happen while holding the lock,
// otherwise the socket might not be canceled during a stop.
peer->connect (remote_endpoint);
peer->connect ();
}
}

View File

@@ -29,6 +29,7 @@ class AutoSocket
{
public:
typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssl_socket;
typedef boost::asio::ip::tcp::socket::endpoint_type endpoint_type;
typedef boost::shared_ptr<ssl_socket> socket_ptr;
typedef ssl_socket::next_layer_type plain_socket;
typedef ssl_socket::lowest_layer_type lowest_layer_type;