display NTCP2 session in web console

This commit is contained in:
orignal 2018-07-18 12:58:29 -04:00
parent fc52b2b940
commit 910a9600bd
3 changed files with 112 additions and 38 deletions

View file

@ -154,6 +154,9 @@ namespace transport
m_IsTerminated = true;
m_IsEstablished = false;
m_Socket.close ();
transports.PeerDisconnected (shared_from_this ());
m_Server.RemoveNTCP2Session (shared_from_this ());
m_SendQueue.clear ();
LogPrint (eLogDebug, "NTCP2: session terminated");
}
}
@ -741,6 +744,14 @@ namespace transport
void NTCP2Server::Stop ()
{
{
// we have to copy it because Terminate changes m_NTCP2Sessions
auto ntcpSessions = m_NTCP2Sessions;
for (auto& it: ntcpSessions)
it.second->Terminate ();
}
m_NTCP2Sessions.clear ();
if (m_IsRunning)
{
m_IsRunning = false;
@ -769,12 +780,44 @@ namespace transport
}
}
bool NTCP2Server::AddNTCP2Session (std::shared_ptr<NTCP2Session> session)
{
if (!session || !session->GetRemoteIdentity ()) return false;
auto& ident = session->GetRemoteIdentity ()->GetIdentHash ();
auto it = m_NTCP2Sessions.find (ident);
if (it != m_NTCP2Sessions.end ())
{
LogPrint (eLogWarning, "NTCP2: session to ", ident.ToBase64 (), " already exists");
session->Terminate();
return false;
}
m_NTCP2Sessions.insert (std::make_pair (ident, session));
return true;
}
void NTCP2Server::RemoveNTCP2Session (std::shared_ptr<NTCP2Session> session)
{
if (session && session->GetRemoteIdentity ())
m_NTCP2Sessions.erase (session->GetRemoteIdentity ()->GetIdentHash ());
}
std::shared_ptr<NTCP2Session> NTCP2Server::FindNTCP2Session (const i2p::data::IdentHash& ident)
{
auto it = m_NTCP2Sessions.find (ident);
if (it != m_NTCP2Sessions.end ())
return it->second;
return nullptr;
}
void NTCP2Server::Connect(const boost::asio::ip::address & address, uint16_t port, std::shared_ptr<NTCP2Session> conn)
{
LogPrint (eLogDebug, "NTCP2: Connecting to ", address ,":", port);
m_Service.post([this, address, port, conn]()
{
conn->GetSocket ().async_connect (boost::asio::ip::tcp::endpoint (address, port), std::bind (&NTCP2Server::HandleConnect, this, std::placeholders::_1, conn));
if (this->AddNTCP2Session (conn))
{
conn->GetSocket ().async_connect (boost::asio::ip::tcp::endpoint (address, port), std::bind (&NTCP2Server::HandleConnect, this, std::placeholders::_1, conn));
}
});
}

View file

@ -5,6 +5,7 @@
#include <memory>
#include <thread>
#include <list>
#include <map>
#include <openssl/bn.h>
#include <boost/asio.hpp>
#include "RouterInfo.h"
@ -70,6 +71,9 @@ namespace transport
boost::asio::ip::tcp::socket& GetSocket () { return m_Socket; };
bool IsEstablished () const { return m_IsEstablished; };
bool IsTerminated () const { return m_IsTerminated; };
void ClientLogin (); // Alice
void ServerLogin (); // Bob
void SendI2NPMessages (const std::vector<std::shared_ptr<I2NPMessage> >& msgs);
@ -139,6 +143,10 @@ namespace transport
void Start ();
void Stop ();
bool AddNTCP2Session (std::shared_ptr<NTCP2Session> session);
void RemoveNTCP2Session (std::shared_ptr<NTCP2Session> session);
std::shared_ptr<NTCP2Session> FindNTCP2Session (const i2p::data::IdentHash& ident);
boost::asio::io_service& GetService () { return m_Service; };
void Connect(const boost::asio::ip::address & address, uint16_t port, std::shared_ptr<NTCP2Session> conn);
@ -154,6 +162,12 @@ namespace transport
std::thread * m_Thread;
boost::asio::io_service m_Service;
boost::asio::io_service::work m_Work;
std::map<i2p::data::IdentHash, std::shared_ptr<NTCP2Session> > m_NTCP2Sessions;
public:
// for HTTP/I2PControl
const decltype(m_NTCP2Sessions)& GetNTCP2Sessions () const { return m_NTCP2Sessions; };
};
}
}