Browse Source

DH keys supplier

pull/59/head
orignal 11 years ago
parent
commit
93ff88bcb8
  1. 75
      Transports.cpp
  2. 31
      Transports.h

75
Transports.cpp

@ -9,10 +9,76 @@ using namespace i2p::data;
namespace i2p namespace i2p
{ {
DHKeysPairSupplier::~DHKeysPairSupplier ()
{
Stop ();
}
void DHKeysPairSupplier::Start ()
{
m_IsRunning = true;
m_Thread = new std::thread (std::bind (&DHKeysPairSupplier::Run, this));
}
void DHKeysPairSupplier::Stop ()
{
m_IsRunning = false;
m_Acquired.notify_one ();
if (m_Thread)
{
m_Thread->join ();
delete m_Thread;
m_Thread = 0;
}
}
void DHKeysPairSupplier::Run ()
{
while (m_IsRunning)
{
int num;
while ((num = m_QueueSize - m_Queue.size ()) > 0)
CreateDHKeysPairs (num);
std::unique_lock<std::mutex> l(m_AcquiredMutex);
m_Acquired.wait (l); // wait for element gets aquired
}
}
void DHKeysPairSupplier::CreateDHKeysPairs (int num)
{
if (num > 0)
{
for (int i = 0; i < num; i++)
{
i2p::data::DHKeysPair * pair = new i2p::data::DHKeysPair ();
i2p::data::CreateRandomDHKeysPair (pair);
m_Queue.push (pair);
}
}
}
i2p::data::DHKeysPair * DHKeysPairSupplier::Acquire ()
{
if (!m_Queue.empty ())
{
auto pair = m_Queue.front ();
m_Queue.pop ();
m_Acquired.notify_one ();
return pair;
}
else // queue is empty, create new
{
i2p::data::DHKeysPair * pair = new i2p::data::DHKeysPair ();
i2p::data::CreateRandomDHKeysPair (pair);
return pair;
}
}
Transports transports; Transports transports;
Transports::Transports (): Transports::Transports ():
m_Thread (nullptr), m_Work (m_Service),m_NTCPAcceptor (nullptr), m_SSUServer (nullptr) m_Thread (nullptr), m_Work (m_Service), m_NTCPAcceptor (nullptr),
m_SSUServer (nullptr), m_DHKeysPairSupplier (5) // 5 pre-generated keys
{ {
} }
@ -23,6 +89,7 @@ namespace i2p
void Transports::Start () void Transports::Start ()
{ {
m_DHKeysPairSupplier.Start ();
m_IsRunning = true; m_IsRunning = true;
m_Thread = new std::thread (std::bind (&Transports::Run, this)); m_Thread = new std::thread (std::bind (&Transports::Run, this));
m_Timer = new boost::asio::deadline_timer (m_Service); m_Timer = new boost::asio::deadline_timer (m_Service);
@ -74,6 +141,7 @@ namespace i2p
delete m_SSUServer; delete m_SSUServer;
} }
m_DHKeysPairSupplier.Stop ();
m_IsRunning = false; m_IsRunning = false;
m_Service.stop (); m_Service.stop ();
if (m_Thread) if (m_Thread)
@ -223,9 +291,6 @@ namespace i2p
i2p::data::DHKeysPair * Transports::GetNextDHKeysPair () i2p::data::DHKeysPair * Transports::GetNextDHKeysPair ()
{ {
// TODO: use supplier with separate thread return m_DHKeysPairSupplier.Acquire ();
i2p::data::DHKeysPair * pair = new i2p::data::DHKeysPair ();
i2p::data::CreateRandomDHKeysPair (pair);
return pair;
} }
} }

31
Transports.h

@ -2,8 +2,11 @@
#define TRANSPORTS_H__ #define TRANSPORTS_H__
#include <thread> #include <thread>
#include <mutex>
#include <condition_variable>
#include <functional> #include <functional>
#include <map> #include <map>
#include <queue>
#include <string> #include <string>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include "NTCPSession.h" #include "NTCPSession.h"
@ -14,6 +17,32 @@
namespace i2p namespace i2p
{ {
class DHKeysPairSupplier
{
public:
DHKeysPairSupplier (int size): m_QueueSize (size), m_IsRunning (false), m_Thread (nullptr) {};
~DHKeysPairSupplier ();
void Start ();
void Stop ();
i2p::data::DHKeysPair * Acquire ();
private:
void Run ();
void CreateDHKeysPairs (int num);
private:
int m_QueueSize;
std::queue<i2p::data::DHKeysPair *> m_Queue;
bool m_IsRunning;
std::thread * m_Thread;
std::condition_variable m_Acquired;
std::mutex m_AcquiredMutex;
};
class Transports class Transports
{ {
public: public:
@ -56,6 +85,8 @@ namespace i2p
i2p::ssu::SSUServer * m_SSUServer; i2p::ssu::SSUServer * m_SSUServer;
boost::asio::deadline_timer * m_Timer; boost::asio::deadline_timer * m_Timer;
DHKeysPairSupplier m_DHKeysPairSupplier;
public: public:
// for HTTP only // for HTTP only

Loading…
Cancel
Save