wrap m_OpenSockets with mutex

pull/1169/head
Jeff Becker 6 years ago
parent 5f525d0e43
commit 73b3fbc2da
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -33,7 +33,7 @@ namespace client
if(m_Stream)
{
m_Stream->AsyncClose ();
m_Stream.reset ();
m_Stream = nullptr;
}
auto Session = m_Owner.FindSession(m_ID);
switch (m_SocketType)
@ -64,7 +64,7 @@ namespace client
m_Socket.shutdown (boost::asio::ip::tcp::socket::shutdown_both, ec);
m_Socket.close ();
}
m_Owner.RemoveSocket(this);
m_Owner.RemoveSocket(shared_from_this());
}
void SAMSocket::ReceiveHandshake ()
@ -974,9 +974,10 @@ namespace client
std::placeholders::_1, newSocket));
}
void SAMBridge::RemoveSocket(const SAMSocket * socket)
void SAMBridge::RemoveSocket(const std::shared_ptr<SAMSocket> & socket)
{
m_OpenSockets.remove_if([socket](const std::shared_ptr<SAMSocket> & item) -> bool { return item.get() == socket; });
std::unique_lock<std::mutex> lock(m_OpenSocketsMutex);
m_OpenSockets.remove_if([socket](const std::shared_ptr<SAMSocket> & item) -> bool { return item == socket; });
}
void SAMBridge::HandleAccept(const boost::system::error_code& ecode, std::shared_ptr<SAMSocket> socket)
@ -988,7 +989,10 @@ namespace client
if (!ec)
{
LogPrint (eLogDebug, "SAM: new connection from ", ep);
m_OpenSockets.push_back(socket);
{
std::unique_lock<std::mutex> l(m_OpenSocketsMutex);
m_OpenSockets.push_back(socket);
}
socket->ReceiveHandshake ();
}
else
@ -1074,7 +1078,7 @@ namespace client
{
std::list<std::shared_ptr<SAMSocket > > list;
{
std::unique_lock<std::mutex> l(m_SessionsMutex);
std::unique_lock<std::mutex> l(m_OpenSocketsMutex);
for (const auto & itr : m_OpenSockets)
if (itr->IsSession(id))
list.push_back(itr);

@ -182,7 +182,7 @@ namespace client
/** send raw data to remote endpoint from our UDP Socket */
void SendTo(const uint8_t * buf, size_t len, std::shared_ptr<boost::asio::ip::udp::endpoint> remote);
void RemoveSocket(const SAMSocket * socket);
void RemoveSocket(const std::shared_ptr<SAMSocket> & socket);
private:
@ -204,6 +204,7 @@ namespace client
boost::asio::ip::udp::socket m_DatagramSocket;
mutable std::mutex m_SessionsMutex;
std::map<std::string, std::shared_ptr<SAMSession> > m_Sessions;
mutable std::mutex m_OpenSocketsMutex;
std::list<std::shared_ptr<SAMSocket> > m_OpenSockets;
uint8_t m_DatagramReceiveBuffer[i2p::datagram::MAX_DATAGRAM_SIZE+1];

Loading…
Cancel
Save