1
0
mirror of https://github.com/PurpleI2P/i2pd.git synced 2025-01-22 12:24:19 +00:00

fix termination crash

This commit is contained in:
Jeff Becker 2016-12-18 12:28:32 -05:00
parent 67927bd8f4
commit 042adb5e34
2 changed files with 36 additions and 22 deletions

56
SAM.cpp
View File

@ -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));
} }
std::shared_ptr<SAMSocket> SAMSession::FindAcceptor()
{
for (auto & sock : m_Sockets) {
auto t = sock->GetSocketType();
if(t == eSAMSocketTypeAcceptor) {
return sock;
}
}
return nullptr;
}
void SAMSession::HandlePumpBacklog(const boost::system::error_code & ec) void SAMSession::HandlePumpBacklog(const boost::system::error_code & ec)
{ {
if(ec) return; if(ec) return;
{
std::unique_lock<std::mutex> lock(m_SocketsMutex); std::unique_lock<std::mutex> lock(m_SocketsMutex);
for(auto & stream : m_Backlog) { auto itr = m_Backlog.begin();
for (auto & sock : m_Sockets) { while(itr != m_Backlog.end()) {
auto t = sock->GetSocketType(); auto sock = FindAcceptor();
if(t == eSAMSocketTypeAcceptor) { if (sock) {
sock->Accept(stream); sock->Accept(*itr);
break; itr = m_Backlog.erase(itr);
} else {
++itr;
} }
} }
} }
@ -755,18 +767,18 @@ 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();
} }
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
View File

@ -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);