Skip to content

Commit

Permalink
shared pointers for streams
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Nov 23, 2014
1 parent 4dc33a6 commit 4bd8b44
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 48 deletions.
2 changes: 1 addition & 1 deletion Destination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ namespace client
}
}

i2p::stream::Stream * ClientDestination::CreateStream (const i2p::data::LeaseSet& remote, int port)
std::shared_ptr<i2p::stream::Stream> ClientDestination::CreateStream (const i2p::data::LeaseSet& remote, int port)
{
if (m_StreamingDestination)
return m_StreamingDestination->CreateNewOutgoingStream (remote, port);
Expand Down
3 changes: 2 additions & 1 deletion Destination.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <thread>
#include <mutex>
#include <memory>
#include "Identity.h"
#include "TunnelPool.h"
#include "CryptoConst.h"
Expand Down Expand Up @@ -41,7 +42,7 @@ namespace client

// streaming
i2p::stream::StreamingDestination * GetStreamingDestination () const { return m_StreamingDestination; };
i2p::stream::Stream * CreateStream (const i2p::data::LeaseSet& remote, int port = 0);
std::shared_ptr<i2p::stream::Stream> CreateStream (const i2p::data::LeaseSet& remote, int port = 0);
void AcceptStreams (const i2p::stream::StreamingDestination::Acceptor& acceptor);
void StopAcceptingStreams ();
bool IsAcceptingStreams () const;
Expand Down
2 changes: 1 addition & 1 deletion HTTPServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ namespace util
{
m_Stream->Close ();
i2p::stream::DeleteStream (m_Stream);
m_Stream = nullptr;
m_Stream.reset ();
}
m_Socket->close ();
//delete this;
Expand Down
3 changes: 2 additions & 1 deletion HTTPServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <sstream>
#include <thread>
#include <memory>
#include <boost/asio.hpp>
#include <boost/array.hpp>
#include "LeaseSet.h"
Expand Down Expand Up @@ -79,7 +80,7 @@ namespace util

boost::asio::ip::tcp::socket * m_Socket;
boost::asio::deadline_timer m_Timer;
i2p::stream::Stream * m_Stream;
std::shared_ptr<i2p::stream::Stream> m_Stream;
char m_Buffer[HTTP_CONNECTION_BUFFER_SIZE + 1], m_StreamBuffer[HTTP_CONNECTION_BUFFER_SIZE + 1];
size_t m_BufferLen;
request m_Request;
Expand Down
6 changes: 3 additions & 3 deletions I2PTunnel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace client
Receive ();
}

I2PTunnelConnection::I2PTunnelConnection (I2PTunnel * owner, i2p::stream::Stream * stream,
I2PTunnelConnection::I2PTunnelConnection (I2PTunnel * owner, std::shared_ptr<i2p::stream::Stream> stream,
boost::asio::ip::tcp::socket * socket, const boost::asio::ip::tcp::endpoint& target):
m_Socket (socket), m_Stream (stream), m_Owner (owner)
{
Expand All @@ -40,7 +40,7 @@ namespace client
{
m_Stream->Close ();
i2p::stream::DeleteStream (m_Stream);
m_Stream = nullptr;
m_Stream.reset ();
}
m_Socket->close ();
if (m_Owner)
Expand Down Expand Up @@ -275,7 +275,7 @@ namespace client
LogPrint ("Local destination not set for server tunnel");
}

void I2PServerTunnel::HandleAccept (i2p::stream::Stream * stream)
void I2PServerTunnel::HandleAccept (std::shared_ptr<i2p::stream::Stream> stream)
{
if (stream)
new I2PTunnelConnection (this, stream, new boost::asio::ip::tcp::socket (GetService ()), m_Endpoint);
Expand Down
7 changes: 4 additions & 3 deletions I2PTunnel.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <inttypes.h>
#include <string>
#include <set>
#include <memory>
#include <boost/asio.hpp>
#include "Identity.h"
#include "Destination.h"
Expand All @@ -24,7 +25,7 @@ namespace client

I2PTunnelConnection (I2PTunnel * owner, boost::asio::ip::tcp::socket * socket,
const i2p::data::LeaseSet * leaseSet);
I2PTunnelConnection (I2PTunnel * owner, i2p::stream::Stream * stream, boost::asio::ip::tcp::socket * socket,
I2PTunnelConnection (I2PTunnel * owner, std::shared_ptr<i2p::stream::Stream> stream, boost::asio::ip::tcp::socket * socket,
const boost::asio::ip::tcp::endpoint& target);
~I2PTunnelConnection ();

Expand All @@ -44,7 +45,7 @@ namespace client

uint8_t m_Buffer[I2P_TUNNEL_CONNECTION_BUFFER_SIZE], m_StreamBuffer[I2P_TUNNEL_CONNECTION_BUFFER_SIZE];
boost::asio::ip::tcp::socket * m_Socket;
i2p::stream::Stream * m_Stream;
std::shared_ptr<i2p::stream::Stream> m_Stream;
I2PTunnel * m_Owner;
};

Expand Down Expand Up @@ -111,7 +112,7 @@ namespace client
private:

void Accept ();
void HandleAccept (i2p::stream::Stream * stream);
void HandleAccept (std::shared_ptr<i2p::stream::Stream> stream);

private:

Expand Down
4 changes: 2 additions & 2 deletions SAM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace client
{
m_Stream->Close ();
i2p::stream::DeleteStream (m_Stream);
m_Stream = nullptr;
m_Stream.reset ();
}
}

Expand Down Expand Up @@ -531,7 +531,7 @@ namespace client
I2PReceive ();
}

