diff --git a/SAM.cpp b/SAM.cpp index 4e62948f..4db0f284 100644 --- a/SAM.cpp +++ b/SAM.cpp @@ -204,6 +204,12 @@ namespace stream std::string& id = params[SAM_PARAM_ID]; std::string& destination = params[SAM_PARAM_DESTINATION]; m_ID = id; + if (m_Owner.FindSession (id)) + { + // session exists + SendMessageReply (SAM_SESSION_CREATE_DUPLICATED_ID, strlen(SAM_SESSION_CREATE_DUPLICATED_ID), true); + return; + } auto session = m_Owner.CreateSession (id, destination == SAM_VALUE_TRANSIENT ? "" : destination); if (session) { @@ -217,7 +223,7 @@ namespace stream SendMessageReply (m_Buffer, l + l2 + 1, false); } else - SendMessageReply (SAM_SESSION_CREATE_DUPLICATED_ID, strlen(SAM_SESSION_CREATE_DUPLICATED_ID), true); + SendMessageReply (SAM_SESSION_CREATE_DUPLICATED_DEST, strlen(SAM_SESSION_CREATE_DUPLICATED_DEST), true); } void SAMSocket::ProcessStreamConnect (char * buf, size_t len) @@ -474,9 +480,6 @@ namespace stream SAMSession * SAMBridge::CreateSession (const std::string& id, const std::string& destination) { - if (m_Sessions.find (id) != m_Sessions.end ()) // session exists - return nullptr; - StreamingDestination * localDestination = nullptr; if (destination != "") { @@ -485,7 +488,7 @@ namespace stream i2p::data::PrivateKeys keys; keys.FromBuffer (buf, l); delete[] buf; - localDestination = GetLocalDestination (keys); + localDestination = CreateNewLocalDestination (keys); } else // transient localDestination = CreateNewLocalDestination (); @@ -493,9 +496,10 @@ namespace stream { SAMSession session; session.localDestination = localDestination; - session.isTransient = destination == ""; - m_Sessions[id] = session; - return &m_Sessions[id]; + auto ret = m_Sessions.insert (std::pair(id, session)); + if (!ret.second) + LogPrint ("Session ", id, " already exists"); + return &(ret.first->second); } return nullptr; } @@ -508,8 +512,7 @@ namespace stream for (auto it1 : it->second.sockets) delete it1; it->second.sockets.clear (); - if (it->second.isTransient) - DeleteLocalDestination (it->second.localDestination); + DeleteLocalDestination (it->second.localDestination); m_Sessions.erase (it); } } diff --git a/SAM.h b/SAM.h index 4697fda4..41227368 100644 --- a/SAM.h +++ b/SAM.h @@ -20,6 +20,7 @@ namespace stream const char SAM_SESSION_CREATE[] = "SESSION CREATE"; const char SAM_SESSION_CREATE_REPLY_OK[] = "SESSION STATUS RESULT=OK DESTINATION="; const char SAM_SESSION_CREATE_DUPLICATED_ID[] = "SESSION STATUS RESULT=DUPLICATED_ID\n"; + const char SAM_SESSION_CREATE_DUPLICATED_DEST[] = "SESSION STATUS RESULT=DUPLICATED_DEST\n"; const char SAM_STREAM_CONNECT[] = "STREAM CONNECT"; const char SAM_STREAM_STATUS_OK[] = "STREAM STATUS RESULT=OK\n"; const char SAM_STREAM_STATUS_INVALID_ID[] = "STREAM STATUS RESULT=INVALID_ID\n"; @@ -94,7 +95,6 @@ namespace stream { StreamingDestination * localDestination; std::list sockets; - bool isTransient; }; class SAMBridge diff --git a/Streaming.cpp b/Streaming.cpp index 05be28f6..9744d475 100644 --- a/Streaming.cpp +++ b/Streaming.cpp @@ -738,13 +738,13 @@ namespace stream } } - StreamingDestination * StreamingDestinations::GetLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic) + StreamingDestination * StreamingDestinations::CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic) { auto it = m_Destinations.find (keys.GetPublic ().GetIdentHash ()); if (it != m_Destinations.end ()) { LogPrint ("Local destination ", keys.GetPublic ().GetIdentHash ().ToBase32 (), ".b32.i2p exists"); - return it->second; + return nullptr; } auto localDestination = new StreamingDestination (m_Service, keys, isPublic); m_Destinations[keys.GetPublic ().GetIdentHash ()] = localDestination; @@ -825,14 +825,14 @@ namespace stream return destinations.CreateNewLocalDestination (isPublic); } - void DeleteLocalDestination (StreamingDestination * destination) + StreamingDestination * CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic) { - destinations.DeleteLocalDestination (destination); + return destinations.CreateNewLocalDestination (keys, isPublic); } - StreamingDestination * GetLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic) + void DeleteLocalDestination (StreamingDestination * destination) { - return destinations.GetLocalDestination (keys, isPublic); + destinations.DeleteLocalDestination (destination); } StreamingDestination * FindLocalDestination (const i2p::data::IdentHash& destination) diff --git a/Streaming.h b/Streaming.h index 4c2e4fec..8b681bef 100644 --- a/Streaming.h +++ b/Streaming.h @@ -199,8 +199,8 @@ namespace stream void DeleteStream (Stream * stream); StreamingDestination * GetSharedLocalDestination () const { return m_SharedLocalDestination; }; StreamingDestination * CreateNewLocalDestination (bool isPublic); + StreamingDestination * CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic); void DeleteLocalDestination (StreamingDestination * destination); - StreamingDestination * GetLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic); StreamingDestination * FindLocalDestination (const i2p::data::IdentHash& destination) const; StreamingDestination * LoadLocalDestination (const std::string& filename, bool isPublic); @@ -231,8 +231,8 @@ namespace stream void StopStreaming (); StreamingDestination * GetSharedLocalDestination (); StreamingDestination * CreateNewLocalDestination (bool isPublic = true); + StreamingDestination * CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic = true); void DeleteLocalDestination (StreamingDestination * destination); - StreamingDestination * GetLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic = true); StreamingDestination * FindLocalDestination (const i2p::data::IdentHash& destination); StreamingDestination * LoadLocalDestination (const std::string& filename, bool isPublic); // for HTTP