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

don't create SAM session is local destination exists

This commit is contained in:
orignal 2014-10-01 14:52:32 -04:00
parent c047544cdb
commit 30233bed77
4 changed files with 23 additions and 20 deletions

23
SAM.cpp
View File

@ -204,6 +204,12 @@ namespace stream
std::string& id = params[SAM_PARAM_ID]; std::string& id = params[SAM_PARAM_ID];
std::string& destination = params[SAM_PARAM_DESTINATION]; std::string& destination = params[SAM_PARAM_DESTINATION];
m_ID = id; 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); auto session = m_Owner.CreateSession (id, destination == SAM_VALUE_TRANSIENT ? "" : destination);
if (session) if (session)
{ {
@ -217,7 +223,7 @@ namespace stream
SendMessageReply (m_Buffer, l + l2 + 1, false); SendMessageReply (m_Buffer, l + l2 + 1, false);
} }
else 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) 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) 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; StreamingDestination * localDestination = nullptr;
if (destination != "") if (destination != "")
{ {
@ -485,7 +488,7 @@ namespace stream
i2p::data::PrivateKeys keys; i2p::data::PrivateKeys keys;
keys.FromBuffer (buf, l); keys.FromBuffer (buf, l);
delete[] buf; delete[] buf;
localDestination = GetLocalDestination (keys); localDestination = CreateNewLocalDestination (keys);
} }
else // transient else // transient
localDestination = CreateNewLocalDestination (); localDestination = CreateNewLocalDestination ();
@ -493,9 +496,10 @@ namespace stream
{ {
SAMSession session; SAMSession session;
session.localDestination = localDestination; session.localDestination = localDestination;
session.isTransient = destination == ""; auto ret = m_Sessions.insert (std::pair<std::string, SAMSession>(id, session));
m_Sessions[id] = session; if (!ret.second)
return &m_Sessions[id]; LogPrint ("Session ", id, " already exists");
return &(ret.first->second);
} }
return nullptr; return nullptr;
} }
@ -508,8 +512,7 @@ namespace stream
for (auto it1 : it->second.sockets) for (auto it1 : it->second.sockets)
delete it1; delete it1;
it->second.sockets.clear (); it->second.sockets.clear ();
if (it->second.isTransient) DeleteLocalDestination (it->second.localDestination);
DeleteLocalDestination (it->second.localDestination);
m_Sessions.erase (it); m_Sessions.erase (it);
} }
} }

2
SAM.h
View File

@ -20,6 +20,7 @@ namespace stream
const char SAM_SESSION_CREATE[] = "SESSION CREATE"; const char SAM_SESSION_CREATE[] = "SESSION CREATE";
const char SAM_SESSION_CREATE_REPLY_OK[] = "SESSION STATUS RESULT=OK DESTINATION="; 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_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_CONNECT[] = "STREAM CONNECT";
const char SAM_STREAM_STATUS_OK[] = "STREAM STATUS RESULT=OK\n"; const char SAM_STREAM_STATUS_OK[] = "STREAM STATUS RESULT=OK\n";
const char SAM_STREAM_STATUS_INVALID_ID[] = "STREAM STATUS RESULT=INVALID_ID\n"; const char SAM_STREAM_STATUS_INVALID_ID[] = "STREAM STATUS RESULT=INVALID_ID\n";
@ -94,7 +95,6 @@ namespace stream
{ {
StreamingDestination * localDestination; StreamingDestination * localDestination;
std::list<SAMSocket *> sockets; std::list<SAMSocket *> sockets;
bool isTransient;
}; };
class SAMBridge class SAMBridge

View File

@ -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 ()); auto it = m_Destinations.find (keys.GetPublic ().GetIdentHash ());
if (it != m_Destinations.end ()) if (it != m_Destinations.end ())
{ {
LogPrint ("Local destination ", keys.GetPublic ().GetIdentHash ().ToBase32 (), ".b32.i2p exists"); LogPrint ("Local destination ", keys.GetPublic ().GetIdentHash ().ToBase32 (), ".b32.i2p exists");
return it->second; return nullptr;
} }
auto localDestination = new StreamingDestination (m_Service, keys, isPublic); auto localDestination = new StreamingDestination (m_Service, keys, isPublic);
m_Destinations[keys.GetPublic ().GetIdentHash ()] = localDestination; m_Destinations[keys.GetPublic ().GetIdentHash ()] = localDestination;
@ -825,16 +825,16 @@ namespace stream
return destinations.CreateNewLocalDestination (isPublic); return destinations.CreateNewLocalDestination (isPublic);
} }
StreamingDestination * CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic)
{
return destinations.CreateNewLocalDestination (keys, isPublic);
}
void DeleteLocalDestination (StreamingDestination * destination) void DeleteLocalDestination (StreamingDestination * destination)
{ {
destinations.DeleteLocalDestination (destination); destinations.DeleteLocalDestination (destination);
} }
StreamingDestination * GetLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic)
{
return destinations.GetLocalDestination (keys, isPublic);
}
StreamingDestination * FindLocalDestination (const i2p::data::IdentHash& destination) StreamingDestination * FindLocalDestination (const i2p::data::IdentHash& destination)
{ {
return destinations.FindLocalDestination (destination); return destinations.FindLocalDestination (destination);

View File

@ -199,8 +199,8 @@ namespace stream
void DeleteStream (Stream * stream); void DeleteStream (Stream * stream);
StreamingDestination * GetSharedLocalDestination () const { return m_SharedLocalDestination; }; StreamingDestination * GetSharedLocalDestination () const { return m_SharedLocalDestination; };
StreamingDestination * CreateNewLocalDestination (bool isPublic); StreamingDestination * CreateNewLocalDestination (bool isPublic);
StreamingDestination * CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic);
void DeleteLocalDestination (StreamingDestination * destination); void DeleteLocalDestination (StreamingDestination * destination);
StreamingDestination * GetLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic);
StreamingDestination * FindLocalDestination (const i2p::data::IdentHash& destination) const; StreamingDestination * FindLocalDestination (const i2p::data::IdentHash& destination) const;
StreamingDestination * LoadLocalDestination (const std::string& filename, bool isPublic); StreamingDestination * LoadLocalDestination (const std::string& filename, bool isPublic);
@ -231,8 +231,8 @@ namespace stream
void StopStreaming (); void StopStreaming ();
StreamingDestination * GetSharedLocalDestination (); StreamingDestination * GetSharedLocalDestination ();
StreamingDestination * CreateNewLocalDestination (bool isPublic = true); StreamingDestination * CreateNewLocalDestination (bool isPublic = true);
StreamingDestination * CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic = true);
void DeleteLocalDestination (StreamingDestination * destination); void DeleteLocalDestination (StreamingDestination * destination);
StreamingDestination * GetLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic = true);
StreamingDestination * FindLocalDestination (const i2p::data::IdentHash& destination); StreamingDestination * FindLocalDestination (const i2p::data::IdentHash& destination);
StreamingDestination * LoadLocalDestination (const std::string& filename, bool isPublic); StreamingDestination * LoadLocalDestination (const std::string& filename, bool isPublic);
// for HTTP // for HTTP