Browse Source

move RunnableService away from LeaseSetDestination

pull/1474/head
orignal 5 years ago
parent
commit
2d154ee640
  1. 76
      libi2pd/Destination.cpp
  2. 20
      libi2pd/Destination.h
  3. 2
      libi2pd/util.h
  4. 27
      libi2pd_client/I2CP.cpp
  5. 7
      libi2pd_client/I2CP.h
  6. 18
      libi2pd_client/MatchedDestination.cpp
  7. 4
      libi2pd_client/MatchedDestination.h

76
libi2pd/Destination.cpp

@ -7,15 +7,15 @@
#include "Timestamp.h" #include "Timestamp.h"
#include "NetDb.hpp" #include "NetDb.hpp"
#include "Destination.h" #include "Destination.h"
#include "util.h"
namespace i2p namespace i2p
{ {
namespace client namespace client
{ {
LeaseSetDestination::LeaseSetDestination (bool isPublic, const std::map<std::string, std::string> * params): LeaseSetDestination::LeaseSetDestination (boost::asio::io_service& service,
m_IsRunning (false), m_Thread (nullptr), m_IsPublic (isPublic), bool isPublic, const std::map<std::string, std::string> * params):
m_PublishReplyToken (0), m_LastSubmissionTime (0), m_PublishConfirmationTimer (m_Service), m_Service (service), m_IsPublic (isPublic), m_PublishReplyToken (0),
m_LastSubmissionTime (0), m_PublishConfirmationTimer (m_Service),
m_PublishVerificationTimer (m_Service), m_PublishDelayTimer (m_Service), m_CleanupTimer (m_Service), m_PublishVerificationTimer (m_Service), m_PublishDelayTimer (m_Service), m_CleanupTimer (m_Service),
m_LeaseSetType (DEFAULT_LEASESET_TYPE), m_AuthType (i2p::data::ENCRYPTED_LEASESET_AUTH_TYPE_NONE) m_LeaseSetType (DEFAULT_LEASESET_TYPE), m_AuthType (i2p::data::ENCRYPTED_LEASESET_AUTH_TYPE_NONE)
{ {
@ -123,77 +123,36 @@ namespace client
LeaseSetDestination::~LeaseSetDestination () LeaseSetDestination::~LeaseSetDestination ()
{ {
if (m_IsRunning)
Stop ();
if (m_Pool) if (m_Pool)
i2p::tunnel::tunnels.DeleteTunnelPool (m_Pool); i2p::tunnel::tunnels.DeleteTunnelPool (m_Pool);
for (auto& it: m_LeaseSetRequests) for (auto& it: m_LeaseSetRequests)
it.second->Complete (nullptr); it.second->Complete (nullptr);
} }
void LeaseSetDestination::Run () void LeaseSetDestination::Start ()
{
while (m_IsRunning)
{
try
{
m_Service.run ();
}
catch (std::exception& ex)
{
LogPrint (eLogError, "Destination: runtime exception: ", ex.what ());
}
}
}
bool LeaseSetDestination::Start ()
{
if (!m_IsRunning)
{ {
if (m_Nickname.empty ()) if (m_Nickname.empty ())
m_Nickname = i2p::data::GetIdentHashAbbreviation (GetIdentHash ()); // set default nickname m_Nickname = i2p::data::GetIdentHashAbbreviation (GetIdentHash ()); // set default nickname
LoadTags (); LoadTags ();
m_IsRunning = true;
m_Pool->SetLocalDestination (shared_from_this ()); m_Pool->SetLocalDestination (shared_from_this ());
m_Pool->SetActive (true); m_Pool->SetActive (true);
m_CleanupTimer.expires_from_now (boost::posix_time::minutes (DESTINATION_CLEANUP_TIMEOUT)); m_CleanupTimer.expires_from_now (boost::posix_time::minutes (DESTINATION_CLEANUP_TIMEOUT));
m_CleanupTimer.async_wait (std::bind (&LeaseSetDestination::HandleCleanupTimer, m_CleanupTimer.async_wait (std::bind (&LeaseSetDestination::HandleCleanupTimer,
shared_from_this (), std::placeholders::_1)); shared_from_this (), std::placeholders::_1));
m_Thread = new std::thread (std::bind (&LeaseSetDestination::Run, shared_from_this ()));
return true;
}
else
return false;
} }
bool LeaseSetDestination::Stop () void LeaseSetDestination::Stop ()
{
if (m_IsRunning)
{ {
m_CleanupTimer.cancel (); m_CleanupTimer.cancel ();
m_PublishConfirmationTimer.cancel (); m_PublishConfirmationTimer.cancel ();
m_PublishVerificationTimer.cancel (); m_PublishVerificationTimer.cancel ();
m_IsRunning = false;
if (m_Pool) if (m_Pool)
{ {
m_Pool->SetLocalDestination (nullptr); m_Pool->SetLocalDestination (nullptr);
i2p::tunnel::tunnels.StopTunnelPool (m_Pool); i2p::tunnel::tunnels.StopTunnelPool (m_Pool);
} }
m_Service.stop ();
if (m_Thread)
{
m_Thread->join ();
delete m_Thread;
m_Thread = 0;
}
SaveTags (); SaveTags ();
CleanUp (); // GarlicDestination CleanUp (); // GarlicDestination
return true;
}
else
return false;
} }
bool LeaseSetDestination::Reconfigure(std::map<std::string, std::string> params) bool LeaseSetDestination::Reconfigure(std::map<std::string, std::string> params)
@ -864,7 +823,8 @@ namespace client
} }
ClientDestination::ClientDestination (const i2p::data::PrivateKeys& keys, bool isPublic, const std::map<std::string, std::string> * params): ClientDestination::ClientDestination (const i2p::data::PrivateKeys& keys, bool isPublic, const std::map<std::string, std::string> * params):
LeaseSetDestination (isPublic, params), m_Keys (keys), m_StreamingAckDelay (DEFAULT_INITIAL_ACK_DELAY), RunnableService ("Destination"), LeaseSetDestination (GetService (), isPublic, params),
m_Keys (keys), m_StreamingAckDelay (DEFAULT_INITIAL_ACK_DELAY),
m_DatagramDestination (nullptr), m_RefCounter (0), m_DatagramDestination (nullptr), m_RefCounter (0),
m_ReadyChecker(GetService()) m_ReadyChecker(GetService())
{ {
@ -932,26 +892,28 @@ namespace client
ClientDestination::~ClientDestination () ClientDestination::~ClientDestination ()
{ {
if (IsRunning ())
Stop ();
} }
bool ClientDestination::Start () void ClientDestination::Start ()
{ {
if (LeaseSetDestination::Start ()) if (!IsRunning ())
{ {
LeaseSetDestination::Start ();
m_StreamingDestination = std::make_shared<i2p::stream::StreamingDestination> (GetSharedFromThis ()); // TODO: m_StreamingDestination = std::make_shared<i2p::stream::StreamingDestination> (GetSharedFromThis ()); // TODO:
m_StreamingDestination->Start (); m_StreamingDestination->Start ();
for (auto& it: m_StreamingDestinationsByPorts) for (auto& it: m_StreamingDestinationsByPorts)
it.second->Start (); it.second->Start ();
return true; StartService ();
} }
else
return false;
} }
bool ClientDestination::Stop () void ClientDestination::Stop ()
{ {
if (LeaseSetDestination::Stop ()) if (IsRunning ())
{ {
LeaseSetDestination::Stop ();
m_ReadyChecker.cancel(); m_ReadyChecker.cancel();
m_StreamingDestination->Stop (); m_StreamingDestination->Stop ();
//m_StreamingDestination->SetOwner (nullptr); //m_StreamingDestination->SetOwner (nullptr);
@ -967,10 +929,8 @@ namespace client
delete m_DatagramDestination; delete m_DatagramDestination;
m_DatagramDestination = nullptr; m_DatagramDestination = nullptr;
} }
return true; StopService ();
} }
else
return false;
} }
#ifdef I2LUA #ifdef I2LUA

20
libi2pd/Destination.h

@ -20,6 +20,7 @@
#include "NetDb.hpp" #include "NetDb.hpp"
#include "Streaming.h" #include "Streaming.h"
#include "Datagram.h" #include "Datagram.h"
#include "util.h"
namespace i2p namespace i2p
{ {
@ -98,18 +99,16 @@ namespace client
public: public:
LeaseSetDestination (bool isPublic, const std::map<std::string, std::string> * params = nullptr); LeaseSetDestination (boost::asio::io_service& service, bool isPublic, const std::map<std::string, std::string> * params = nullptr);
~LeaseSetDestination (); ~LeaseSetDestination ();
const std::string& GetNickname () const { return m_Nickname; }; const std::string& GetNickname () const { return m_Nickname; };
virtual bool Start (); virtual void Start ();
virtual bool Stop (); virtual void Stop ();
/** i2cp reconfigure */ /** i2cp reconfigure */
virtual bool Reconfigure(std::map<std::string, std::string> i2cpOpts); virtual bool Reconfigure(std::map<std::string, std::string> i2cpOpts);
bool IsRunning () const { return m_IsRunning; };
boost::asio::io_service& GetService () { return m_Service; };
std::shared_ptr<i2p::tunnel::TunnelPool> GetTunnelPool () { return m_Pool; }; std::shared_ptr<i2p::tunnel::TunnelPool> GetTunnelPool () { return m_Pool; };
bool IsReady () const { return m_LeaseSet && !m_LeaseSet->IsExpired () && m_Pool->GetOutboundTunnels ().size () > 0; }; bool IsReady () const { return m_LeaseSet && !m_LeaseSet->IsExpired () && m_Pool->GetOutboundTunnels ().size () > 0; };
std::shared_ptr<i2p::data::LeaseSet> FindLeaseSet (const i2p::data::IdentHash& ident); std::shared_ptr<i2p::data::LeaseSet> FindLeaseSet (const i2p::data::IdentHash& ident);
@ -146,7 +145,6 @@ namespace client
private: private:
void Run ();
void UpdateLeaseSet (); void UpdateLeaseSet ();
std::shared_ptr<const i2p::data::LocalLeaseSet> GetLeaseSetMt (); std::shared_ptr<const i2p::data::LocalLeaseSet> GetLeaseSetMt ();
void Publish (); void Publish ();
@ -165,9 +163,7 @@ namespace client
private: private:
volatile bool m_IsRunning; boost::asio::io_service& m_Service;
std::thread * m_Thread;
boost::asio::io_service m_Service;
mutable std::mutex m_RemoteLeaseSetsMutex; mutable std::mutex m_RemoteLeaseSetsMutex;
std::map<i2p::data::IdentHash, std::shared_ptr<i2p::data::LeaseSet> > m_RemoteLeaseSets; std::map<i2p::data::IdentHash, std::shared_ptr<i2p::data::LeaseSet> > m_RemoteLeaseSets;
std::map<i2p::data::IdentHash, std::shared_ptr<LeaseSetRequest> > m_LeaseSetRequests; std::map<i2p::data::IdentHash, std::shared_ptr<LeaseSetRequest> > m_LeaseSetRequests;
@ -195,7 +191,7 @@ namespace client
bool IsPerClientAuth () const { return m_AuthType > 0; }; bool IsPerClientAuth () const { return m_AuthType > 0; };
}; };
class ClientDestination: public LeaseSetDestination class ClientDestination: public i2p::util::RunnableService, public LeaseSetDestination
{ {
public: public:
#ifdef I2LUA #ifdef I2LUA
@ -209,8 +205,8 @@ namespace client
ClientDestination (const i2p::data::PrivateKeys& keys, bool isPublic, const std::map<std::string, std::string> * params = nullptr); ClientDestination (const i2p::data::PrivateKeys& keys, bool isPublic, const std::map<std::string, std::string> * params = nullptr);
~ClientDestination (); ~ClientDestination ();
virtual bool Start (); virtual void Start ();
virtual bool Stop (); virtual void Stop ();
const i2p::data::PrivateKeys& GetPrivateKeys () const { return m_Keys; }; const i2p::data::PrivateKeys& GetPrivateKeys () const { return m_Keys; };
void Sign (const uint8_t * buf, int len, uint8_t * signature) const { m_Keys.Sign (buf, len, signature); }; void Sign (const uint8_t * buf, int len, uint8_t * signature) const { m_Keys.Sign (buf, len, signature); };

2
libi2pd/util.h

@ -143,7 +143,7 @@ namespace util
private: private:
std::string m_Name; std::string m_Name;
bool m_IsRunning; volatile bool m_IsRunning;
std::unique_ptr<std::thread> m_Thread; std::unique_ptr<std::thread> m_Thread;
boost::asio::io_service m_Service; boost::asio::io_service m_Service;
}; };

27
libi2pd_client/I2CP.cpp

@ -24,10 +24,35 @@ namespace client
{ {
I2CPDestination::I2CPDestination (std::shared_ptr<I2CPSession> owner, std::shared_ptr<const i2p::data::IdentityEx> identity, bool isPublic, const std::map<std::string, std::string>& params): I2CPDestination::I2CPDestination (std::shared_ptr<I2CPSession> owner, std::shared_ptr<const i2p::data::IdentityEx> identity, bool isPublic, const std::map<std::string, std::string>& params):
LeaseSetDestination (isPublic, &params), m_Owner (owner), m_Identity (identity) RunnableService ("I2CP"), LeaseSetDestination (GetService (), isPublic, &params),
m_Owner (owner), m_Identity (identity)
{ {
} }
I2CPDestination::~I2CPDestination ()
{
if (IsRunning ())
Stop ();
}
void I2CPDestination::Start ()
{
if (!IsRunning ())
{
LeaseSetDestination::Start ();
StartService ();
}
}
void I2CPDestination::Stop ()
{
if (IsRunning ())
{
LeaseSetDestination::Stop ();
StopService ();
}
}
void I2CPDestination::SetEncryptionPrivateKey (const uint8_t * key) void I2CPDestination::SetEncryptionPrivateKey (const uint8_t * key)
{ {
memcpy (m_EncryptionPrivateKey, key, 256); memcpy (m_EncryptionPrivateKey, key, 256);

7
libi2pd_client/I2CP.h

@ -15,6 +15,7 @@
#include <thread> #include <thread>
#include <map> #include <map>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include "util.h"
#include "Destination.h" #include "Destination.h"
namespace i2p namespace i2p
@ -61,11 +62,15 @@ namespace client
const char I2CP_PARAM_MESSAGE_RELIABILITY[] = "i2cp.messageReliability"; const char I2CP_PARAM_MESSAGE_RELIABILITY[] = "i2cp.messageReliability";
class I2CPSession; class I2CPSession;
class I2CPDestination: public LeaseSetDestination class I2CPDestination: public i2p::util::RunnableService, public LeaseSetDestination
{ {
public: public:
I2CPDestination (std::shared_ptr<I2CPSession> owner, std::shared_ptr<const i2p::data::IdentityEx> identity, bool isPublic, const std::map<std::string, std::string>& params); I2CPDestination (std::shared_ptr<I2CPSession> owner, std::shared_ptr<const i2p::data::IdentityEx> identity, bool isPublic, const std::map<std::string, std::string>& params);
~I2CPDestination ();
void Start ();
void Stop ();
void SetEncryptionPrivateKey (const uint8_t * key); void SetEncryptionPrivateKey (const uint8_t * key);
void LeaseSetCreated (const uint8_t * buf, size_t len); // called from I2CPSession void LeaseSetCreated (const uint8_t * buf, size_t len); // called from I2CPSession

18
libi2pd_client/MatchedDestination.cpp

@ -45,29 +45,19 @@ namespace client
} }
bool MatchedTunnelDestination::Start() void MatchedTunnelDestination::Start()
{
if(ClientDestination::Start())
{ {
ClientDestination::Start();
m_ResolveTimer = std::make_shared<boost::asio::deadline_timer>(GetService()); m_ResolveTimer = std::make_shared<boost::asio::deadline_timer>(GetService());
GetTunnelPool()->SetCustomPeerSelector(this); GetTunnelPool()->SetCustomPeerSelector(this);
ResolveCurrentLeaseSet(); ResolveCurrentLeaseSet();
return true;
}
else
return false;
} }
bool MatchedTunnelDestination::Stop() void MatchedTunnelDestination::Stop()
{
if(ClientDestination::Stop())
{ {
ClientDestination::Stop();
if(m_ResolveTimer) if(m_ResolveTimer)
m_ResolveTimer->cancel(); m_ResolveTimer->cancel();
return true;
}
else
return false;
} }

4
libi2pd_client/MatchedDestination.h

@ -14,8 +14,8 @@ namespace client
{ {
public: public:
MatchedTunnelDestination(const i2p::data::PrivateKeys& keys, const std::string & remoteName, const std::map<std::string, std::string> * params = nullptr); MatchedTunnelDestination(const i2p::data::PrivateKeys& keys, const std::string & remoteName, const std::map<std::string, std::string> * params = nullptr);
bool Start(); void Start();
bool Stop(); void Stop();
bool SelectPeers(i2p::tunnel::Path & peers, int hops, bool inbound); bool SelectPeers(i2p::tunnel::Path & peers, int hops, bool inbound);
bool OnBuildResult(const i2p::tunnel::Path & peers, bool inbound, i2p::tunnel::TunnelBuildResult result); bool OnBuildResult(const i2p::tunnel::Path & peers, bool inbound, i2p::tunnel::TunnelBuildResult result);

Loading…
Cancel
Save