mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-03-10 01:01:02 +00:00
prevent race condition in datagram destination
clean up style a bit
This commit is contained in:
parent
caace05ba6
commit
c770bcbf96
@ -543,11 +543,12 @@ namespace client
|
|||||||
auto dest = i2p::client::context.GetSharedLocalDestination ();
|
auto dest = i2p::client::context.GetSharedLocalDestination ();
|
||||||
if (dest)
|
if (dest)
|
||||||
{
|
{
|
||||||
|
|
||||||
auto datagram = dest->GetDatagramDestination ();
|
auto datagram = dest->GetDatagramDestination ();
|
||||||
if(datagram == nullptr) datagram = dest->CreateDatagramDestination();
|
if (!datagram)
|
||||||
datagram->SetReceiver (std::bind (&AddressBook::HandleLookupResponse, this,
|
datagram = dest->CreateDatagramDestination ();
|
||||||
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5));
|
datagram->SetReceiver (std::bind (&AddressBook::HandleLookupResponse, this,
|
||||||
|
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5),
|
||||||
|
ADDRESS_RESPONSE_DATAGRAM_PORT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -557,8 +558,7 @@ namespace client
|
|||||||
if (dest)
|
if (dest)
|
||||||
{
|
{
|
||||||
auto datagram = dest->GetDatagramDestination ();
|
auto datagram = dest->GetDatagramDestination ();
|
||||||
if (datagram)
|
if (datagram) datagram->ResetReceiver (ADDRESS_RESPONSE_DATAGRAM_PORT);
|
||||||
datagram->ResetReceiver ();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
20
Datagram.cpp
20
Datagram.cpp
@ -71,18 +71,26 @@ namespace datagram
|
|||||||
|
|
||||||
if (verified)
|
if (verified)
|
||||||
{
|
{
|
||||||
auto it = m_ReceiversByPorts.find (toPort);
|
auto r = FindReceiver(toPort);
|
||||||
if (it != m_ReceiversByPorts.end ())
|
if(r)
|
||||||
it->second (identity, fromPort, toPort, buf + headerLen, len -headerLen);
|
r(identity, fromPort, toPort, buf + headerLen, len -headerLen);
|
||||||
else if (m_Receiver != nullptr)
|
|
||||||
m_Receiver (identity, fromPort, toPort, buf + headerLen, len -headerLen);
|
|
||||||
else
|
else
|
||||||
LogPrint (eLogWarning, "Receiver for datagram is not set");
|
LogPrint (eLogWarning, "DatagramDestination: no receiver for port ", toPort);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
LogPrint (eLogWarning, "Datagram signature verification failed");
|
LogPrint (eLogWarning, "Datagram signature verification failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DatagramDestination::Receiver DatagramDestination::FindReceiver(uint16_t port)
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(m_ReceiversMutex);
|
||||||
|
Receiver r = m_Receiver;
|
||||||
|
auto itr = m_ReceiversByPorts.find(port);
|
||||||
|
if (itr != m_ReceiversByPorts.end())
|
||||||
|
r = itr->second;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
void DatagramDestination::HandleDataMessagePayload (uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len)
|
void DatagramDestination::HandleDataMessagePayload (uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len)
|
||||||
{
|
{
|
||||||
// unzip it
|
// unzip it
|
||||||
|
14
Datagram.h
14
Datagram.h
@ -80,8 +80,8 @@ namespace datagram
|
|||||||
void SetReceiver (const Receiver& receiver) { m_Receiver = receiver; };
|
void SetReceiver (const Receiver& receiver) { m_Receiver = receiver; };
|
||||||
void ResetReceiver () { m_Receiver = nullptr; };
|
void ResetReceiver () { m_Receiver = nullptr; };
|
||||||
|
|
||||||
void SetReceiver (const Receiver& receiver, uint16_t port) { m_ReceiversByPorts[port] = receiver; };
|
void SetReceiver (const Receiver& receiver, uint16_t port) { std::lock_guard<std::mutex> lock(m_ReceiversMutex); m_ReceiversByPorts[port] = receiver; };
|
||||||
void ResetReceiver (uint16_t port) { m_ReceiversByPorts.erase (port); };
|
void ResetReceiver (uint16_t port) { std::lock_guard<std::mutex> lock(m_ReceiversMutex); m_ReceiversByPorts.erase (port); };
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// clean up after next tick
|
// clean up after next tick
|
||||||
@ -96,12 +96,16 @@ namespace datagram
|
|||||||
|
|
||||||
void HandleDatagram (uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len);
|
void HandleDatagram (uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len);
|
||||||
|
|
||||||
|
/** find a receiver by port, if none by port is found try default receiever, otherwise returns nullptr */
|
||||||
|
Receiver FindReceiver(uint16_t port);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
i2p::client::ClientDestination * m_Owner;
|
i2p::client::ClientDestination * m_Owner;
|
||||||
boost::asio::deadline_timer m_CleanupTimer;
|
boost::asio::deadline_timer m_CleanupTimer;
|
||||||
Receiver m_Receiver; // default
|
Receiver m_Receiver; // default
|
||||||
std::mutex m_SessionsMutex;
|
std::mutex m_SessionsMutex;
|
||||||
std::map<i2p::data::IdentHash, std::shared_ptr<DatagramSession> > m_Sessions;
|
std::map<i2p::data::IdentHash, std::shared_ptr<DatagramSession> > m_Sessions;
|
||||||
|
std::mutex m_ReceiversMutex;
|
||||||
std::map<uint16_t, Receiver> m_ReceiversByPorts;
|
std::map<uint16_t, Receiver> m_ReceiversByPorts;
|
||||||
|
|
||||||
i2p::data::GzipInflator m_Inflator;
|
i2p::data::GzipInflator m_Inflator;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user