@ -7,6 +7,7 @@
# include <set>
# include <set>
# include <queue>
# include <queue>
# include <functional>
# include <functional>
# include <memory>
# include <boost/asio.hpp>
# include <boost/asio.hpp>
# include <boost/bind.hpp>
# include <boost/bind.hpp>
# include "I2PEndian.h"
# include "I2PEndian.h"
@ -78,7 +79,7 @@ namespace stream
} ;
} ;
class StreamingDestination ;
class StreamingDestination ;
class Stream
class Stream : public std : : enable_shared_from_this < Stream >
{
{
public :
public :
@ -153,7 +154,7 @@ namespace stream
{
{
public :
public :
typedef std : : function < void ( Stream * ) > Acceptor ;
typedef std : : function < void ( std : : shared_ptr < Stream > ) > Acceptor ;
StreamingDestination ( i2p : : client : : ClientDestination & owner ) : m_Owner ( owner ) { } ;
StreamingDestination ( i2p : : client : : ClientDestination & owner ) : m_Owner ( owner ) { } ;
~ StreamingDestination ( ) { } ;
~ StreamingDestination ( ) { } ;
@ -161,8 +162,8 @@ namespace stream
void Start ( ) ;
void Start ( ) ;
void Stop ( ) ;
void Stop ( ) ;
Stream * CreateNewOutgoingStream ( const i2p : : data : : LeaseSet & remote , int port = 0 ) ;
std : : shared_ptr < Stream > CreateNewOutgoingStream ( const i2p : : data : : LeaseSet & remote , int port = 0 ) ;
void DeleteStream ( Stream * stream ) ;
void DeleteStream ( std : : shared_ptr < Stream > stream ) ;
void SetAcceptor ( const Acceptor & acceptor ) { m_Acceptor = acceptor ; } ;
void SetAcceptor ( const Acceptor & acceptor ) { m_Acceptor = acceptor ; } ;
void ResetAcceptor ( ) { m_Acceptor = nullptr ; } ;
void ResetAcceptor ( ) { m_Acceptor = nullptr ; } ;
bool IsAcceptorSet ( ) const { return m_Acceptor ! = nullptr ; } ;
bool IsAcceptorSet ( ) const { return m_Acceptor ! = nullptr ; } ;
@ -173,13 +174,13 @@ namespace stream
private :
private :
void HandleNextPacket ( Packet * packet ) ;
void HandleNextPacket ( Packet * packet ) ;
Stream * CreateNewIncomingStream ( ) ;
std : : shared_ptr < Stream > CreateNewIncomingStream ( ) ;
private :
private :
i2p : : client : : ClientDestination & m_Owner ;
i2p : : client : : ClientDestination & m_Owner ;
std : : mutex m_StreamsMutex ;
std : : mutex m_StreamsMutex ;
std : : map < uint32_t , Stream * > m_Streams ;
std : : map < uint32_t , std : : shared_ptr < Stream > > m_Streams ;
Acceptor m_Acceptor ;
Acceptor m_Acceptor ;
public :
public :
@ -188,7 +189,7 @@ namespace stream
const decltype ( m_Streams ) & GetStreams ( ) const { return m_Streams ; } ;
const decltype ( m_Streams ) & GetStreams ( ) const { return m_Streams ; } ;
} ;
} ;
void DeleteStream ( Stream * stream ) ;
void DeleteStream ( std : : shared_ptr < Stream > stream ) ;
//-------------------------------------------------
//-------------------------------------------------
@ -197,15 +198,17 @@ namespace stream
{
{
if ( ! m_ReceiveQueue . empty ( ) )
if ( ! m_ReceiveQueue . empty ( ) )
{
{
m_Service . post ( [ = ] ( void ) { this - > HandleReceiveTimer (
auto s = shared_from_this ( ) ;
m_Service . post ( [ = ] ( void ) { s - > HandleReceiveTimer (
boost : : asio : : error : : make_error_code ( boost : : asio : : error : : operation_aborted ) ,
boost : : asio : : error : : make_error_code ( boost : : asio : : error : : operation_aborted ) ,
buffer , handler ) ; } ) ;
buffer , handler ) ; } ) ;
}
}
else
else
{
{
m_ReceiveTimer . expires_from_now ( boost : : posix_time : : seconds ( timeout ) ) ;
m_ReceiveTimer . expires_from_now ( boost : : posix_time : : seconds ( timeout ) ) ;
auto s = shared_from_this ( ) ;
m_ReceiveTimer . async_wait ( [ = ] ( const boost : : system : : error_code & ecode )
m_ReceiveTimer . async_wait ( [ = ] ( const boost : : system : : error_code & ecode )
{ thi s- > HandleReceiveTimer ( ecode , buffer , handler ) ; } ) ;
{ s - > HandleReceiveTimer ( ecode , buffer , handler ) ; } ) ;
}
}
}
}