mirror of https://github.com/PurpleI2P/i2pd
Create I2Pservice as a way to integrate service management, hide unnecessary handlers
parent
88560d06a1
commit
dd42819a2f
@ -0,0 +1,19 @@
|
|||||||
|
#include "Destination.h"
|
||||||
|
#include "Identity.h"
|
||||||
|
#include "ClientContext.h"
|
||||||
|
#include "I2PService.h"
|
||||||
|
|
||||||
|
|
||||||
|
namespace i2p
|
||||||
|
{
|
||||||
|
namespace client
|
||||||
|
{
|
||||||
|
static const i2p::data::SigningKeyType I2P_SERVICE_DEFAULT_KEY_TYPE = i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA256_P256;
|
||||||
|
|
||||||
|
I2PService::I2PService (ClientDestination * localDestination):
|
||||||
|
m_LocalDestination (localDestination ? localDestination :
|
||||||
|
i2p::client::context.CreateNewLocalDestination (false, I2P_SERVICE_DEFAULT_KEY_TYPE))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,73 @@
|
|||||||
|
#ifndef I2PSERVICE_H__
|
||||||
|
#define I2PSERVICE_H__
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
|
#include <mutex>
|
||||||
|
#include <unordered_set>
|
||||||
|
#include <memory>
|
||||||
|
#include <boost/asio.hpp>
|
||||||
|
#include "Destination.h"
|
||||||
|
#include "Identity.h"
|
||||||
|
|
||||||
|
namespace i2p
|
||||||
|
{
|
||||||
|
namespace client
|
||||||
|
{
|
||||||
|
class I2PServiceHandler;
|
||||||
|
class I2PService
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
I2PService (ClientDestination * localDestination = nullptr);
|
||||||
|
virtual ~I2PService () { ClearHandlers (); }
|
||||||
|
|
||||||
|
inline void AddHandler (std::shared_ptr<I2PServiceHandler> conn) {
|
||||||
|
std::unique_lock<std::mutex> l(m_HandlersMutex);
|
||||||
|
m_Handlers.insert(conn);
|
||||||
|
}
|
||||||
|
inline void RemoveHandler (std::shared_ptr<I2PServiceHandler> conn) {
|
||||||
|
std::unique_lock<std::mutex> l(m_HandlersMutex);
|
||||||
|
m_Handlers.erase(conn);
|
||||||
|
}
|
||||||
|
inline void ClearHandlers () {
|
||||||
|
std::unique_lock<std::mutex> l(m_HandlersMutex);
|
||||||
|
m_Handlers.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline ClientDestination * GetLocalDestination () { return m_LocalDestination; };
|
||||||
|
inline void SetLocalDestination (ClientDestination * dest) { m_LocalDestination = dest; };
|
||||||
|
|
||||||
|
inline boost::asio::io_service& GetService () { return m_LocalDestination->GetService (); };
|
||||||
|
|
||||||
|
virtual void Start () = 0;
|
||||||
|
virtual void Stop () = 0;
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
ClientDestination * m_LocalDestination;
|
||||||
|
std::unordered_set<std::shared_ptr<I2PServiceHandler> > m_Handlers;
|
||||||
|
std::mutex m_HandlersMutex;
|
||||||
|
};
|
||||||
|
|
||||||
|
/*Simple interface for I2PHandlers, allows detection of finalization amongst other things */
|
||||||
|
class I2PServiceHandler
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
I2PServiceHandler(I2PService * parent) : m_Dead(false), m_Service(parent) { }
|
||||||
|
virtual ~I2PServiceHandler() { }
|
||||||
|
protected:
|
||||||
|
// Call when terminating or handing over to avoid race conditions
|
||||||
|
inline bool Kill() { return m_Dead.exchange(true); }
|
||||||
|
// Call to know if the handler is dead
|
||||||
|
inline bool Dead() { return m_Dead; }
|
||||||
|
// Call when done to clean up (make sure Kill is called first)
|
||||||
|
inline void Done(std::shared_ptr<I2PServiceHandler> me) { if(m_Service) m_Service->RemoveHandler(me); }
|
||||||
|
// Call to talk with the owner
|
||||||
|
inline I2PService * GetOwner() { return m_Service; }
|
||||||
|
private:
|
||||||
|
I2PService *m_Service;
|
||||||
|
std::atomic<bool> m_Dead; //To avoid cleaning up multiple times
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue