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 ());
}
}
}
}
}