Browse Source

Merge pull request #4 from orignal/master

Merge pull request from orignal/master
pull/76/head
chertov 10 years ago
parent
commit
a5776b064b
  1. 2
      Daemon.cpp
  2. 5
      HTTPServer.cpp
  3. 6
      Identity.cpp
  4. 6
      Identity.h
  5. 15
      Log.cpp
  6. 14
      Log.h
  7. 36
      Queue.h
  8. 2
      Streaming.cpp
  9. 4
      Streaming.h
  10. 12
      build/CMakeLists.txt

2
Daemon.cpp

@ -33,7 +33,7 @@ namespace i2p
i2p::proxy::HTTPProxy *httpProxy; i2p::proxy::HTTPProxy *httpProxy;
}; };
Daemon_Singleton::Daemon_Singleton() : d(*new Daemon_Singleton_Private()), running(1) {}; Daemon_Singleton::Daemon_Singleton() : running(1), d(*new Daemon_Singleton_Private()) {};
Daemon_Singleton::~Daemon_Singleton() { Daemon_Singleton::~Daemon_Singleton() {
delete &d; delete &d;
}; };

5
HTTPServer.cpp

@ -1,5 +1,4 @@
#include <boost/bind.hpp> #include <boost/bind.hpp>
#include <boost/bind/protect.hpp>
#include <boost/lexical_cast.hpp> #include <boost/lexical_cast.hpp>
#include "base64.h" #include "base64.h"
#include "Log.h" #include "Log.h"
@ -388,8 +387,8 @@ namespace util
{ {
if (m_Stream) if (m_Stream)
m_Stream->AsyncReceive (boost::asio::buffer (m_StreamBuffer, 8192), m_Stream->AsyncReceive (boost::asio::buffer (m_StreamBuffer, 8192),
boost::protect (boost::bind (&HTTPConnection::HandleStreamReceive, this, boost::bind (&HTTPConnection::HandleStreamReceive, this,
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred),
45); // 45 seconds timeout 45); // 45 seconds timeout
} }

6
Identity.cpp

@ -40,6 +40,12 @@ namespace data
return *this; return *this;
} }
bool IdentHash::FromBase32(const std::string& s)
{
size_t count = Base32ToByteStream(s.c_str(), s.length(), m_Hash, sizeof(m_Hash));
return count == sizeof(m_Hash);
}
Keys CreateRandomKeys () Keys CreateRandomKeys ()
{ {
Keys keys; Keys keys;

6
Identity.h

@ -73,7 +73,9 @@ namespace data
bool operator== (const IdentHash& other) const { return !memcmp (m_Hash, other.m_Hash, 32); }; bool operator== (const IdentHash& other) const { return !memcmp (m_Hash, other.m_Hash, 32); };
bool operator< (const IdentHash& other) const { return memcmp (m_Hash, other.m_Hash, 32) < 0; }; bool operator< (const IdentHash& other) const { return memcmp (m_Hash, other.m_Hash, 32) < 0; };
bool FromBase32(const std::string&);
private: private:
uint8_t m_Hash[32]; uint8_t m_Hash[32];
@ -106,7 +108,7 @@ namespace data
public: public:
RoutingDestination (): m_ElGamalEncryption (nullptr) {}; RoutingDestination (): m_ElGamalEncryption (nullptr) {};
virtual ~RoutingDestination () { if (m_ElGamalEncryption) delete m_ElGamalEncryption; }; virtual ~RoutingDestination () { delete m_ElGamalEncryption; };
virtual const IdentHash& GetIdentHash () const = 0; virtual const IdentHash& GetIdentHash () const = 0;
virtual const uint8_t * GetEncryptionPublicKey () const = 0; virtual const uint8_t * GetEncryptionPublicKey () const = 0;

15
Log.cpp

@ -2,14 +2,19 @@
#include "Daemon.h" #include "Daemon.h"
i2p::util::MsgQueue<LogMsg> g_Log; Log g_Log;
void LogMsg::Process() void LogMsg::Process()
{ {
if (Daemon.isLogging == 1 && Daemon.logfile.is_open()) if (Daemon.isLogging == 1 && Daemon.logfile.is_open())
{
Daemon.logfile << s.str(); Daemon.logfile << s.str();
Daemon.logfile.flush();
}
output << s.str(); output << s.str();
} }
void Log::Flush ()
{
if (Daemon.isLogging == 1 && Daemon.logfile.is_open())
Daemon.logfile.flush();
}

14
Log.h

@ -3,6 +3,7 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <functional>
#include "Queue.h" #include "Queue.h"
struct LogMsg struct LogMsg
@ -15,7 +16,18 @@ struct LogMsg
void Process(); void Process();
}; };
extern i2p::util::MsgQueue<LogMsg> g_Log; class Log: public i2p::util::MsgQueue<LogMsg>
{
public:
Log () { SetOnEmpty (std::bind (&Log::Flush, this)); };
private:
void Flush ();
};
extern Log g_Log;
template<typename TValue> template<typename TValue>
void LogPrint (std::stringstream& s, TValue arg) void LogPrint (std::stringstream& s, TValue arg)

36
Queue.h

@ -5,6 +5,7 @@
#include <mutex> #include <mutex>
#include <thread> #include <thread>
#include <condition_variable> #include <condition_variable>
#include <functional>
namespace i2p namespace i2p
{ {
@ -46,6 +47,12 @@ namespace util
return el; return el;
} }
void Wait ()
{
std::unique_lock<std::mutex> l(m_QueueMutex);
m_NonEmpty.wait (l);
}
bool Wait (int sec, int usec) bool Wait (int sec, int usec)
{ {
std::unique_lock<std::mutex> l(m_QueueMutex); std::unique_lock<std::mutex> l(m_QueueMutex);
@ -98,27 +105,40 @@ namespace util
{ {
public: public:
MsgQueue (): m_Thread (std::bind (&MsgQueue<Msg>::Run, this)) , running(1) {}; typedef std::function<void()> OnEmpty;
MsgQueue (): m_IsRunning (true), m_Thread (std::bind (&MsgQueue<Msg>::Run, this)) {};
void Stop() void Stop()
{ {
running = 0; m_IsRunning = false;
Queue<Msg>::WakeUp ();
m_Thread.join(); m_Thread.join();
} }
void SetOnEmpty (OnEmpty const & e) { m_OnEmpty = e; };
private: private:
void Run () void Run ()
{ {
Msg * msg = nullptr; while (m_IsRunning)
while ((msg = Queue<Msg>::GetNext ()) != nullptr && running)
{ {
msg->Process (); while (Msg * msg = Queue<Msg>::Get ())
delete msg; {
msg->Process ();
delete msg;
}
if (m_OnEmpty != nullptr)
m_OnEmpty ();
Queue<Msg>::Wait ();
} }
} }
private: private:
std::thread m_Thread;
volatile int running; bool m_IsRunning;
std::thread m_Thread;
OnEmpty m_OnEmpty;
}; };
} }
} }