void SAMSocket::HandleI2PAccept (i2p::stream::Stream * stream)
void SAMSocket::HandleI2PAccept (std::shared_ptr<i2p::stream::Stream> stream)
{
if (stream)
{
Expand Down
4 changes: 2 additions & 2 deletions SAM.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ namespace client

void I2PReceive ();
void HandleI2PReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred);
void HandleI2PAccept (i2p::stream::Stream * stream);
void HandleI2PAccept (std::shared_ptr<i2p::stream::Stream> stream);
void HandleWriteI2PData (const boost::system::error_code& ecode);
void HandleI2PDatagramReceive (const i2p::data::IdentityEx& ident, const uint8_t * buf, size_t len);

Expand Down Expand Up @@ -118,7 +118,7 @@ namespace client
SAMSocketType m_SocketType;
std::string m_ID; // nickname
bool m_IsSilent;
i2p::stream::Stream * m_Stream;
std::shared_ptr<i2p::stream::Stream> m_Stream;
SAMSession * m_Session;
};

Expand Down
3 changes: 1 addition & 2 deletions SOCKS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ namespace proxy
{
if (m_stream) {
LogPrint("--- socks4a close stream");
delete m_stream;
m_stream = nullptr;
m_stream.reset ();
}
}

Expand Down
4 changes: 2 additions & 2 deletions SOCKS.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <boost/asio.hpp>
#include <vector>
#include <mutex>

#include <memory>
#include "Identity.h"
#include "Streaming.h"

Expand Down Expand Up @@ -47,7 +47,7 @@ namespace proxy
boost::asio::io_service * m_ios;
boost::asio::ip::tcp::socket * m_sock;
boost::asio::deadline_timer m_ls_timer;
i2p::stream::Stream * m_stream;
std::shared_ptr<i2p::stream::Stream> m_stream;
i2p::data::LeaseSet * m_ls;
i2p::data::IdentHash m_dest;
state m_state;
Expand Down
24 changes: 8 additions & 16 deletions Streaming.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ namespace stream
m_IsAckSendScheduled = true;
m_AckSendTimer.expires_from_now (boost::posix_time::milliseconds(ACK_SEND_TIMEOUT));
m_AckSendTimer.async_wait (boost::bind (&Stream::HandleAckSendTimer,
this, boost::asio::placeholders::error));
shared_from_this (), boost::asio::placeholders::error));
}
}
else if (isSyn)
Expand Down Expand Up @@ -461,7 +461,7 @@ namespace stream
m_ResendTimer.cancel ();
m_ResendTimer.expires_from_now (boost::posix_time::seconds(RESEND_TIMEOUT));
m_ResendTimer.async_wait (boost::bind (&Stream::HandleResendTimer,
this, boost::asio::placeholders::error));
shared_from_this (), boost::asio::placeholders::error));
}

void Stream::HandleResendTimer (const boost::system::error_code& ecode)
Expand Down Expand Up @@ -563,8 +563,6 @@ namespace stream
ResetAcceptor ();
{
std::unique_lock<std::mutex> l(m_StreamsMutex);
for (auto it: m_Streams)
delete it.second;
m_Streams.clear ();
}
}
Expand Down Expand Up @@ -597,36 +595,30 @@ namespace stream
}
}

Stream * StreamingDestination::CreateNewOutgoingStream (const i2p::data::LeaseSet& remote, int port)
std::shared_ptr<Stream> StreamingDestination::CreateNewOutgoingStream (const i2p::data::LeaseSet& remote, int port)
{
Stream * s = new Stream (*m_Owner.GetService (), *this, remote, port);
auto s = std::make_shared<Stream> (*m_Owner.GetService (), *this, remote, port);
std::unique_lock<std::mutex> l(m_StreamsMutex);
m_Streams[s->GetRecvStreamID ()] = s;
return s;
}

Stream * StreamingDestination::CreateNewIncomingStream ()
std::shared_ptr<Stream> StreamingDestination::CreateNewIncomingStream ()
{
Stream * s = new Stream (*m_Owner.GetService (), *this);
auto s = std::make_shared<Stream> (*m_Owner.GetService (), *this);
std::unique_lock<std::mutex> l(m_StreamsMutex);
m_Streams[s->GetRecvStreamID ()] = s;
return s;
}

