Browse Source

use shared_ptr for AddressReceiver

pull/177/head
orignal 10 years ago
parent
commit
bc21f5955f
  1. 26
      BOB.cpp
  2. 12
      BOB.h

26
BOB.cpp

@ -33,24 +33,22 @@ namespace client
void BOBI2PInboundTunnel::Accept () void BOBI2PInboundTunnel::Accept ()
{ {
auto receiver = new AddressReceiver (); auto receiver = std::make_shared<AddressReceiver> ();
receiver->socket = std::make_shared<boost::asio::ip::tcp::socket> (GetService ()); receiver->socket = std::make_shared<boost::asio::ip::tcp::socket> (GetService ());
m_Acceptor.async_accept (*receiver->socket, std::bind (&BOBI2PInboundTunnel::HandleAccept, this, m_Acceptor.async_accept (*receiver->socket, std::bind (&BOBI2PInboundTunnel::HandleAccept, this,
std::placeholders::_1, receiver)); std::placeholders::_1, receiver));
} }
void BOBI2PInboundTunnel::HandleAccept (const boost::system::error_code& ecode, AddressReceiver * receiver) void BOBI2PInboundTunnel::HandleAccept (const boost::system::error_code& ecode, std::shared_ptr<AddressReceiver> receiver)
{ {
if (!ecode) if (!ecode)
{ {
Accept (); Accept ();
ReceiveAddress (receiver); ReceiveAddress (receiver);
} }
else
delete receiver;
} }
void BOBI2PInboundTunnel::ReceiveAddress (AddressReceiver * receiver) void BOBI2PInboundTunnel::ReceiveAddress (std::shared_ptr<AddressReceiver> receiver)
{ {
receiver->socket->async_read_some (boost::asio::buffer( receiver->socket->async_read_some (boost::asio::buffer(
receiver->buffer + receiver->bufferOffset, receiver->buffer + receiver->bufferOffset,
@ -60,13 +58,10 @@ namespace client
} }
void BOBI2PInboundTunnel::HandleReceivedAddress (const boost::system::error_code& ecode, std::size_t bytes_transferred, void BOBI2PInboundTunnel::HandleReceivedAddress (const boost::system::error_code& ecode, std::size_t bytes_transferred,
AddressReceiver * receiver) std::shared_ptr<AddressReceiver> receiver)
{ {
if (ecode) if (ecode)
{
LogPrint ("BOB inbound tunnel read error: ", ecode.message ()); LogPrint ("BOB inbound tunnel read error: ", ecode.message ());
delete receiver;
}
else else
{ {
receiver->bufferOffset += bytes_transferred; receiver->bufferOffset += bytes_transferred;
@ -82,7 +77,6 @@ namespace client
if (!context.GetAddressBook ().GetIdentHash (receiver->buffer, ident)) if (!context.GetAddressBook ().GetIdentHash (receiver->buffer, ident))
{ {
LogPrint (eLogError, "BOB address ", receiver->buffer, " not found"); LogPrint (eLogError, "BOB address ", receiver->buffer, " not found");
delete receiver;
return; return;
} }
auto leaseSet = GetLocalDestination ()->FindLeaseSet (ident); auto leaseSet = GetLocalDestination ()->FindLeaseSet (ident);
@ -98,37 +92,29 @@ namespace client
if (receiver->bufferOffset < BOB_COMMAND_BUFFER_SIZE) if (receiver->bufferOffset < BOB_COMMAND_BUFFER_SIZE)
ReceiveAddress (receiver); ReceiveAddress (receiver);
else else
{
LogPrint ("BOB missing inbound address "); LogPrint ("BOB missing inbound address ");
delete receiver;
}
} }
} }
} }
void BOBI2PInboundTunnel::HandleDestinationRequestComplete (bool success, AddressReceiver * receiver, i2p::data::IdentHash ident) void BOBI2PInboundTunnel::HandleDestinationRequestComplete (bool success, std::shared_ptr<AddressReceiver> receiver, i2p::data::IdentHash ident)
{ {
if (success) if (success)
{ {
auto leaseSet = GetLocalDestination ()->FindLeaseSet (ident); auto leaseSet = GetLocalDestination ()->FindLeaseSet (ident);
if (leaseSet) if (leaseSet)
{
CreateConnection (receiver, leaseSet); CreateConnection (receiver, leaseSet);
return;
}
else else
LogPrint ("LeaseSet for BOB inbound destination not found"); LogPrint ("LeaseSet for BOB inbound destination not found");
} }
delete receiver;
} }
void BOBI2PInboundTunnel::CreateConnection (AddressReceiver * receiver, std::shared_ptr<const i2p::data::LeaseSet> leaseSet) void BOBI2PInboundTunnel::CreateConnection (std::shared_ptr<AddressReceiver> receiver, std::shared_ptr<const i2p::data::LeaseSet> leaseSet)
{ {
LogPrint ("New BOB inbound connection"); LogPrint ("New BOB inbound connection");
auto connection = std::make_shared<I2PTunnelConnection>(this, receiver->socket, leaseSet); auto connection = std::make_shared<I2PTunnelConnection>(this, receiver->socket, leaseSet);
AddHandler (connection); AddHandler (connection);
connection->I2PConnect (receiver->data, receiver->dataLen); connection->I2PConnect (receiver->data, receiver->dataLen);
delete receiver;
} }
BOBI2POutboundTunnel::BOBI2POutboundTunnel (const std::string& address, int port, BOBI2POutboundTunnel::BOBI2POutboundTunnel (const std::string& address, int port,

12
BOB.h

@ -59,7 +59,7 @@ namespace client
{ {
std::shared_ptr<boost::asio::ip::tcp::socket> socket; std::shared_ptr<boost::asio::ip::tcp::socket> socket;
char buffer[BOB_COMMAND_BUFFER_SIZE + 1]; // for destination base64 address char buffer[BOB_COMMAND_BUFFER_SIZE + 1]; // for destination base64 address
uint8_t * data; uint8_t * data; // pointer to buffer
size_t dataLen, bufferOffset; size_t dataLen, bufferOffset;
AddressReceiver (): data (nullptr), dataLen (0), bufferOffset (0) {}; AddressReceiver (): data (nullptr), dataLen (0), bufferOffset (0) {};
@ -76,15 +76,15 @@ namespace client
private: private:
void Accept (); void Accept ();
void HandleAccept (const boost::system::error_code& ecode, AddressReceiver * receiver); void HandleAccept (const boost::system::error_code& ecode, std::shared_ptr<AddressReceiver> receiver);
void ReceiveAddress (AddressReceiver * receiver); void ReceiveAddress (std::shared_ptr<AddressReceiver> receiver);
void HandleReceivedAddress (const boost::system::error_code& ecode, std::size_t bytes_transferred, void HandleReceivedAddress (const boost::system::error_code& ecode, std::size_t bytes_transferred,
AddressReceiver * receiver); std::shared_ptr<AddressReceiver> receiver);
void HandleDestinationRequestComplete (bool success, AddressReceiver * receiver, i2p::data::IdentHash ident); void HandleDestinationRequestComplete (bool success, std::shared_ptr<AddressReceiver> receiver, i2p::data::IdentHash ident);
void CreateConnection (AddressReceiver * receiver, std::shared_ptr<const i2p::data::LeaseSet> leaseSet); void CreateConnection (std::shared_ptr<AddressReceiver> receiver, std::shared_ptr<const i2p::data::LeaseSet> leaseSet);
private: private:

Loading…
Cancel
Save