Browse Source

whitespace cleanup

pull/1122/head
Jeff Becker 6 years ago
parent
commit
cd59ca8376
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05
  1. 119
      libi2pd/CryptoWorker.h
  2. 7
      libi2pd/NTCPSession.cpp
  3. 11
      libi2pd/NTCPSession.h

119
libi2pd/CryptoWorker.h

@ -12,69 +12,68 @@ namespace i2p
{ {
namespace worker namespace worker
{ {
template<typename Caller> template<typename Caller>
struct ThreadPool struct ThreadPool
{ {
typedef std::function<void(void)> ResultFunc; typedef std::function<void(void)> ResultFunc;
typedef std::function<ResultFunc(void)> WorkFunc; typedef std::function<ResultFunc(void)> WorkFunc;
typedef std::pair<std::shared_ptr<Caller>, WorkFunc> Job; typedef std::pair<std::shared_ptr<Caller>, WorkFunc> Job;
typedef std::mutex mtx_t; typedef std::mutex mtx_t;
typedef std::unique_lock<mtx_t> lock_t; typedef std::unique_lock<mtx_t> lock_t;
typedef std::condition_variable cond_t; typedef std::condition_variable cond_t;
ThreadPool(int workers) ThreadPool(int workers)
{ {
stop = false; stop = false;
if(workers > 0) if(workers > 0)
{ {
while(workers--) while(workers--)
{ {
threads.emplace_back([this] { threads.emplace_back([this] {
for (;;) for (;;)
{ {
Job job; Job job;
{ {
lock_t lock(this->queue_mutex); lock_t lock(this->queue_mutex);
this->condition.wait( this->condition.wait(
lock, [this] { return this->stop || !this->jobs.empty(); }); lock, [this] { return this->stop || !this->jobs.empty(); });
if (this->stop && this->jobs.empty()) return; if (this->stop && this->jobs.empty()) return;
job = std::move(this->jobs.front()); job = std::move(this->jobs.front());
this->jobs.pop_front(); this->jobs.pop_front();
} }
ResultFunc result = job.second(); ResultFunc result = job.second();
job.first->GetService().post(result); job.first->GetService().post(result);
} }
}); });
} }
} }
}; };
void Offer(const Job & job) void Offer(const Job & job)
{ {
{ {
lock_t lock(queue_mutex); lock_t lock(queue_mutex);
if (stop) return; if (stop) return;
jobs.emplace_back(job); jobs.emplace_back(job);
} }
condition.notify_one(); condition.notify_one();
} }
~ThreadPool() ~ThreadPool()
{ {
{ {
lock_t lock(queue_mutex); lock_t lock(queue_mutex);
stop = true; stop = true;
} }
condition.notify_all(); condition.notify_all();
for(auto &t: threads) t.join(); for(auto &t: threads) t.join();
} }
std::vector<std::thread> threads; std::vector<std::thread> threads;
std::deque<Job> jobs; std::deque<Job> jobs;
mtx_t queue_mutex; mtx_t queue_mutex;
cond_t condition; cond_t condition;
bool stop; bool stop;
};
};
} }
} }

7
libi2pd/NTCPSession.cpp

