Browse Source

wrap m_OpenSockets with mutex

pull/1169/head
Jeff Becker 7 years ago
parent
commit
73b3fbc2da
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05
  1. 14
      libi2pd_client/SAM.cpp
  2. 3
      libi2pd_client/SAM.h

14
libi2pd_client/SAM.cpp

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

3
libi2pd_client/SAM.h

@ -182,7 +182,7 @@ namespace client
/** send raw data to remote endpoint from our UDP Socket */ /** 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 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: private:
@ -204,6 +204,7 @@ namespace client
boost::asio::ip::udp::socket m_DatagramSocket; boost::asio::ip::udp::socket m_DatagramSocket;
mutable std::mutex m_SessionsMutex; mutable std::mutex m_SessionsMutex;
std::map<std::string, std::shared_ptr<SAMSession> > m_Sessions; std::map<std::string, std::shared_ptr<SAMSession> > m_Sessions;
mutable std::mutex m_OpenSocketsMutex;
std::list<std::shared_ptr<SAMSocket> > m_OpenSockets; std::list<std::shared_ptr<SAMSocket> > m_OpenSockets;
uint8_t m_DatagramReceiveBuffer[i2p::datagram::MAX_DATAGRAM_SIZE+1]; uint8_t m_DatagramReceiveBuffer[i2p::datagram::MAX_DATAGRAM_SIZE+1];

Loading…
Cancel
Save