Browse Source

handle garlic message in separate thread

pull/46/head
orignal 10 years ago
parent
commit
28cc50fece
  1. 40
      Garlic.cpp
  2. 10
      Garlic.h
  3. 3
      i2p.cpp

40
Garlic.cpp

@ -263,6 +263,11 @@ namespace garlic
} }
void GarlicRouting::HandleGarlicMessage (I2NPMessage * msg) void GarlicRouting::HandleGarlicMessage (I2NPMessage * msg)
{
if (msg) m_Queue.Put (msg);
}
void GarlicRouting::ProcessGarlicMessage (I2NPMessage * msg)
{ {
uint8_t * buf = msg->GetPayload (); uint8_t * buf = msg->GetPayload ();
uint32_t length = be32toh (*(uint32_t *)buf); uint32_t length = be32toh (*(uint32_t *)buf);
@ -411,5 +416,40 @@ namespace garlic
LogPrint ("Garlic message ", be32toh (msg->msgID), " acknowledged"); 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 ());
}
}
}
} }
} }

10
Garlic.h

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

3
i2p.cpp

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

Loading…
Cancel
Save