request destination LeaseSet and wait for 5 seconds if not found

This commit is contained in:
orignal 2014-10-02 12:42:28 -04:00
parent ef2932a234
commit 9b0e8f6a71
2 changed files with 40 additions and 12 deletions

44
SAM.cpp
View File

@ -12,8 +12,8 @@ namespace i2p
namespace stream namespace stream
{ {
SAMSocket::SAMSocket (SAMBridge& owner): SAMSocket::SAMSocket (SAMBridge& owner):
m_Owner (owner), m_Socket (m_Owner.GetService ()), m_SocketType (eSAMSocketTypeUnknown), m_Owner (owner), m_Socket (m_Owner.GetService ()), m_Timer (m_Owner.GetService ()),
m_IsSilent (false), m_Stream (nullptr) m_SocketType (eSAMSocketTypeUnknown), m_IsSilent (false), m_Stream (nullptr)
{ {
} }
@ -245,24 +245,44 @@ namespace stream
dest.FromBuffer (ident, l); dest.FromBuffer (ident, l);
auto leaseSet = i2p::data::netdb.FindLeaseSet (dest.GetIdentHash ()); auto leaseSet = i2p::data::netdb.FindLeaseSet (dest.GetIdentHash ());
if (leaseSet) if (leaseSet)
{ Connect (*leaseSet, session);
m_SocketType = eSAMSocketTypeStream;
session->sockets.push_back (this);
m_Stream = session->localDestination->CreateNewOutgoingStream (*leaseSet);
m_Stream->Send ((uint8_t *)m_Buffer, 0); // connect
I2PReceive ();
SendMessageReply (SAM_STREAM_STATUS_OK, strlen(SAM_STREAM_STATUS_OK), false);
}
else else
{ {
i2p::data::netdb.Subscribe (dest.GetIdentHash ()); i2p::data::netdb.Subscribe (dest.GetIdentHash (), session->localDestination->GetTunnelPool ());
SendMessageReply (SAM_STREAM_STATUS_CANT_REACH_PEER, strlen(SAM_STREAM_STATUS_CANT_REACH_PEER), true); m_Timer.expires_from_now (boost::posix_time::seconds(SAM_CONNECT_TIMEOUT));
m_Timer.async_wait (boost::bind (&SAMSocket::HandleDestinationRequestTimer,
this, boost::asio::placeholders::error, dest.GetIdentHash (), session));
} }
} }
else else
SendMessageReply (SAM_STREAM_STATUS_INVALID_ID, strlen(SAM_STREAM_STATUS_INVALID_ID), true); SendMessageReply (SAM_STREAM_STATUS_INVALID_ID, strlen(SAM_STREAM_STATUS_INVALID_ID), true);
} }
void SAMSocket::Connect (const i2p::data::LeaseSet& remote, SAMSession * session)
{
m_SocketType = eSAMSocketTypeStream;
session->sockets.push_back (this);
m_Stream = session->localDestination->CreateNewOutgoingStream (remote);
m_Stream->Send ((uint8_t *)m_Buffer, 0); // connect
I2PReceive ();
SendMessageReply (SAM_STREAM_STATUS_OK, strlen(SAM_STREAM_STATUS_OK), false);
}
void SAMSocket::HandleDestinationRequestTimer (const boost::system::error_code& ecode, i2p::data::IdentHash ident, SAMSession * session)
{
if (!ecode) // timeout expired
{
auto leaseSet = i2p::data::netdb.FindLeaseSet (ident);
if (leaseSet)
Connect (*leaseSet, session);
else
{
LogPrint ("SAM destination to connect not found");
SendMessageReply (SAM_STREAM_STATUS_CANT_REACH_PEER, strlen(SAM_STREAM_STATUS_CANT_REACH_PEER), true);
}
}
}
void SAMSocket::ProcessStreamAccept (char * buf, size_t len) void SAMSocket::ProcessStreamAccept (char * buf, size_t len)
{ {
LogPrint ("SAM stream accept: ", buf); LogPrint ("SAM stream accept: ", buf);

8
SAM.h
View File

@ -7,6 +7,8 @@
#include <list> #include <list>
#include <thread> #include <thread>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include "Identity.h"
#include "LeaseSet.h"
#include "Streaming.h" #include "Streaming.h"
namespace i2p namespace i2p
@ -15,6 +17,7 @@ namespace stream
{ {
const size_t SAM_SOCKET_BUFFER_SIZE = 4096; const size_t SAM_SOCKET_BUFFER_SIZE = 4096;
const int SAM_SOCKET_CONNECTION_MAX_IDLE = 3600; // in seconds const int SAM_SOCKET_CONNECTION_MAX_IDLE = 3600; // in seconds
const int SAM_CONNECT_TIMEOUT = 5; // in seconds
const char SAM_HANDSHAKE[] = "HELLO VERSION"; const char SAM_HANDSHAKE[] = "HELLO VERSION";
const char SAM_HANDSHAKE_REPLY[] = "HELLO REPLY RESULT=OK VERSION=3.1\n"; const char SAM_HANDSHAKE_REPLY[] = "HELLO REPLY RESULT=OK VERSION=3.1\n";
const char SAM_SESSION_CREATE[] = "SESSION CREATE"; const char SAM_SESSION_CREATE[] = "SESSION CREATE";
@ -47,6 +50,7 @@ namespace stream
}; };
class SAMBridge; class SAMBridge;
class SAMSession;
class SAMSocket class SAMSocket
{ {
public: public:
@ -79,10 +83,14 @@ namespace stream
void ProcessDestGenerate (); void ProcessDestGenerate ();
void ExtractParams (char * buf, size_t len, std::map<std::string, std::string>& params); void ExtractParams (char * buf, size_t len, std::map<std::string, std::string>& params);
void Connect (const i2p::data::LeaseSet& remote, SAMSession * session);
void HandleDestinationRequestTimer (const boost::system::error_code& ecode, i2p::data::IdentHash ident, SAMSession * session);
private: private:
SAMBridge& m_Owner; SAMBridge& m_Owner;
boost::asio::ip::tcp::socket m_Socket; boost::asio::ip::tcp::socket m_Socket;
boost::asio::deadline_timer m_Timer;
char m_Buffer[SAM_SOCKET_BUFFER_SIZE + 1]; char m_Buffer[SAM_SOCKET_BUFFER_SIZE + 1];
uint8_t m_StreamBuffer[SAM_SOCKET_BUFFER_SIZE]; uint8_t m_StreamBuffer[SAM_SOCKET_BUFFER_SIZE];
SAMSocketType m_SocketType; SAMSocketType m_SocketType;