Browse Source

moved NTCP client code to Transports

pull/113/head
orignal 10 years ago
parent
commit
0ccb66476e
  1. 35
      NTCPSession.cpp
  2. 20
      NTCPSession.h
  3. 30
      Transports.cpp
  4. 3
      Transports.h

35
NTCPSession.cpp

@ -658,40 +658,5 @@ namespace transport
m_Socket.close ();// invoke Terminate () from HandleReceive m_Socket.close ();// invoke Terminate () from HandleReceive
} }
} }
NTCPClient::NTCPClient (boost::asio::io_service& service, const boost::asio::ip::address& address,
int port, std::shared_ptr<const i2p::data::RouterInfo> in_RouterInfo):
NTCPSession (service, in_RouterInfo), m_Endpoint (address, port)
{
Connect ();
}
void NTCPClient::Connect ()
{
LogPrint ("Connecting to ", m_Endpoint.address ().to_string (),":", m_Endpoint.port ());
GetSocket ().async_connect (m_Endpoint, boost::bind (&NTCPClient::HandleConnect,
this, boost::asio::placeholders::error));
}
void NTCPClient::HandleConnect (const boost::system::error_code& ecode)
{
if (ecode)
{
LogPrint ("Connect error: ", ecode.message ());
if (ecode != boost::asio::error::operation_aborted)
{
i2p::data::netdb.SetUnreachable (GetRemoteIdentity ().GetIdentHash (), true);
Terminate ();
}
}
else
{
LogPrint ("Connected");
if (GetSocket ().local_endpoint ().protocol () == boost::asio::ip::tcp::v6()) // ipv6
context.UpdateNTCPV6Address (GetSocket ().local_endpoint ().address ());
ClientLogin ();
}
}
} }
} }

20
NTCPSession.h

@ -49,6 +49,7 @@ namespace transport
NTCPSession (boost::asio::io_service& service, std::shared_ptr<const i2p::data::RouterInfo> in_RemoteRouter = nullptr); NTCPSession (boost::asio::io_service& service, std::shared_ptr<const i2p::data::RouterInfo> in_RemoteRouter = nullptr);
~NTCPSession (); ~NTCPSession ();
void Terminate ();
boost::asio::ip::tcp::socket& GetSocket () { return m_Socket; }; boost::asio::ip::tcp::socket& GetSocket () { return m_Socket; };
bool IsEstablished () const { return m_IsEstablished; }; bool IsEstablished () const { return m_IsEstablished; };
@ -62,8 +63,7 @@ namespace transport
protected: protected:
void Terminate (); void Connected ();
virtual void Connected ();
void SendTimeSyncMessage (); void SendTimeSyncMessage ();
void SetIsEstablished (bool isEstablished) { m_IsEstablished = isEstablished; } void SetIsEstablished (bool isEstablished) { m_IsEstablished = isEstablished; }
@ -127,22 +127,6 @@ namespace transport
size_t m_NumSentBytes, m_NumReceivedBytes; size_t m_NumSentBytes, m_NumReceivedBytes;
}; };
class NTCPClient: public NTCPSession
{
public:
NTCPClient (boost::asio::io_service& service, const boost::asio::ip::address& address, int port, std::shared_ptr<const i2p::data::RouterInfo> in_RouterInfo);
private:
void Connect ();
void HandleConnect (const boost::system::error_code& ecode);
private:
boost::asio::ip::tcp::endpoint m_Endpoint;
};
} }
} }

30
Transports.cpp

@ -245,6 +245,33 @@ namespace transport
} }
} }
void Transports::Connect (const boost::asio::ip::address& address, int port, NTCPSession * conn)
{
LogPrint ("Connecting to ", address ,":", port);
conn->GetSocket ().async_connect (boost::asio::ip::tcp::endpoint (address, port),
boost::bind (&Transports::HandleConnect, this, boost::asio::placeholders::error, conn));
}
void Transports::HandleConnect (const boost::system::error_code& ecode, NTCPSession * conn)
{
if (ecode)
{
LogPrint ("Connect error: ", ecode.message ());
if (ecode != boost::asio::error::operation_aborted)
{
i2p::data::netdb.SetUnreachable (conn->GetRemoteIdentity ().GetIdentHash (), true);
conn->Terminate ();
}
}
else
{
LogPrint ("Connected");
if (conn->GetSocket ().local_endpoint ().protocol () == boost::asio::ip::tcp::v6()) // ipv6
context.UpdateNTCPV6Address (conn->GetSocket ().local_endpoint ().address ());
conn->ClientLogin ();
}
}
NTCPSession * Transports::GetNextNTCPSession () NTCPSession * Transports::GetNextNTCPSession ()
{ {
for (auto session: m_NTCPSessions) for (auto session: m_NTCPSessions)
@ -290,9 +317,10 @@ namespace transport
auto address = r->GetNTCPAddress (!context.SupportsV6 ()); auto address = r->GetNTCPAddress (!context.SupportsV6 ());
if (address && !r->UsesIntroducer () && !r->IsUnreachable () && msg->GetLength () < NTCP_MAX_MESSAGE_SIZE) if (address && !r->UsesIntroducer () && !r->IsUnreachable () && msg->GetLength () < NTCP_MAX_MESSAGE_SIZE)
{ {
auto s = new NTCPClient (m_Service, address->host, address->port, r); auto s = new NTCPSession (m_Service, r);
AddNTCPSession (s); AddNTCPSession (s);
s->SendI2NPMessage (msg); s->SendI2NPMessage (msg);
Connect (address->host, address->port, s);
} }
else else
{ {

3
Transports.h

@ -82,6 +82,9 @@ namespace transport
void PostMessage (const i2p::data::IdentHash& ident, i2p::I2NPMessage * msg); void PostMessage (const i2p::data::IdentHash& ident, i2p::I2NPMessage * msg);
void PostCloseSession (std::shared_ptr<const i2p::data::RouterInfo> router); void PostCloseSession (std::shared_ptr<const i2p::data::RouterInfo> router);
void Connect (const boost::asio::ip::address& address, int port, NTCPSession * conn);
void HandleConnect (const boost::system::error_code& ecode, NTCPSession * conn);
void DetectExternalIP (); void DetectExternalIP ();
private: private:

Loading…
Cancel
Save