void StreamingDestination::DeleteStream (Stream * stream)
void StreamingDestination::DeleteStream (std::shared_ptr<Stream> stream)
{
if (stream)
{
std::unique_lock<std::mutex> l(m_StreamsMutex);
auto it = m_Streams.find (stream->GetRecvStreamID ());
if (it != m_Streams.end ())
{
m_Streams.erase (it);
if (m_Owner.GetService ())
m_Owner.GetService ()->post ([stream](void) { delete stream; });
else
delete stream;
}
}
}

Expand All @@ -651,7 +643,7 @@ namespace stream
}
}

void DeleteStream (Stream * stream)
void DeleteStream (std::shared_ptr<Stream> stream)
{
if (stream)
stream->GetLocalDestination ().DeleteStream (stream);
Expand Down
21 changes: 12 additions & 9 deletions Streaming.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <set>
#include <queue>
#include <functional>
#include <memory>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include "I2PEndian.h"
Expand Down Expand Up @@ -78,7 +79,7 @@ namespace stream
};

class StreamingDestination;
class Stream
class Stream: public std::enable_shared_from_this<Stream>
{
public:

Expand Down Expand Up @@ -153,16 +154,16 @@ namespace stream
{
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 () {};

void Start ();
void Stop ();

Stream * CreateNewOutgoingStream (const i2p::data::LeaseSet& remote, int port = 0);
void DeleteStream (Stream * stream);
std::shared_ptr<Stream> CreateNewOutgoingStream (const i2p::data::LeaseSet& remote, int port = 0);
void DeleteStream (std::shared_ptr<Stream> stream);
void SetAcceptor (const Acceptor& acceptor) { m_Acceptor = acceptor; };
void ResetAcceptor () { m_Acceptor = nullptr; };
bool IsAcceptorSet () const { return m_Acceptor != nullptr; };
Expand All @@ -173,13 +174,13 @@ namespace stream
private:

void HandleNextPacket (Packet * packet);
Stream * CreateNewIncomingStream ();
std::shared_ptr<Stream> CreateNewIncomingStream ();

private:

i2p::client::ClientDestination& m_Owner;
std::mutex m_StreamsMutex;
std::map<uint32_t, Stream *> m_Streams;
std::map<uint32_t, std::shared_ptr<Stream> > m_Streams;
Acceptor m_Acceptor;

public:
Expand All @@ -188,7 +189,7 @@ namespace stream
const decltype(m_Streams)& GetStreams () const { return m_Streams; };
};

void DeleteStream (Stream * stream);
void DeleteStream (std::shared_ptr<Stream> stream);

//-------------------------------------------------

Expand All @@ -197,15 +198,17 @@ namespace stream
{
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),
buffer, handler); });
}
else
{
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)
{ this->HandleReceiveTimer (ecode, buffer, handler); });
{ s->HandleReceiveTimer (ecode, buffer, handler); });
}
}

Expand Down
4 changes: 2 additions & 2 deletions api/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ namespace api
i2p::data::netdb.RequestDestination (remote, true, dest->GetTunnelPool ());
}

i2p::stream::Stream * CreateStream (i2p::client::ClientDestination * dest, const i2p::data::IdentHash& remote)
std::shared_ptr<i2p::stream::Stream> CreateStream (i2p::client::ClientDestination * dest, const i2p::data::IdentHash& remote)
{
auto leaseSet = i2p::data::netdb.FindLeaseSet (remote);
if (leaseSet)
Expand All @@ -95,7 +95,7 @@ namespace api
dest->AcceptStreams (acceptor);
}

void DestroyStream (i2p::stream::Stream * stream)
void DestroyStream (std::shared_ptr<i2p::stream::Stream> stream)
{
if (stream)
{
Expand Down
7 changes: 4 additions & 3 deletions api/api.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef API_H__
#define API_H__

#include <memory>
#include "Identity.h"
#include "Destination.h"
#include "Streaming.h"
Expand All @@ -16,14 +17,14 @@ namespace api

// destinations
i2p::client::ClientDestination * CreateLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic = true);
i2p::client::ClientDestination * CreateLocalDestination (bool isPublic = false, i2p::data::SigningKeyType sigType = i2p::data::SIGNING_KEY_TYPE_DSA_SHA1); // transient destinations usually not published
i2p::client::ClientDestination * CreateLocalDestination (bool isPublic = false, i2p::data::SigningKeyType sigType = i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA256_P256); // transient destinations usually not published
void DestoroyLocalDestination (i2p::client::ClientDestination * dest);

// streams
void RequestLeaseSet (i2p::client::ClientDestination * dest, const i2p::data::IdentHash& remote);
i2p::stream::Stream * CreateStream (i2p::client::ClientDestination * dest, const i2p::data::IdentHash& remote);
std::shared_ptr<i2p::stream::Stream> CreateStream (i2p::client::ClientDestination * dest, const i2p::data::IdentHash& remote);
void AcceptStream (i2p::client::ClientDestination * dest, const i2p::stream::StreamingDestination::Acceptor& acceptor);
void DestroyStream (i2p::stream::Stream * stream);
void DestroyStream (std::shared_ptr<i2p::stream::Stream> stream);
}
}

Expand Down

0 comments on commit 4bd8b44

Please sign in to comment.