Skip to content

Commit

Permalink
separate thread for SSU server
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Apr 20, 2014
1 parent 31295a6 commit bf2e833
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
31 changes: 28 additions & 3 deletions SSU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -992,8 +992,8 @@ namespace ssu
m_Server.Send (buf, msgSize, m_RemoteEndpoint);
}

SSUServer::SSUServer (boost::asio::io_service& service, int port):
m_Endpoint (boost::asio::ip::udp::v4 (), port), m_Socket (service, m_Endpoint)
SSUServer::SSUServer (int port): m_Thread (nullptr), m_Work (m_Service),
m_Endpoint (boost::asio::ip::udp::v4 (), port), m_Socket (m_Service, m_Endpoint)
{
m_Socket.set_option (boost::asio::socket_base::receive_buffer_size (65535));
m_Socket.set_option (boost::asio::socket_base::send_buffer_size (65535));
Expand All @@ -1007,15 +1007,40 @@ namespace ssu

void SSUServer::Start ()
{
Receive ();
m_IsRunning = true;
m_Thread = new std::thread (std::bind (&SSUServer::Run, this));
m_Service.post (boost::bind (&SSUServer::Receive, this));
}

void SSUServer::Stop ()
{
DeleteAllSessions ();
m_IsRunning = false;
m_Service.stop ();
m_Socket.close ();
if (m_Thread)
{
m_Thread->join ();
delete m_Thread;
m_Thread = 0;
}
}

void SSUServer::Run ()
{
while (m_IsRunning)
{
try
{
m_Service.run ();
}
catch (std::exception& ex)
{
LogPrint ("SSU server: ", ex.what ());
}
}
}

void SSUServer::AddRelay (uint32_t tag, const boost::asio::ip::udp::endpoint& relay)
{
m_Relays[tag] = relay;
Expand Down
10 changes: 8 additions & 2 deletions SSU.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <map>
#include <list>
#include <set>
#include <thread>
#include <boost/asio.hpp>
#include <cryptopp/modes.h>
#include <cryptopp/aes.h>
Expand Down Expand Up @@ -154,7 +155,7 @@ namespace ssu
{
public:

SSUServer (boost::asio::io_service& service, int port);
SSUServer (int port);
~SSUServer ();
void Start ();
void Stop ();
Expand All @@ -172,11 +173,16 @@ namespace ssu

private:

void Run ();
void Receive ();
void HandleReceivedFrom (const boost::system::error_code& ecode, std::size_t bytes_transferred);

private:


bool m_IsRunning;
std::thread * m_Thread;
boost::asio::io_service m_Service;
boost::asio::io_service::work m_Work;
boost::asio::ip::udp::endpoint m_Endpoint;
boost::asio::ip::udp::socket m_Socket;
boost::asio::ip::udp::endpoint m_SenderEndpoint;
Expand Down
2 changes: 1 addition & 1 deletion Transports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ namespace i2p
{
if (!m_SSUServer)
{
m_SSUServer = new i2p::ssu::SSUServer (m_Service, address.port);
m_SSUServer = new i2p::ssu::SSUServer (address.port);
LogPrint ("Start listening UDP port ", address.port);
m_SSUServer->Start ();
DetectExternalIP ();
Expand Down

0 comments on commit bf2e833

Please sign in to comment.