@ -109,7 +109,7 @@ namespace transport
{ {
return m_Server.GetService(); return m_Server.GetService();
} }
void NTCPSession::ClientLogin () void NTCPSession::ClientLogin ()
{ {
if (!m_DHKeysPair) if (!m_DHKeysPair)
@ -202,7 +202,8 @@ namespace transport
m_Encryption.SetIV (y + 240); m_Encryption.SetIV (y + 240);
m_Decryption.SetIV (m_Establisher->phase1.HXxorHI + 16); m_Decryption.SetIV (m_Establisher->phase1.HXxorHI + 16);
m_Encryption.Encrypt ((uint8_t *)&m_Establisher->phase2.encrypted, sizeof(m_Establisher->phase2.encrypted), (uint8_t *)&m_Establisher->phase2.encrypted); m_Encryption.Encrypt ((uint8_t *)&m_Establisher->phase2.encrypted, sizeof(m_Establisher->phase2.encrypted), (uint8_t *)&m_Establisher->phase2.encrypted);
boost::asio::async_write(m_Socket, boost::asio::buffer (&m_Establisher->phase2, sizeof (NTCPPhase2)), boost::asio::transfer_all(), std::bind(&NTCPSession::HandlePhase2Sent, shared_from_this(), std::placeholders::_1, std::placeholders::_2, tsB)); boost::asio::async_write(m_Socket, boost::asio::buffer (&m_Establisher->phase2, sizeof (NTCPPhase2)), boost::asio::transfer_all(),
std::bind(&NTCPSession::HandlePhase2Sent, shared_from_this(), std::placeholders::_1, std::placeholders::_2, tsB));
} }
void NTCPSession::HandlePhase2Sent (const boost::system::error_code& ecode, std::size_t bytes_transferred, uint32_t tsB) void NTCPSession::HandlePhase2Sent (const boost::system::error_code& ecode, std::size_t bytes_transferred, uint32_t tsB)
@ -299,7 +300,7 @@ namespace transport
s.Sign (keys, buf); s.Sign (keys, buf);
m_Encryption.Encrypt(m_ReceiveBuffer, len, m_ReceiveBuffer); m_Encryption.Encrypt(m_ReceiveBuffer, len, m_ReceiveBuffer);
boost::asio::async_write (m_Socket, boost::asio::buffer (m_ReceiveBuffer, len), boost::asio::transfer_all (), boost::asio::async_write (m_Socket, boost::asio::buffer (m_ReceiveBuffer, len), boost::asio::transfer_all (),
std::bind(&NTCPSession::HandlePhase3Sent, shared_from_this (), std::placeholders::_1, std::placeholders::_2, tsA)); std::bind(&NTCPSession::HandlePhase3Sent, shared_from_this (), std::placeholders::_1, std::placeholders::_2, tsA));
} }
void NTCPSession::HandlePhase3Sent (const boost::system::error_code& ecode, std::size_t bytes_transferred, uint32_t tsA) void NTCPSession::HandlePhase3Sent (const boost::system::error_code& ecode, std::size_t bytes_transferred, uint32_t tsA)

11
libi2pd/NTCPSession.h

@ -102,7 +102,7 @@ namespace transport
void HandleSent (const boost::system::error_code& ecode, std::size_t bytes_transferred, std::vector<std::shared_ptr<I2NPMessage> > msgs); void HandleSent (const boost::system::error_code& ecode, std::size_t bytes_transferred, std::vector<std::shared_ptr<I2NPMessage> > msgs);
private: private:
NTCPServer& m_Server; NTCPServer& m_Server;
boost::asio::ip::tcp::socket m_Socket; boost::asio::ip::tcp::socket m_Socket;
bool m_IsEstablished, m_IsTerminated; bool m_IsEstablished, m_IsTerminated;
@ -133,7 +133,7 @@ namespace transport
{ {
public: public:
typedef i2p::worker::ThreadPool<NTCPSession> Pool; typedef i2p::worker::ThreadPool<NTCPSession> Pool;
enum RemoteAddressType enum RemoteAddressType
{ {
@ -177,7 +177,6 @@ namespace transport
{ {
m_CryptoPool->Offer({conn, work}); m_CryptoPool->Offer({conn, work});
} }
private: private:
/** @brief return true for hard limit */ /** @brief return true for hard limit */
@ -203,7 +202,7 @@ namespace transport
void HandleTerminationTimer (const boost::system::error_code& ecode); void HandleTerminationTimer (const boost::system::error_code& ecode);
private: private:
bool m_IsRunning; bool m_IsRunning;
std::thread * m_Thread; std::thread * m_Thread;
boost::asio::io_service m_Service; boost::asio::io_service m_Service;
@ -219,8 +218,8 @@ namespace transport
boost::asio::ip::tcp::resolver m_Resolver; boost::asio::ip::tcp::resolver m_Resolver;
boost::asio::ip::tcp::endpoint * m_ProxyEndpoint; boost::asio::ip::tcp::endpoint * m_ProxyEndpoint;
std::shared_ptr<Pool> m_CryptoPool; std::shared_ptr<Pool> m_CryptoPool;
uint16_t m_SoftLimit, m_HardLimit; uint16_t m_SoftLimit, m_HardLimit;
public: public:

Loading…
Cancel
Save