handle garlic message in separate thread

This commit is contained in:
orignal 2014-03-12 21:13:54 -04:00
parent 6732ba21f9
commit 28cc50fece
3 changed files with 53 additions and 0 deletions

View File

@ -263,6 +263,11 @@ namespace garlic
}
void GarlicRouting::HandleGarlicMessage (I2NPMessage * msg)
{
if (msg) m_Queue.Put (msg);
}
void GarlicRouting::ProcessGarlicMessage (I2NPMessage * msg)
{
uint8_t * buf = msg->GetPayload ();
uint32_t length = be32toh (*(uint32_t *)buf);
@ -411,5 +416,40 @@ namespace garlic
LogPrint ("Garlic message ", be32toh (msg->msgID), " acknowledged");
}
}
void GarlicRouting::Start ()
{
m_IsRunning = true;
m_Thread = new std::thread (std::bind (&GarlicRouting::Run, this));
}
void GarlicRouting::Stop ()
{
m_IsRunning = false;
m_Queue.WakeUp ();
if (m_Thread)
{
m_Thread->join ();
delete m_Thread;
m_Thread = 0;
}
}
void GarlicRouting::Run ()
{
while (m_IsRunning)
{
try
{
I2NPMessage * msg = m_Queue.GetNext ();
if (msg)
ProcessGarlicMessage (msg);
}
catch (std::exception& ex)
{
LogPrint ("GarlicRouting: ", ex.what ());
}
}
}
}
}

View File

@ -4,12 +4,14 @@
#include <inttypes.h>
#include <map>
#include <string>
#include <thread>
#include <cryptopp/modes.h>
#include <cryptopp/aes.h>
#include <cryptopp/osrng.h>
#include "I2NPProtocol.h"
#include "LeaseSet.h"
#include "Tunnel.h"
#include "Queue.h"
namespace i2p
{
@ -76,6 +78,9 @@ namespace garlic
GarlicRouting ();
~GarlicRouting ();
void Start ();
void Stop ();
void HandleGarlicMessage (I2NPMessage * msg);
void HandleDeliveryStatusMessage (uint8_t * buf, size_t len);
@ -85,11 +90,16 @@ namespace garlic
private:
void Run ();
void ProcessGarlicMessage (I2NPMessage * msg);
void HandleAESBlock (uint8_t * buf, size_t len, uint8_t * sessionKey);
void HandleGarlicPayload (uint8_t * buf, size_t len);
private:
bool m_IsRunning;
std::thread * m_Thread;
i2p::util::Queue<I2NPMessage> m_Queue;
// outgoing sessions
std::map<i2p::data::IdentHash, GarlicRoutingSession *> m_Sessions;
std::map<uint32_t, GarlicRoutingSession *> m_CreatedSessions; // msgID -> session

View File

@ -22,6 +22,7 @@
#include "Tunnel.h"
#include "NetDb.h"
#include "HTTPServer.h"
#include "Garlic.h"
#include "util.h"
@ -152,6 +153,7 @@ int main( int argc, char* argv[] )
i2p::data::netdb.Start ();
i2p::transports.Start ();
i2p::tunnel::tunnels.Start ();
i2p::garlic::routing.Start ();
while (running)
{
@ -160,6 +162,7 @@ int main( int argc, char* argv[] )
}
LogPrint("Shutdown started.");
i2p::garlic::routing.Stop ();
i2p::tunnel::tunnels.Stop ();
i2p::transports.Stop ();
i2p::data::netdb.Stop ();