Skip to content

Commit

Permalink
use shared_ptr for libtorrent::session. fix #217.
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelfreitas committed Jun 20, 2014
1 parent e3ab5f8 commit 2d23677
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions src/twister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using namespace json_spirit;
using namespace std;

#include <boost/shared_ptr.hpp>
#include <boost/filesystem.hpp>
#ifdef HAVE_BOOST_LOCALE
#include <boost/locale.hpp>
Expand Down Expand Up @@ -42,7 +43,7 @@ twister::twister()
//#define DEBUG_NEIGHBOR_TORRENT 1

using namespace libtorrent;
static session *ses = NULL;
static boost::shared_ptr<session> m_ses;
static bool m_shuttingDownSession = false;
static bool m_usingProxy;
static int num_outstanding_resume_data;
Expand Down Expand Up @@ -105,7 +106,8 @@ sha1_hash dhtTargetHash(std::string const &username, std::string const &resource
torrent_handle startTorrentUser(std::string const &username, bool following)
{
bool userInTxDb = usernameExists(username); // keep this outside cs_twister to avoid deadlock
if( !userInTxDb || !ses)
boost::shared_ptr<session> ses(m_ses);
if( !userInTxDb || !ses )
return torrent_handle();

LOCK(cs_twister);
Expand Down Expand Up @@ -258,11 +260,12 @@ void ThreadWaitExtIP()
ipStr.c_str(), !m_usingProxy ? listen_port : 0,
m_usingProxy ? proxyInfoOut.first.ToStringIPPort().c_str() : "");

ses = new session(*m_swarmDb, fingerprint("TW", LIBTORRENT_VERSION_MAJOR, LIBTORRENT_VERSION_MINOR, 0, 0)
m_ses.reset(new session(*m_swarmDb, fingerprint("TW", LIBTORRENT_VERSION_MAJOR, LIBTORRENT_VERSION_MINOR, 0, 0)
, session::add_default_plugins
, alert::dht_notification
, ipStr.size() ? ipStr.c_str() : NULL
, !m_usingProxy ? std::make_pair(listen_port, listen_port) : std::make_pair(0, 0) );
, !m_usingProxy ? std::make_pair(listen_port, listen_port) : std::make_pair(0, 0) ));
boost::shared_ptr<session> ses(m_ses);

if( m_usingProxy ) {
proxy_settings proxy;
Expand Down Expand Up @@ -385,6 +388,7 @@ bool yes(libtorrent::torrent_status const&)

void saveTorrentResumeData()
{
boost::shared_ptr<session> ses(m_ses);
if( ses ){
printf("saving resume data\n");
std::vector<torrent_status> temp;
Expand Down Expand Up @@ -428,6 +432,7 @@ void lockAndSaveUserData()

int getDhtNodes(boost::int64_t *dht_global_nodes)
{
boost::shared_ptr<session> ses(m_ses);
if( !ses )
return 0;
session_status ss = ses->status();
Expand All @@ -440,14 +445,16 @@ void ThreadMaintainDHTNodes()
{
SimpleThreadCounter threadCounter(&cs_twister, &m_threadsToJoin, "maintain-dht-nodes");

while(!ses && !m_shuttingDownSession) {
while(!m_ses && !m_shuttingDownSession) {
MilliSleep(200);
}

int64 lastSaveResumeTime = GetTime();
int lastTotalNodesCandidates = 0;

while(ses && !m_shuttingDownSession) {
while(m_ses && !m_shuttingDownSession) {
boost::shared_ptr<session> ses(m_ses);

session_status ss = ses->status();
int dht_nodes = ss.dht_nodes;
bool nodesAdded = false;
Expand Down Expand Up @@ -546,6 +553,7 @@ void ThreadMaintainDHTNodes()
lockAndSaveUserData();
}

ses.reset();
MilliSleep(5000);
}
}
Expand All @@ -557,11 +565,11 @@ void ThreadSessionAlerts()

SimpleThreadCounter threadCounter(&cs_twister, &m_threadsToJoin, "session-alerts");


while(!ses && !m_shuttingDownSession) {
while(!m_ses && !m_shuttingDownSession) {
MilliSleep(200);
}
while (ses && !m_shuttingDownSession) {
while (m_ses && !m_shuttingDownSession) {
boost::shared_ptr<session> ses(m_ses);
alert const* a = ses->wait_for_alert(seconds(1));
if (a == 0) continue;

Expand Down Expand Up @@ -752,8 +760,8 @@ void startSessionTorrent(boost::thread_group& threadGroup)

void stopSessionTorrent()
{
if( ses ){
ses->pause();
if( m_ses ){
m_ses->pause();

saveTorrentResumeData();

Expand All @@ -777,7 +785,7 @@ void stopSessionTorrent()
printf("\nsaving session state\n");

entry session_state;
ses->save_state(session_state,
m_ses->save_state(session_state,
session::save_settings |
session::save_dht_settings |
session::save_dht_state |
Expand All @@ -790,10 +798,9 @@ void stopSessionTorrent()
boost::filesystem::path sesStatePath = GetDataDir() / "ses_state";
save_file(sesStatePath.string(), out);

ses->stop_dht();
m_ses->stop_dht();

delete ses;
ses = NULL;
m_ses.reset();
}

boost::filesystem::path globalDataPath = GetDataDir() / GLOBAL_DATA_FILE;
Expand Down Expand Up @@ -1319,6 +1326,7 @@ Value dhtput(const Array& params, bool fHelp)
"dhtput <username> <resource> <s(ingle)/m(ulti)> <value> <sig_user> <seq>\n"
"Store resource in dht network");

boost::shared_ptr<session> ses(m_ses);
if( !ses )
return Value();

Expand Down Expand Up @@ -1365,6 +1373,7 @@ Value dhtget(const Array& params, bool fHelp)
"dhtget <username> <resource> <s(ingle)/m(ulti)> [timeout_ms] [timeout_multi_ms] [min_multi]\n"
"Get resource from dht network");

boost::shared_ptr<session> ses(m_ses);
if( !ses )
return Array();

Expand Down Expand Up @@ -1479,6 +1488,10 @@ Value newpostmsg(const Array& params, bool fHelp)
"newpostmsg <username> <k> <msg> [reply_n] [reply_k]\n"
"Post a new message to swarm");

boost::shared_ptr<session> ses(m_ses);
if( !ses )
return Array();

EnsureWalletIsUnlocked();

string strUsername = params[0].get_str();
Expand Down Expand Up @@ -1616,6 +1629,10 @@ Value newrtmsg(const Array& params, bool fHelp)
"newrtmsg <username> <k> <rt_v_object>\n"
"Post a new RT to swarm");

boost::shared_ptr<session> ses(m_ses);
if( !ses )
return Array();

EnsureWalletIsUnlocked();

string strUsername = params[0].get_str();
Expand Down

0 comments on commit 2d23677

Please sign in to comment.