2
Streaming.cpp

@ -283,11 +283,13 @@ namespace stream
bool Stream::SendPacket (const uint8_t * buf, size_t len) bool Stream::SendPacket (const uint8_t * buf, size_t len)
{ {
const I2NPMessage * leaseSet = nullptr; const I2NPMessage * leaseSet = nullptr;
if (m_LeaseSetUpdated) if (m_LeaseSetUpdated)
{ {
leaseSet = m_LocalDestination->GetLeaseSet (); leaseSet = m_LocalDestination->GetLeaseSet ();
m_LeaseSetUpdated = false; m_LeaseSetUpdated = false;
} }
I2NPMessage * msg = i2p::garlic::routing.WrapMessage (m_RemoteLeaseSet, I2NPMessage * msg = i2p::garlic::routing.WrapMessage (m_RemoteLeaseSet,
CreateDataMessage (this, buf, len), leaseSet); CreateDataMessage (this, buf, len), leaseSet);
if (!m_OutboundTunnel || m_OutboundTunnel->IsFailed ()) if (!m_OutboundTunnel || m_OutboundTunnel->IsFailed ())

4
Streaming.h

@ -214,8 +214,8 @@ namespace stream
} }
} }
m_ReceiveTimer.expires_from_now (boost::posix_time::seconds(timeout)); m_ReceiveTimer.expires_from_now (boost::posix_time::seconds(timeout));
m_ReceiveTimer.async_wait (boost::bind (&Stream::HandleReceiveTimer<Buffer, ReceiveHandler>, m_ReceiveTimer.async_wait ([=](const boost::system::error_code& ecode)
this, boost::asio::placeholders::error, buffer, handler)); { this->HandleReceiveTimer (ecode, buffer, handler); });
} }
template<typename Buffer, typename ReceiveHandler> template<typename Buffer, typename ReceiveHandler>

12
build/CMakeLists.txt

@ -21,6 +21,7 @@ set ( SOURCES
NTCPSession.cpp NTCPSession.cpp
RouterContext.cpp RouterContext.cpp
SSU.cpp SSU.cpp
SSUData.cpp
TransitTunnel.cpp TransitTunnel.cpp
Tunnel.cpp Tunnel.cpp
TunnelGateway.cpp TunnelGateway.cpp
@ -37,6 +38,7 @@ set ( SOURCES
TunnelEndpoint.cpp TunnelEndpoint.cpp
TunnelPool.cpp TunnelPool.cpp
util.cpp util.cpp
Daemon.cpp
) )
set ( HEADERS set ( HEADERS
@ -48,6 +50,7 @@ set ( HEADERS
NTCPSession.h NTCPSession.h
RouterContext.h RouterContext.h
SSU.h SSU.h
SSUData.h
TransitTunnel.h TransitTunnel.h
Tunnel.h Tunnel.h
TunnelGateway.h TunnelGateway.h
@ -64,8 +67,17 @@ set ( HEADERS
TunnelEndpoint.h TunnelEndpoint.h
TunnelPool.h TunnelPool.h
util.h util.h
Daemon.h
) )
if (WIN32)
list (APPEND SOURCES DeamonWin32.cpp)
else ()
list (APPEND SOURCES DaemonLinux.cpp)
endif ()
source_group ("Header Files" FILES ${HEADERS}) source_group ("Header Files" FILES ${HEADERS})
source_group ("Source Files" FILES ${SOURCES}) source_group ("Source Files" FILES ${SOURCES})

Loading…
Cancel
Save