Browse Source

streaming api functions added

pull/113/head
orignal 10 years ago
parent
commit
77b7fff5ed
  1. 2
      Destination.cpp
  2. 2
      Destination.h
  3. 6
      Streaming.h
  4. 62
      api/api.cpp
  5. 16
      api/api.h

2
Destination.cpp

@ -281,7 +281,7 @@ namespace client
return nullptr; return nullptr;
} }
void ClientDestination::AcceptStreams (const std::function<void (i2p::stream::Stream *)>& acceptor) void ClientDestination::AcceptStreams (const i2p::stream::StreamingDestination::Acceptor& acceptor)
{ {
if (m_StreamingDestination) if (m_StreamingDestination)
m_StreamingDestination->SetAcceptor (acceptor); m_StreamingDestination->SetAcceptor (acceptor);

2
Destination.h

@ -42,7 +42,7 @@ namespace client
// streaming // streaming
i2p::stream::StreamingDestination * GetStreamingDestination () const { return m_StreamingDestination; }; i2p::stream::StreamingDestination * GetStreamingDestination () const { return m_StreamingDestination; };
i2p::stream::Stream * CreateStream (const i2p::data::LeaseSet& remote, int port = 0); i2p::stream::Stream * CreateStream (const i2p::data::LeaseSet& remote, int port = 0);
void AcceptStreams (const std::function<void (i2p::stream::Stream *)>& acceptor); void AcceptStreams (const i2p::stream::StreamingDestination::Acceptor& acceptor);
void StopAcceptingStreams (); void StopAcceptingStreams ();
bool IsAcceptingStreams () const; bool IsAcceptingStreams () const;

6
Streaming.h

@ -153,6 +153,8 @@ namespace stream
{ {
public: public:
typedef std::function<void (Stream *)> Acceptor;
StreamingDestination (i2p::client::ClientDestination& owner): m_Owner (owner) {}; StreamingDestination (i2p::client::ClientDestination& owner): m_Owner (owner) {};
~StreamingDestination () {}; ~StreamingDestination () {};
@ -161,7 +163,7 @@ namespace stream
Stream * CreateNewOutgoingStream (const i2p::data::LeaseSet& remote, int port = 0); Stream * CreateNewOutgoingStream (const i2p::data::LeaseSet& remote, int port = 0);
void DeleteStream (Stream * stream); void DeleteStream (Stream * stream);
void SetAcceptor (const std::function<void (Stream *)>& acceptor) { m_Acceptor = acceptor; }; void SetAcceptor (const Acceptor& acceptor) { m_Acceptor = acceptor; };
void ResetAcceptor () { m_Acceptor = nullptr; }; void ResetAcceptor () { m_Acceptor = nullptr; };
bool IsAcceptorSet () const { return m_Acceptor != nullptr; }; bool IsAcceptorSet () const { return m_Acceptor != nullptr; };
i2p::client::ClientDestination& GetOwner () { return m_Owner; }; i2p::client::ClientDestination& GetOwner () { return m_Owner; };
@ -178,7 +180,7 @@ namespace stream
i2p::client::ClientDestination& m_Owner; i2p::client::ClientDestination& m_Owner;
std::mutex m_StreamsMutex; std::mutex m_StreamsMutex;
std::map<uint32_t, Stream *> m_Streams; std::map<uint32_t, Stream *> m_Streams;
std::function<void (Stream *)> m_Acceptor; Acceptor m_Acceptor;
public: public:

62
api/api.cpp

@ -14,11 +14,9 @@ namespace i2p
{ {
namespace api namespace api
{ {
static std::map<i2p::data::IdentHash, i2p::client::ClientDestination *> g_Destinations; void InitI2P (int argc, char* argv[], const char * appName)
void InitI2P (int argc, char* argv[])
{ {
i2p::util::filesystem::SetAppName ("i2papi"); i2p::util::filesystem::SetAppName (appName);
i2p::util::config::OptionParser(argc, argv); i2p::util::config::OptionParser(argc, argv);
i2p::context.Init (); i2p::context.Init ();
} }
@ -43,29 +41,19 @@ namespace api
LogPrint("Transports stoped"); LogPrint("Transports stoped");
i2p::data::netdb.Stop(); i2p::data::netdb.Stop();
LogPrint("NetDB stoped"); LogPrint("NetDB stoped");
for (auto it: g_Destinations)
{
it.second->Stop ();
delete it.second;
}
g_Destinations.clear ();
LogPrint("Local destinations deleted");
StopLog (); StopLog ();
} }
i2p::client::ClientDestination * CreateLocalDestination (const i2p::data::PrivateKeys& keys) i2p::client::ClientDestination * CreateLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic)
{ {
auto localDestination = new i2p::client::ClientDestination (keys, true); // public auto localDestination = new i2p::client::ClientDestination (keys, isPublic);
g_Destinations[localDestination->GetIdentHash ()] = localDestination;
localDestination->Start (); localDestination->Start ();
return localDestination; return localDestination;
} }
i2p::client::ClientDestination * CreateLocalDestination (i2p::data::SigningKeyType sigType) i2p::client::ClientDestination * CreateLocalDestination (bool isPublic, i2p::data::SigningKeyType sigType)
{ {
auto localDestination = new i2p::client::ClientDestination (true, sigType); // public auto localDestination = new i2p::client::ClientDestination (isPublic, sigType);
g_Destinations[localDestination->GetIdentHash ()] = localDestination;
localDestination->Start (); localDestination->Start ();
return localDestination; return localDestination;
} }
@ -75,10 +63,46 @@ namespace api
if (dest) if (dest)
{ {
dest->Stop (); dest->Stop ();
g_Destinations.erase (dest->GetIdentHash ());
delete dest; delete dest;
} }
} }
void RequestLeaseSet (i2p::client::ClientDestination * dest, const i2p::data::IdentHash& remote)
{
if (dest)
i2p::data::netdb.RequestDestination (remote, true, dest->GetTunnelPool ());
}
i2p::stream::Stream * CreateStream (i2p::client::ClientDestination * dest, const i2p::data::IdentHash& remote)
{
auto leaseSet = i2p::data::netdb.FindLeaseSet (remote);
if (leaseSet)
{
auto stream = dest->CreateStream (*leaseSet);
stream->Send (nullptr, 0); // connect
return stream;
}
else
{
RequestLeaseSet (dest, remote);
return nullptr;
}
}
void AcceptStream (i2p::client::ClientDestination * dest, const i2p::stream::StreamingDestination::Acceptor& acceptor)
{
if (dest)
dest->AcceptStreams (acceptor);
}
void DestroyStream (i2p::stream::Stream * stream)
{
if (stream)
{
stream->Close ();
i2p::stream::DeleteStream (stream);
}
}
} }
} }

