Browse Source

fix termination crash

pull/742/head
Jeff Becker 8 years ago
parent
commit
d5f27ecb0e
  1. 34
      SAM.cpp
  2. 2
      SAM.h

34
SAM.cpp

@ -404,6 +404,7 @@ namespace client
{ {
m_SocketType = eSAMSocketTypeAcceptor; m_SocketType = eSAMSocketTypeAcceptor;
m_Session->AddSocket (shared_from_this ()); m_Session->AddSocket (shared_from_this ());
SendMessageReply (SAM_STREAM_STATUS_OK, strlen(SAM_STREAM_STATUS_OK), false);
} }
else else
SendMessageReply (SAM_STREAM_STATUS_INVALID_ID, strlen(SAM_STREAM_STATUS_INVALID_ID), true); SendMessageReply (SAM_STREAM_STATUS_INVALID_ID, strlen(SAM_STREAM_STATUS_INVALID_ID), true);
@ -413,7 +414,6 @@ namespace client
{ {
if(stream) { if(stream) {
m_SocketType = eSAMSocketTypeStream; m_SocketType = eSAMSocketTypeStream;
SendMessageReply (SAM_STREAM_STATUS_OK, strlen(SAM_STREAM_STATUS_OK), false);
HandleI2PAccept(stream); HandleI2PAccept(stream);
} else { } else {
SendMessageReply (SAM_STREAM_STATUS_I2P_ERROR, strlen(SAM_STREAM_STATUS_I2P_ERROR), true); SendMessageReply (SAM_STREAM_STATUS_I2P_ERROR, strlen(SAM_STREAM_STATUS_I2P_ERROR), true);
@ -713,7 +713,6 @@ namespace client
SAMSession::~SAMSession () SAMSession::~SAMSession ()
{ {
CloseStreams();
i2p::client::context.DeleteLocalDestination (localDestination); i2p::client::context.DeleteLocalDestination (localDestination);
} }
@ -736,17 +735,30 @@ namespace client
m_BacklogPumper.async_wait(std::bind(&SAMSession::HandlePumpBacklog, this, std::placeholders::_1)); m_BacklogPumper.async_wait(std::bind(&SAMSession::HandlePumpBacklog, this, std::placeholders::_1));
} }
void SAMSession::HandlePumpBacklog(const boost::system::error_code & ec) std::shared_ptr<SAMSocket> SAMSession::FindAcceptor()
{ {
if(ec) return;
std::unique_lock<std::mutex> lock(m_SocketsMutex);
for(auto & stream : m_Backlog) {
for (auto & sock : m_Sockets) { for (auto & sock : m_Sockets) {
auto t = sock->GetSocketType(); auto t = sock->GetSocketType();
if(t == eSAMSocketTypeAcceptor) { if(t == eSAMSocketTypeAcceptor) {
sock->Accept(stream); return sock;
break; }
}
return nullptr;
}
void SAMSession::HandlePumpBacklog(const boost::system::error_code & ec)
{
if(ec) return;
{
std::unique_lock<std::mutex> lock(m_SocketsMutex);
auto itr = m_Backlog.begin();
while(itr != m_Backlog.end()) {
auto sock = FindAcceptor();
if (sock) {
sock->Accept(*itr);
itr = m_Backlog.erase(itr);
} else {
++itr;
} }
} }
} }
@ -755,7 +767,7 @@ namespace client
void SAMSession::CloseStreams () void SAMSession::CloseStreams ()
{ {
{ localDestination->GetService().post([&] () {
std::lock_guard<std::mutex> lock(m_SocketsMutex); std::lock_guard<std::mutex> lock(m_SocketsMutex);
for (auto& sock : m_Sockets) { for (auto& sock : m_Sockets) {
sock->CloseStream(); sock->CloseStream();
@ -763,10 +775,10 @@ namespace client
for(auto & stream : m_Backlog) { for(auto & stream : m_Backlog) {
stream->Close(); stream->Close();
} }
}
// XXX: should this be done inside locked parts? // XXX: should this be done inside locked parts?
m_Sockets.clear(); m_Sockets.clear();
m_Backlog.clear(); m_Backlog.clear();
});
} }
SAMBridge::SAMBridge (const std::string& address, int port): SAMBridge::SAMBridge (const std::string& address, int port):

2
SAM.h

@ -169,6 +169,8 @@ namespace client
void AcceptI2P(std::shared_ptr<i2p::stream::Stream> stream); void AcceptI2P(std::shared_ptr<i2p::stream::Stream> stream);
std::shared_ptr<SAMSocket> FindAcceptor();
void PumpBacklog(); void PumpBacklog();
void HandlePumpBacklog(const boost::system::error_code & ec); void HandlePumpBacklog(const boost::system::error_code & ec);

Loading…
Cancel
Save