Browse Source

separate thread for SSU server

pull/70/head
orignal 11 years ago
parent
commit
bf2e833f26
  1. 31
      SSU.cpp
  2. 8
      SSU.h
  3. 2
      Transports.cpp

31
SSU.cpp

@ -992,8 +992,8 @@ namespace ssu
m_Server.Send (buf, msgSize, m_RemoteEndpoint); m_Server.Send (buf, msgSize, m_RemoteEndpoint);
} }
SSUServer::SSUServer (boost::asio::io_service& service, int port): SSUServer::SSUServer (int port): m_Thread (nullptr), m_Work (m_Service),
m_Endpoint (boost::asio::ip::udp::v4 (), port), m_Socket (service, m_Endpoint) 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::receive_buffer_size (65535));
m_Socket.set_option (boost::asio::socket_base::send_buffer_size (65535)); m_Socket.set_option (boost::asio::socket_base::send_buffer_size (65535));
@ -1007,13 +1007,38 @@ namespace ssu
void SSUServer::Start () 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 () void SSUServer::Stop ()
{ {
DeleteAllSessions (); DeleteAllSessions ();
m_IsRunning = false;
m_Service.stop ();
m_Socket.close (); 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) void SSUServer::AddRelay (uint32_t tag, const boost::asio::ip::udp::endpoint& relay)

8
SSU.h

@ -5,6 +5,7 @@
#include <map> #include <map>
#include <list> #include <list>
#include <set> #include <set>
#include <thread>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <cryptopp/modes.h> #include <cryptopp/modes.h>
#include <cryptopp/aes.h> #include <cryptopp/aes.h>
@ -154,7 +155,7 @@ namespace ssu
{ {
public: public:
SSUServer (boost::asio::io_service& service, int port); SSUServer (int port);
~SSUServer (); ~SSUServer ();
void Start (); void Start ();
void Stop (); void Stop ();
@ -172,11 +173,16 @@ namespace ssu
private: private:
void Run ();
void Receive (); void Receive ();
void HandleReceivedFrom (const boost::system::error_code& ecode, std::size_t bytes_transferred); void HandleReceivedFrom (const boost::system::error_code& ecode, std::size_t bytes_transferred);
private: 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::endpoint m_Endpoint;
boost::asio::ip::udp::socket m_Socket; boost::asio::ip::udp::socket m_Socket;
boost::asio::ip::udp::endpoint m_SenderEndpoint; boost::asio::ip::udp::endpoint m_SenderEndpoint;

2
Transports.cpp

@ -110,7 +110,7 @@ namespace i2p
{ {
if (!m_SSUServer) 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); LogPrint ("Start listening UDP port ", address.port);
m_SSUServer->Start (); m_SSUServer->Start ();
DetectExternalIP (); DetectExternalIP ();

Loading…
Cancel
Save