16
api/api.h

@ -2,18 +2,28 @@
#define API_H__ #define API_H__
#include "Identity.h" #include "Identity.h"
#include "Destination.h"
#include "Streaming.h"
namespace i2p namespace i2p
{ {
namespace api namespace api
{ {
void InitI2P (int argc, char* argv[]); // initialization start and stop
void InitI2P (int argc, char* argv[], const char * appName);
void StartI2P (); void StartI2P ();
void StopI2P (); void StopI2P ();
i2p::client::ClientDestination * CreateLocalDestination (const i2p::data::PrivateKeys& keys); // destinations
i2p::client::ClientDestination * CreateLocalDestination (i2p::data::SigningKeyType sigType = i2p::data::SIGNING_KEY_TYPE_DSA_SHA1); i2p::client::ClientDestination * CreateLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic = true);
i2p::client::ClientDestination * CreateLocalDestination (bool isPublic = false, i2p::data::SigningKeyType sigType = i2p::data::SIGNING_KEY_TYPE_DSA_SHA1); // transient destinations usually not published
void DestoroyLocalDestination (i2p::client::ClientDestination * dest); void DestoroyLocalDestination (i2p::client::ClientDestination * dest);
// streams
void RequestLeaseSet (i2p::client::ClientDestination * dest, const i2p::data::IdentHash& remote);
i2p::stream::Stream * CreateStream (i2p::client::ClientDestination * dest, const i2p::data::IdentHash& remote);
void AcceptStream (i2p::client::ClientDestination * dest, const i2p::stream::StreamingDestination::Acceptor& acceptor);
void DestroyStream (i2p::stream::Stream * stream);
} }
} }

Loading…
Cancel
Save