tr_bool haveSentBitTorrentHandshake;
tr_peerIo * io;
tr_crypto * crypto;
- struct tr_handle * handle;
+ tr_session * session;
uint8_t myPublicKey[KEY_LEN];
uint8_t mySecret[KEY_LEN];
uint8_t state;
uint8_t * buf = tr_new0( uint8_t, HANDSHAKE_SIZE );
uint8_t * walk = buf;
const uint8_t * torrentHash = tr_cryptoGetTorrentHash( handshake->crypto );
- const tr_torrent * tor = tr_torrentFindFromHash( handshake->handle, torrentHash );
+ const tr_torrent * tor = tr_torrentFindFromHash( handshake->session, torrentHash );
const uint8_t * peer_id = tor && tor->peer_id ? tor->peer_id : tr_getPeerId( );
memcpy( walk, HANDSHAKE_NAME, HANDSHAKE_NAME_LEN );
/* torrent hash */
tr_peerIoReadBytes( handshake->io, inbuf, hash, sizeof( hash ) );
assert( tr_peerIoHasTorrentHash( handshake->io ) );
- if( !tr_torrentExists( handshake->handle, hash )
+ if( !tr_torrentExists( handshake->session, hash )
|| memcmp( hash, tr_peerIoGetTorrentHash( handshake->io ),
SHA_DIGEST_LENGTH ) )
{
dbgmsg( handshake, "peer-id is [%*.*s]", PEER_ID_LEN, PEER_ID_LEN,
handshake->peer_id );
- tor = tr_torrentFindFromHash( handshake->handle, hash );
+ tor = tr_torrentFindFromHash( handshake->session, hash );
peer_id = tor && tor->peer_id ? tor->peer_id : tr_getPeerId( );
if( !memcmp( handshake->peer_id, peer_id, PEER_ID_LEN ) )
{
tr_peerIoReadBytes( handshake->io, inbuf, hash, sizeof( hash ) );
if( tr_peerIoIsIncoming( handshake->io ) )
{
- if( !tr_torrentExists( handshake->handle, hash ) )
+ if( !tr_torrentExists( handshake->session, hash ) )
{
dbgmsg( handshake, "peer is trying to connect to us for a torrent we don't have." );
return tr_handshakeDone( handshake, FALSE );
tr_peerIoIsIncoming( handshake->io ) );
/* if we've somehow connected to ourselves, don't keep the connection */
- tor = tr_torrentFindFromHash( handshake->handle, tr_peerIoGetTorrentHash( handshake->io ) );
+ tor = tr_torrentFindFromHash( handshake->session, tr_peerIoGetTorrentHash( handshake->io ) );
peer_id = tor && tor->peer_id ? tor->peer_id : tr_getPeerId( );
peerIsGood = memcmp( handshake->peer_id, peer_id, PEER_ID_LEN ) != 0;
dbgmsg( handshake, "isPeerGood == %d", peerIsGood );
for( i = 0; i < SHA_DIGEST_LENGTH; ++i )
obfuscatedTorrentHash[i] = req2[i] ^ req3[i];
if( ( tor =
- tr_torrentFindFromObfuscatedHash( handshake->handle,
+ tr_torrentFindFromObfuscatedHash( handshake->session,
obfuscatedTorrentHash ) ) )
{
dbgmsg( handshake, "got INCOMING connection's encrypted handshake for torrent [%s]",
tor->info.name );
tr_peerIoSetTorrentHash( handshake->io, tor->info.hash );
if( !tr_torrentAllowsPex( tor )
- && tr_peerMgrPeerIsSeed( handshake->handle->peerMgr,
+ && tr_peerMgrPeerIsSeed( handshake->session->peerMgr,
tor->info.hash,
tr_peerIoGetAddress( handshake->io, NULL ) ) )
{
handshake->encryptionMode = encryptionMode;
handshake->doneCB = doneCB;
handshake->doneUserData = doneUserData;
- handshake->handle = tr_peerIoGetSession( io );
+ handshake->session = tr_peerIoGetSession( io );
tr_peerIoSetTimeoutSecs( io, 15 );
tr_peerIoSetIOFuncs( handshake->io, canRead, NULL, gotError, handshake );
int timeout_write; /* in seconds */
short enabled; /* events that are currently enabled */
- struct tr_handle * session;
+ tr_session * session;
struct tr_bandwidth * bandwidth;
};
*/
struct tr_iobuf *
-tr_iobuf_new( struct tr_handle * session,
+tr_iobuf_new( tr_session * session,
tr_bandwidth * bandwidth,
int fd,
short event,
typedef void (*tr_iobuf_error_cb)( struct tr_iobuf*, short what, void* );
/** @brief create a new tr_iobuf object. */
-struct tr_iobuf* tr_iobuf_new( struct tr_handle * session,
+struct tr_iobuf* tr_iobuf_new( tr_session * session,
struct tr_bandwidth * bandwidth,
int fd,
short event,
}
tr_metainfo_builder*
-tr_metaInfoBuilderCreate( tr_handle * handle,
+tr_metaInfoBuilderCreate( tr_session * session,
const char * topFile )
{
int i;
tr_metainfo_builder * ret = tr_new0( tr_metainfo_builder, 1 );
ret->top = tr_strdup( topFile );
- ret->handle = handle;
+ ret->handle = session;
{
struct stat sb;
stat( topFile, &sb );
static tr_thread * workerThread = NULL;
static tr_lock*
-getQueueLock( tr_handle * h )
+getQueueLock( tr_session * session )
{
static tr_lock * lock = NULL;
+ tr_globalLock( session );
- tr_globalLock( h );
if( !lock )
lock = tr_lockNew( );
- tr_globalUnlock( h );
+ tr_globalUnlock( session );
return lock;
}
static void
makeMetaWorkerFunc( void * user_data )
{
- tr_handle * handle = (tr_handle *) user_data;
+ tr_session * session = user_data;
for( ; ; )
{
tr_metainfo_builder * builder = NULL;
/* find the next builder to process */
- tr_lock * lock = getQueueLock ( handle );
+ tr_lock * lock = getQueueLock( session );
tr_lockLock( lock );
if( queue )
{
uint32_t pieceSize;
uint32_t pieceCount;
int isSingleFile;
- tr_handle * handle;
+ tr_session * handle;
/**
*** These are set inside tr_makeMetaInfo()
tr_metainfo_builder;
-tr_metainfo_builder*tr_metaInfoBuilderCreate( tr_handle * handle,
+tr_metainfo_builder*tr_metaInfoBuilderCreate( tr_session * session,
const char * topFile );
void tr_metaInfoBuilderFree( tr_metainfo_builder* );
***/
static char*
-getTorrentFilename( const tr_handle * handle,
+getTorrentFilename( const tr_session * session,
const tr_info * inf )
{
return tr_strdup_printf( "%s%c%s.%16.16s.torrent",
- tr_getTorrentDir( handle ),
+ tr_getTorrentDir( session ),
TR_PATH_DELIMITER,
inf->name,
inf->hashString );
}
static char*
-getOldTorrentFilename( const tr_handle * handle,
+getOldTorrentFilename( const tr_session * session,
const tr_info * inf )
{
char * ret;
struct evbuffer * buf = evbuffer_new( );
- evbuffer_add_printf( buf, "%s%c%s", tr_getTorrentDir( handle ),
+ evbuffer_add_printf( buf, "%s%c%s", tr_getTorrentDir( session ),
TR_PATH_DELIMITER,
inf->hashString );
- if( handle->tag )
- evbuffer_add_printf( buf, "-%s", handle->tag );
+ if( session->tag )
+ evbuffer_add_printf( buf, "-%s", session->tag );
ret = tr_strndup( EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ) );
evbuffer_free( buf );
}
void
-tr_metainfoMigrate( tr_handle * handle,
+tr_metainfoMigrate( tr_session * session,
tr_info * inf )
{
struct stat new_sb;
- char * name = getTorrentFilename( handle, inf );
+ char * name = getTorrentFilename( session, inf );
if( stat( name, &new_sb ) || ( ( new_sb.st_mode & S_IFMT ) != S_IFREG ) )
{
- char * old_name = getOldTorrentFilename( handle, inf );
+ char * old_name = getOldTorrentFilename( session, inf );
size_t contentLen;
uint8_t * content;
- tr_mkdirp( tr_getTorrentDir( handle ), 0777 );
+ tr_mkdirp( tr_getTorrentDir( session ), 0777 );
if( ( content = tr_loadFile( old_name, &contentLen ) ) )
{
FILE * out;
{
tr_free( inf->torrent );
inf->torrent = tr_strdup( name );
- tr_sessionSetTorrentFile( handle, inf->hashString, name );
+ tr_sessionSetTorrentFile( session, inf->hashString, name );
unlink( old_name );
}
fclose( out );
}
static const char*
-tr_metainfoParseImpl( const tr_handle * handle,
+tr_metainfoParseImpl( const tr_session * session,
tr_info * inf,
const tr_benc * meta_in )
{
/* filename of Transmission's copy */
tr_free( inf->torrent );
- inf->torrent = getTorrentFilename( handle, inf );
+ inf->torrent = getTorrentFilename( session, inf );
return NULL;
}
int
-tr_metainfoParse( const tr_handle * handle,
+tr_metainfoParse( const tr_session * session,
tr_info * inf,
const tr_benc * meta_in )
{
- const char * badTag = tr_metainfoParseImpl( handle, inf, meta_in );
+ const char * badTag = tr_metainfoParseImpl( session, inf, meta_in );
if( badTag )
{
}
void
-tr_metainfoRemoveSaved( const tr_handle * handle,
+tr_metainfoRemoveSaved( const tr_session * session,
const tr_info * inf )
{
char * filename;
- filename = getTorrentFilename( handle, inf );
+ filename = getTorrentFilename( session, inf );
unlink( filename );
tr_free( filename );
- filename = getOldTorrentFilename( handle, inf );
+ filename = getOldTorrentFilename( session, inf );
unlink( filename );
tr_free( filename );
}
struct tr_benc;
-int tr_metainfoParse( const tr_handle * handle,
- tr_info * info,
+int tr_metainfoParse( const tr_session * session,
+ tr_info * info,
const struct tr_benc * benc );
-void tr_metainfoRemoveSaved( const tr_handle * handle,
- const tr_info * info );
+void tr_metainfoRemoveSaved( const tr_session * session,
+ const tr_info * info );
-void tr_metainfoMigrate( tr_handle * handle,
- tr_info * inf );
+void tr_metainfoMigrate( tr_session * session,
+ tr_info * inf );
#ifdef __cplusplus
}
***
**/
-tr_peerIo* tr_peerIoNewOutgoing( struct tr_handle * session,
+tr_peerIo* tr_peerIoNewOutgoing( tr_session * session,
const struct tr_address * addr,
tr_port port,
const uint8_t * torrentHash );
-tr_peerIo* tr_peerIoNewIncoming( struct tr_handle * session,
+tr_peerIo* tr_peerIoNewIncoming( tr_session * session,
const struct tr_address * addr,
tr_port port,
int socket );
#include "net.h"
-struct tr_handle;
struct tr_peer_stat;
struct tr_torrent;
typedef struct tr_peerMgr tr_peerMgr;
int tr_pexCompare( const void * a, const void * b );
-tr_peerMgr* tr_peerMgrNew( struct tr_handle * );
+tr_peerMgr* tr_peerMgrNew( tr_session * );
void tr_peerMgrFree( tr_peerMgr * manager );
}
static void
-migrateFiles( const tr_handle * handle )
+migrateFiles( const tr_session * session )
{
static int migrated = FALSE;
migrated = TRUE;
oldDir = getOldTorrentsDir( );
- newDir = tr_getTorrentDir( handle );
+ newDir = tr_getTorrentDir( session );
moveFiles( oldDir, newDir );
oldDir = getOldCacheDir( );
- newDir = tr_getResumeDir( handle );
+ newDir = tr_getResumeDir( session );
moveFiles( oldDir, newDir );
}
}
void
-tr_setConfigDir( tr_handle * handle,
+tr_setConfigDir( tr_session * session,
const char * configDir )
{
char * path;
- handle->configDir = tr_strdup( configDir );
+ session->configDir = tr_strdup( configDir );
path = tr_buildPath( configDir, RESUME_SUBDIR, NULL );
tr_mkdirp( path, 0777 );
- handle->resumeDir = path;
+ session->resumeDir = path;
path = tr_buildPath( configDir, TORRENT_SUBDIR, NULL );
tr_mkdirp( path, 0777 );
- handle->torrentDir = path;
+ session->torrentDir = path;
- migrateFiles( handle );
+ migrateFiles( session );
}
const char *
-tr_sessionGetConfigDir( const tr_handle * handle )
+tr_sessionGetConfigDir( const tr_session * session )
{
- return handle->configDir;
+ return session->configDir;
}
const char *
-tr_getTorrentDir( const tr_handle * handle )
+tr_getTorrentDir( const tr_session * session )
{
- return handle->torrentDir;
+ return session->torrentDir;
}
const char *
-tr_getResumeDir( const tr_handle * handle )
+tr_getResumeDir( const tr_session * session )
{
- return handle->resumeDir;
+ return session->resumeDir;
}
const char*
typedef struct tr_lock tr_lock;
typedef struct tr_thread tr_thread;
-struct tr_handle;
+void tr_setConfigDir( tr_session * session,
+ const char * configDir );
-void tr_setConfigDir( struct tr_handle * handle,
- const char * configDir );
+const char * tr_getResumeDir( const tr_session * );
-const char * tr_getResumeDir( const struct tr_handle * );
+const char * tr_getTorrentDir( const tr_session * );
-const char * tr_getTorrentDir( const struct tr_handle * );
-
-const char * tr_getClutchDir( const struct tr_handle * );
+const char * tr_getClutchDir( const tr_session * );
tr_thread* tr_threadNew( void ( *func )(void *),
tr_shared *
tr_sharedInit( tr_session * session,
- int isEnabled,
- int publicPort )
+ tr_bool isEnabled,
+ tr_bool publicPort )
{
tr_shared * s = tr_new0( tr_shared, 1 );
}
void
-tr_sharedSetPort( tr_shared * s,
- int port )
+tr_sharedSetPort( tr_shared * s, tr_port port )
{
tr_torrent * tor = NULL;
tr_torrentChangeMyPort( tor );
}
-int
+tr_port
tr_sharedGetPeerPort( const tr_shared * s )
{
return s->publicPort;
}
void
-tr_sharedTraversalEnable( tr_shared * s,
- int isEnabled )
+tr_sharedTraversalEnable( tr_shared * s, tr_bool isEnabled )
{
s->isEnabled = isEnabled;
}
-int
+tr_bool
tr_sharedTraversalIsEnabled( const tr_shared * s )
{
return s->isEnabled;
typedef struct tr_shared tr_shared;
-tr_shared* tr_sharedInit( tr_handle *,
- int isEnabled,
- int publicPort );
+tr_shared* tr_sharedInit( tr_session*, tr_bool isEnabled, tr_bool publicPort );
void tr_sharedShuttingDown( tr_shared * );
-void tr_sharedSetPort( tr_shared *,
- int publicPort );
+void tr_sharedSetPort( tr_shared *, tr_port publicPort );
-void tr_sharedTraversalEnable( tr_shared *,
- int isEnabled );
+void tr_sharedTraversalEnable( tr_shared *, tr_bool isEnabled );
-int tr_sharedGetPeerPort( const tr_shared * s );
+tr_port tr_sharedGetPeerPort( const tr_shared * s );
-int tr_sharedTraversalIsEnabled( const tr_shared * s );
+tr_bool tr_sharedTraversalIsEnabled( const tr_shared * s );
int tr_sharedTraversalStatus( const tr_shared * );
tr_bool isWhitelistEnabled;
tr_port port;
struct evhttp * httpd;
- tr_handle * session;
+ tr_session * session;
char * username;
char * password;
char * whitelist;
}
tr_rpc_server *
-tr_rpcInit( tr_handle * session,
- tr_bool isEnabled,
- tr_port port,
- tr_bool isWhitelistEnabled,
- const char * whitelist,
- tr_bool isPasswordEnabled,
- const char * username,
- const char * password )
+tr_rpcInit( tr_session * session,
+ tr_bool isEnabled,
+ tr_port port,
+ tr_bool isWhitelistEnabled,
+ const char * whitelist,
+ tr_bool isPasswordEnabled,
+ const char * username,
+ const char * password )
{
tr_rpc_server * s;
typedef struct tr_rpc_server tr_rpc_server;
-tr_rpc_server * tr_rpcInit( struct tr_handle * session,
- tr_bool isEnabled,
- tr_port port,
- tr_bool isWhitelistEnabled,
- const char * whitelist,
- tr_bool isPasswordEnabled,
- const char * username,
- const char * password );
+tr_rpc_server * tr_rpcInit( tr_session * session,
+ tr_bool isEnabled,
+ tr_port port,
+ tr_bool isWhitelistEnabled,
+ const char * whitelist,
+ tr_bool isPasswordEnabled,
+ const char * username,
+ const char * password );
void tr_rpcClose( tr_rpc_server ** freeme );
***/
static tr_rpc_callback_status
-notify( tr_handle * session,
+notify( tr_session * session,
int type,
tr_torrent * tor )
{
***/
static tr_torrent **
-getTorrents( tr_handle * handle,
- tr_benc * args,
- int * setmeCount )
+getTorrents( tr_session * session,
+ tr_benc * args,
+ int * setmeCount )
{
int torrentCount = 0;
int64_t id;
int64_t id;
const char * str;
if( tr_bencGetInt( node, &id ) )
- tor = tr_torrentFindFromId( handle, id );
+ tor = tr_torrentFindFromId( session, id );
else if( tr_bencGetStr( node, &str ) )
- tor = tr_torrentFindFromHashString( handle, str );
+ tor = tr_torrentFindFromHashString( session, str );
if( tor )
torrents[torrentCount++] = tor;
}
{
tr_torrent * tor;
torrents = tr_new0( tr_torrent *, 1 );
- if( ( tor = tr_torrentFindFromId( handle, id ) ) )
+ if( ( tor = tr_torrentFindFromId( session, id ) ) )
torrents[torrentCount++] = tor;
}
else /* all of them */
{
tr_torrent * tor = NULL;
- const int n = tr_sessionCountTorrents( handle );
+ const int n = tr_sessionCountTorrents( session );
torrents = tr_new0( tr_torrent *, n );
- while( ( tor = tr_torrentNext( handle, tor ) ) )
+ while( ( tor = tr_torrentNext( session, tor ) ) )
torrents[torrentCount++] = tor;
}
}
static const char*
-torrentStart( tr_handle * h,
- tr_benc * args_in,
- tr_benc * args_out UNUSED )
+torrentStart( tr_session * session,
+ tr_benc * args_in,
+ tr_benc * args_out UNUSED )
{
int i, torrentCount;
- tr_torrent ** torrents = getTorrents( h, args_in, &torrentCount );
+ tr_torrent ** torrents = getTorrents( session, args_in, &torrentCount );
for( i = 0; i < torrentCount; ++i )
{
tr_torrent * tor = torrents[i];
tr_torrentStart( tor );
- notify( h, TR_RPC_TORRENT_STARTED, tor );
+ notify( session, TR_RPC_TORRENT_STARTED, tor );
}
tr_free( torrents );
return NULL;
}
static const char*
-torrentStop( tr_handle * h,
- tr_benc * args_in,
- tr_benc * args_out UNUSED )
+torrentStop( tr_session * session,
+ tr_benc * args_in,
+ tr_benc * args_out UNUSED )
{
int i, torrentCount;
- tr_torrent ** torrents = getTorrents( h, args_in, &torrentCount );
+ tr_torrent ** torrents = getTorrents( session, args_in, &torrentCount );
for( i = 0; i < torrentCount; ++i )
{
tr_torrent * tor = torrents[i];
tr_torrentStop( tor );
- notify( h, TR_RPC_TORRENT_STOPPED, tor );
+ notify( session, TR_RPC_TORRENT_STOPPED, tor );
}
tr_free( torrents );
return NULL;
}
static const char*
-torrentRemove( tr_handle * h,
+torrentRemove( tr_session * session,
tr_benc * args_in,
tr_benc * args_out UNUSED )
{
int i;
int torrentCount;
- tr_torrent ** torrents = getTorrents( h, args_in, &torrentCount );
+ tr_torrent ** torrents = getTorrents( session, args_in, &torrentCount );
for( i=0; i<torrentCount; ++i )
{
tr_torrent * tor = torrents[i];
- const tr_rpc_callback_status status = notify( h, TR_RPC_TORRENT_REMOVING, tor );
+ const tr_rpc_callback_status status = notify( session, TR_RPC_TORRENT_REMOVING, tor );
int64_t deleteFlag;
if( tr_bencDictFindInt( args_in, "delete-local-data", &deleteFlag ) && deleteFlag )
tr_torrentDeleteLocalData( tor );
if( !( status & TR_RPC_NOREMOVE ) )
tr_torrentRemove( tor );
}
+
tr_free( torrents );
return NULL;
}
static const char*
-torrentVerify( tr_handle * h,
- tr_benc * args_in,
- tr_benc * args_out UNUSED )
+torrentVerify( tr_session * session,
+ tr_benc * args_in,
+ tr_benc * args_out UNUSED )
{
int i, torrentCount;
- tr_torrent ** torrents = getTorrents( h, args_in, &torrentCount );
+ tr_torrent ** torrents = getTorrents( session, args_in, &torrentCount );
for( i = 0; i < torrentCount; ++i )
{
tr_torrent * tor = torrents[i];
tr_torrentVerify( tor );
- notify( h, TR_RPC_TORRENT_CHANGED, tor );
+ notify( session, TR_RPC_TORRENT_CHANGED, tor );
}
+
tr_free( torrents );
return NULL;
}
}
static const char*
-torrentGet( tr_handle * handle,
- tr_benc * args_in,
- tr_benc * args_out )
+torrentGet( tr_session * session,
+ tr_benc * args_in,
+ tr_benc * args_out )
{
int i, torrentCount;
- tr_torrent ** torrents = getTorrents( handle, args_in, &torrentCount );
+ tr_torrent ** torrents = getTorrents( session, args_in, &torrentCount );
tr_benc * list = tr_bencDictAddList( args_out, "torrents",
torrentCount );
tr_benc * fields;
}
static const char*
-torrentSet( tr_handle * h,
- tr_benc * args_in,
- tr_benc * args_out UNUSED )
+torrentSet( tr_session * session,
+ tr_benc * args_in,
+ tr_benc * args_out UNUSED )
{
int i, torrentCount;
- tr_torrent ** torrents = getTorrents( h, args_in, &torrentCount );
+ tr_torrent ** torrents = getTorrents( session, args_in, &torrentCount );
for( i = 0; i < torrentCount; ++i )
{
tr_torrentSetSpeedMode( tor, TR_UP, tmp ? TR_SPEEDLIMIT_SINGLE
: TR_SPEEDLIMIT_GLOBAL );
- notify( h, TR_RPC_TORRENT_CHANGED, tor );
+ notify( session, TR_RPC_TORRENT_CHANGED, tor );
}
tr_free( torrents );
***/
static const char*
-torrentAdd( tr_handle * h,
- tr_benc * args_in,
- tr_benc * args_out )
+torrentAdd( tr_session * session,
+ tr_benc * args_in,
+ tr_benc * args_out )
{
const char * filename = NULL;
const char * metainfo_base64 = NULL;
tr_ctor * ctor;
tr_torrent * tor;
- ctor = tr_ctorNew( h );
+ ctor = tr_ctorNew( session );
/* set the metainfo */
if( filename )
if( tr_bencDictFindInt( args_in, "peer-limit", &i ) )
tr_ctorSetPeerLimit( ctor, TR_FORCE, i );
- tor = tr_torrentNew( h, ctor, &err );
+ tor = tr_torrentNew( session, ctor, &err );
tr_ctorFree( ctor );
if( tor )
tr_bencListAddStr( &fields, "hashString" );
addInfo( tor, tr_bencDictAdd( args_out,
"torrent-added" ), &fields );
- notify( h, TR_RPC_TORRENT_ADDED, tor );
+ notify( session, TR_RPC_TORRENT_ADDED, tor );
tr_bencFree( &fields );
}
else if( err == TR_EDUPLICATE )
***/
static const char*
-sessionSet( tr_handle * h,
- tr_benc * args_in,
- tr_benc * args_out UNUSED )
+sessionSet( tr_session * session,
+ tr_benc * args_in,
+ tr_benc * args_out UNUSED )
{
int64_t i;
const char * str;
if( tr_bencDictFindStr( args_in, "download-dir", &str ) )
- tr_sessionSetDownloadDir( h, str );
+ tr_sessionSetDownloadDir( session, str );
if( tr_bencDictFindInt( args_in, "peer-limit", &i ) )
- tr_sessionSetPeerLimit( h, i );
+ tr_sessionSetPeerLimit( session, i );
if( tr_bencDictFindInt( args_in, "pex-allowed", &i ) )
- tr_sessionSetPexEnabled( h, i );
+ tr_sessionSetPexEnabled( session, i );
if( tr_bencDictFindInt( args_in, "port", &i ) )
- tr_sessionSetPeerPort( h, i );
+ tr_sessionSetPeerPort( session, i );
if( tr_bencDictFindInt( args_in, "port-forwarding-enabled", &i ) )
- tr_sessionSetPortForwardingEnabled( h, i );
+ tr_sessionSetPortForwardingEnabled( session, i );
if( tr_bencDictFindInt( args_in, "speed-limit-down", &i ) )
- tr_sessionSetSpeedLimit( h, TR_DOWN, i );
+ tr_sessionSetSpeedLimit( session, TR_DOWN, i );
if( tr_bencDictFindInt( args_in, "speed-limit-down-enabled", &i ) )
- tr_sessionSetSpeedLimitEnabled( h, TR_DOWN, i );
+ tr_sessionSetSpeedLimitEnabled( session, TR_DOWN, i );
if( tr_bencDictFindInt( args_in, "speed-limit-up", &i ) )
- tr_sessionSetSpeedLimit( h, TR_UP, i );
+ tr_sessionSetSpeedLimit( session, TR_UP, i );
if( tr_bencDictFindInt( args_in, "speed-limit-up-enabled", &i ) )
- tr_sessionSetSpeedLimitEnabled( h, TR_UP, i );
+ tr_sessionSetSpeedLimitEnabled( session, TR_UP, i );
if( tr_bencDictFindStr( args_in, "encryption", &str ) )
{
if( !strcmp( str, "required" ) )
- tr_sessionSetEncryption( h, TR_ENCRYPTION_REQUIRED );
+ tr_sessionSetEncryption( session, TR_ENCRYPTION_REQUIRED );
else if( !strcmp( str, "tolerated" ) )
- tr_sessionSetEncryption( h, TR_CLEAR_PREFERRED );
+ tr_sessionSetEncryption( session, TR_CLEAR_PREFERRED );
else
- tr_sessionSetEncryption( h, TR_ENCRYPTION_PREFERRED );
+ tr_sessionSetEncryption( session, TR_ENCRYPTION_PREFERRED );
}
- notify( h, TR_RPC_SESSION_CHANGED, NULL );
+ notify( session, TR_RPC_SESSION_CHANGED, NULL );
return NULL;
}
static const char*
-sessionStats( tr_handle * h,
- tr_benc * args_in UNUSED,
- tr_benc * args_out )
+sessionStats( tr_session * session,
+ tr_benc * args_in UNUSED,
+ tr_benc * args_out )
{
- tr_benc * d = tr_bencDictAddDict( args_out, "session-stats", 10 );
- tr_torrent * tor = NULL;
- int running = 0;
- int total = 0;
-
- while( ( tor = tr_torrentNext( h, tor ) ) )
- {
+ tr_benc * d;
+ tr_torrent * tor;
+ int running;
+ int total;
+
+ tor = NULL;
+ total = running = 0;
+ while(( tor = tr_torrentNext( session, tor ))) {
++total;
if( tor->isRunning )
++running;
}
+ d = tr_bencDictAddDict( args_out, "session-stats", 5 );
tr_bencDictAddInt( d, "activeTorrentCount", running );
- tr_bencDictAddInt( d, "downloadSpeed", (int)( tr_sessionGetPieceSpeed( h, TR_DOWN ) * 1024 ) );
+ tr_bencDictAddInt( d, "downloadSpeed", (int)( tr_sessionGetPieceSpeed( session, TR_DOWN ) * 1024 ) );
tr_bencDictAddInt( d, "pausedTorrentCount", total - running );
tr_bencDictAddInt( d, "torrentCount", total );
- tr_bencDictAddInt( d, "uploadSpeed", (int)( tr_sessionGetPieceSpeed( h, TR_UP ) * 1024 ) );
+ tr_bencDictAddInt( d, "uploadSpeed", (int)( tr_sessionGetPieceSpeed( session, TR_UP ) * 1024 ) );
return NULL;
}
static const char*
-sessionGet( tr_handle * h,
- tr_benc * args_in UNUSED,
- tr_benc * args_out )
+sessionGet( tr_session * session,
+ tr_benc * args_in UNUSED,
+ tr_benc * args_out )
{
const char * str;
tr_benc * d = args_out;
- tr_bencDictAddStr( d, "download-dir",
- tr_sessionGetDownloadDir( h ) );
- tr_bencDictAddInt( d, "peer-limit",
- tr_sessionGetPeerLimit( h ) );
- tr_bencDictAddInt( d, "pex-allowed",
- tr_sessionIsPexEnabled( h ) );
- tr_bencDictAddInt( d, "port",
- tr_sessionGetPeerPort( h ) );
- tr_bencDictAddInt( d, "port-forwarding-enabled",
- tr_sessionIsPortForwardingEnabled( h ) );
- tr_bencDictAddInt( d, "speed-limit-up",
- tr_sessionGetSpeedLimit( h, TR_UP ) );
- tr_bencDictAddInt( d, "speed-limit-up-enabled",
- tr_sessionIsSpeedLimitEnabled( h, TR_UP ) );
- tr_bencDictAddInt( d, "speed-limit-down",
- tr_sessionGetSpeedLimit( h, TR_DOWN ) );
- tr_bencDictAddInt( d, "speed-limit-down-enabled",
- tr_sessionIsSpeedLimitEnabled( h, TR_DOWN ) );
+ tr_bencDictAddStr( d, "download-dir", tr_sessionGetDownloadDir( session ) );
+ tr_bencDictAddInt( d, "peer-limit", tr_sessionGetPeerLimit( session ) );
+ tr_bencDictAddInt( d, "pex-allowed", tr_sessionIsPexEnabled( session ) );
+ tr_bencDictAddInt( d, "port", tr_sessionGetPeerPort( session ) );
+ tr_bencDictAddInt( d, "port-forwarding-enabled", tr_sessionIsPortForwardingEnabled( session ) );
+ tr_bencDictAddInt( d, "speed-limit-up", tr_sessionGetSpeedLimit( session, TR_UP ) );
+ tr_bencDictAddInt( d, "speed-limit-up-enabled", tr_sessionIsSpeedLimitEnabled( session, TR_UP ) );
+ tr_bencDictAddInt( d, "speed-limit-down", tr_sessionGetSpeedLimit( session, TR_DOWN ) );
+ tr_bencDictAddInt( d, "speed-limit-down-enabled", tr_sessionIsSpeedLimitEnabled( session, TR_DOWN ) );
tr_bencDictAddStr( d, "version", LONG_VERSION_STRING );
- switch( tr_sessionGetEncryption( h ) )
- {
- case TR_CLEAR_PREFERRED:
- str = "tolerated"; break;
-
- case TR_ENCRYPTION_REQUIRED:
- str = "required"; break;
-
- default:
- str = "preferred"; break;
+ switch( tr_sessionGetEncryption( session ) ) {
+ case TR_CLEAR_PREFERRED: str = "tolerated"; break;
+ case TR_ENCRYPTION_REQUIRED: str = "required"; break;
+ default: str = "preferred"; break;
}
tr_bencDictAddStr( d, "encryption", str );
****
***/
-typedef const char* ( handler )( tr_handle*, tr_benc*, tr_benc* );
+typedef const char* ( handler )( tr_session*, tr_benc*, tr_benc* );
static struct method
{
};
static char*
-request_exec( struct tr_handle * handle,
- tr_benc * request,
- int * response_len )
+request_exec( tr_session * session,
+ tr_benc * request,
+ int * response_len )
{
int64_t i;
const char * str;
break;
result = i == n
? "method name not recognized"
- : ( *methods[i].func )( handle, args_in, args_out );
+ : ( *methods[i].func )( session, args_in, args_out );
}
/* serialize & return the response */
}
char*
-tr_rpc_request_exec_json( struct tr_handle * handle,
- const void * request_json,
- int request_len,
- int * response_len )
+tr_rpc_request_exec_json( tr_session * session,
+ const void * request_json,
+ int request_len,
+ int * response_len )
{
tr_benc top;
int have_content;
request_len = strlen( request_json );
have_content = !tr_jsonParse( request_json, request_len, &top, NULL );
- ret = request_exec( handle, have_content ? &top : NULL, response_len );
+ ret = request_exec( session, have_content ? &top : NULL, response_len );
if( have_content )
tr_bencFree( &top );
}
char*
-tr_rpc_request_exec_uri( struct tr_handle * handle,
- const void * request_uri,
- int request_len,
- int * response_len )
+tr_rpc_request_exec_uri( tr_session * session,
+ const void * request_uri,
+ int request_len,
+ int * response_len )
{
char * ret = NULL;
tr_benc top, * args;
pch = next ? next + 1 : NULL;
}
- ret = request_exec( handle, &top, response_len );
+ ret = request_exec( session, &top, response_len );
/* cleanup */
tr_bencFree( &top );
***/
struct tr_benc;
-struct tr_handle;
/* http://www.json.org/ */
-char*tr_rpc_request_exec_json( struct tr_handle * handle,
- const void * request_json,
- int request_len,
- int * response_len );
+char*tr_rpc_request_exec_json( tr_session * session,
+ const void * request_json,
+ int request_len,
+ int * response_len );
/* see the RPC spec's "Request URI Notation" section */
-char*tr_rpc_request_exec_uri( struct tr_handle * handle,
- const void * request_uri,
- int request_len,
- int * response_len );
+char*tr_rpc_request_exec_uri( tr_session * session,
+ const void * request_uri,
+ int request_len,
+ int * response_len );
void tr_rpc_parse_list_str( struct tr_benc * setme,
- const char * list_str,
+ const char * list_str,
size_t list_str_len );
tr_free( filename );
}
-static void metainfoLookupRescan( tr_handle * h );
+static void metainfoLookupRescan( tr_session * );
tr_session *
tr_sessionInit( const char * tag,
***/
void
-tr_sessionSetDownloadDir( tr_handle * handle,
- const char * dir )
+tr_sessionSetDownloadDir( tr_session * session, const char * dir )
{
- if( handle->downloadDir != dir )
+ if( session->downloadDir != dir )
{
- tr_free( handle->downloadDir );
- handle->downloadDir = tr_strdup( dir );
+ tr_free( session->downloadDir );
+ session->downloadDir = tr_strdup( dir );
}
}
const char *
-tr_sessionGetDownloadDir( const tr_handle * handle )
+tr_sessionGetDownloadDir( const tr_session * session )
{
- return handle->downloadDir;
+ return session->downloadDir;
}
/***
***/
void
-tr_globalLock( struct tr_handle * handle )
+tr_globalLock( tr_session * session )
{
- tr_lockLock( handle->lock );
+ tr_lockLock( session->lock );
}
void
-tr_globalUnlock( struct tr_handle * handle )
+tr_globalUnlock( tr_session * session )
{
- tr_lockUnlock( handle->lock );
+ tr_lockUnlock( session->lock );
}
tr_bool
-tr_globalIsLocked( const struct tr_handle * handle )
+tr_globalIsLocked( const tr_session * session )
{
- return handle && tr_lockHave( handle->lock );
+ return session && tr_lockHave( session->lock );
}
/***********************************************************************
}
tr_port_forwarding
-tr_sessionGetPortForwarding( const tr_handle * h )
+tr_sessionGetPortForwarding( const tr_session * session )
{
- return tr_sharedTraversalStatus( h->shared );
+ return tr_sharedTraversalStatus( session->shared );
}
/***
***/
void
-tr_sessionSetPeerLimit( tr_handle * handle UNUSED,
- uint16_t maxGlobalPeers )
+tr_sessionSetPeerLimit( tr_session * session UNUSED,
+ uint16_t maxGlobalPeers )
{
tr_fdSetPeerLimit( maxGlobalPeers );
}
}
int
-tr_sessionCountTorrents( const tr_handle * h )
+tr_sessionCountTorrents( const tr_session * session )
{
- return h->torrentCount;
+ return session->torrentCount;
}
static int
-compareTorrentByCur( const void * va,
- const void * vb )
+compareTorrentByCur( const void * va, const void * vb )
{
const tr_torrent * a = *(const tr_torrent**)va;
const tr_torrent * b = *(const tr_torrent**)vb;
static void
tr_closeAllConnections( void * vsession )
{
- tr_handle * session = vsession;
+ tr_session * session = vsession;
tr_torrent * tor;
int i, n;
tr_torrent ** torrents;
} while( 0 )
void
-tr_sessionClose( tr_handle * session )
+tr_sessionClose( tr_session * session )
{
int i;
const int maxwait_msec = SHUTDOWN_MAX_SECONDS * 1000;
}
tr_torrent **
-tr_sessionLoadTorrents( tr_handle * h,
- tr_ctor * ctor,
- int * setmeCount )
+tr_sessionLoadTorrents( tr_session * session,
+ tr_ctor * ctor,
+ int * setmeCount )
{
int i, n = 0;
struct stat sb;
DIR * odir = NULL;
- const char * dirname = tr_getTorrentDir( h );
+ const char * dirname = tr_getTorrentDir( session );
tr_torrent ** torrents;
tr_list * l = NULL, *list = NULL;
tr_torrent * tor;
char * path = tr_buildPath( dirname, d->d_name, NULL );
tr_ctorSetMetainfoFromFile( ctor, path );
- if(( tor = tr_torrentNew( h, ctor, NULL )))
+ if(( tor = tr_torrentNew( session, ctor, NULL )))
{
tr_list_append( &list, tor );
++n;
}
tr_bool
-tr_sessionIsPortForwardingEnabled( const tr_handle * h )
+tr_sessionIsPortForwardingEnabled( const tr_session * session )
{
- return tr_sharedTraversalIsEnabled( h->shared );
+ return tr_sharedTraversalIsEnabled( session->shared );
}
/***
}
static void
-metainfoLookupResort( tr_handle * h )
+metainfoLookupResort( tr_session * session )
{
- qsort( h->metainfoLookup,
- h->metainfoLookupCount,
+ qsort( session->metainfoLookup,
+ session->metainfoLookupCount,
sizeof( struct tr_metainfo_lookup ),
compareLookupEntries );
}
}
const char*
-tr_sessionFindTorrentFile( const tr_handle * h,
- const char * hashStr )
+tr_sessionFindTorrentFile( const tr_session * session,
+ const char * hashStr )
{
struct tr_metainfo_lookup * l = bsearch( hashStr,
- h->metainfoLookup,
- h->metainfoLookupCount,
- sizeof( struct
- tr_metainfo_lookup ),
+ session->metainfoLookup,
+ session->metainfoLookupCount,
+ sizeof( struct tr_metainfo_lookup ),
compareHashStringToLookupEntry );
return l ? l->filename : NULL;
}
static void
-metainfoLookupRescan( tr_handle * h )
+metainfoLookupRescan( tr_session * session )
{
int i;
int n;
struct stat sb;
- const char * dirname = tr_getTorrentDir( h );
+ const char * dirname = tr_getTorrentDir( session );
DIR * odir = NULL;
tr_ctor * ctor = NULL;
tr_list * list = NULL;
/* walk through the directory and find the mappings */
- ctor = tr_ctorNew( h );
+ ctor = tr_ctorNew( session );
tr_ctorSetSave( ctor, FALSE ); /* since we already have them */
- if( !stat( dirname,
- &sb ) && S_ISDIR( sb.st_mode )
- && ( ( odir = opendir( dirname ) ) ) )
+ if( !stat( dirname, &sb ) && S_ISDIR( sb.st_mode ) && ( ( odir = opendir( dirname ) ) ) )
{
struct dirent *d;
for( d = readdir( odir ); d != NULL; d = readdir( odir ) )
tr_info inf;
char * path = tr_buildPath( dirname, d->d_name, NULL );
tr_ctorSetMetainfoFromFile( ctor, path );
- if( !tr_torrentParse( h, ctor, &inf ) )
+ if( !tr_torrentParse( session, ctor, &inf ) )
{
tr_list_append( &list, tr_strdup( inf.hashString ) );
tr_list_append( &list, tr_strdup( path ) );
tr_ctorFree( ctor );
n = tr_list_size( list ) / 2;
- h->metainfoLookup = tr_new0( struct tr_metainfo_lookup, n );
- h->metainfoLookupCount = n;
+ session->metainfoLookup = tr_new0( struct tr_metainfo_lookup, n );
+ session->metainfoLookupCount = n;
for( i = 0; i < n; ++i )
{
char * hashString = tr_list_pop_front( &list );
char * filename = tr_list_pop_front( &list );
- memcpy( h->metainfoLookup[i].hashString, hashString,
+ memcpy( session->metainfoLookup[i].hashString, hashString,
2 * SHA_DIGEST_LENGTH + 1 );
tr_free( hashString );
- h->metainfoLookup[i].filename = filename;
+ session->metainfoLookup[i].filename = filename;
}
- metainfoLookupResort( h );
+ metainfoLookupResort( session );
tr_dbg( "Found %d torrents in \"%s\"", n, dirname );
}
void
-tr_sessionSetTorrentFile( tr_handle * h,
+tr_sessionSetTorrentFile( tr_session * session,
const char * hashString,
const char * filename )
{
struct tr_metainfo_lookup * l = bsearch( hashString,
- h->metainfoLookup,
- h->metainfoLookupCount,
- sizeof( struct
- tr_metainfo_lookup ),
+ session->metainfoLookup,
+ session->metainfoLookupCount,
+ sizeof( struct tr_metainfo_lookup ),
compareHashStringToLookupEntry );
if( l )
}
else
{
- const int n = h->metainfoLookupCount++;
+ const int n = session->metainfoLookupCount++;
struct tr_metainfo_lookup * node;
- h->metainfoLookup = tr_renew( struct tr_metainfo_lookup,
- h->metainfoLookup,
- h->metainfoLookupCount );
- node = h->metainfoLookup + n;
+ session->metainfoLookup = tr_renew( struct tr_metainfo_lookup,
+ session->metainfoLookup,
+ session->metainfoLookupCount );
+ node = session->metainfoLookup + n;
memcpy( node->hashString, hashString, 2 * SHA_DIGEST_LENGTH + 1 );
node->filename = tr_strdup( filename );
- metainfoLookupResort( h );
+ metainfoLookupResort( session );
}
}
tr_torrent*
-tr_torrentNext( tr_handle * session,
+tr_torrentNext( tr_session * session,
tr_torrent * tor )
{
return tor ? tor->next : session->torrentList;
};
static char*
-getOldFilename( const tr_handle * handle )
+getOldFilename( const tr_session * session )
{
- return tr_buildPath( tr_sessionGetConfigDir( handle ), "stats.benc", NULL );
+ return tr_buildPath( tr_sessionGetConfigDir( session ), "stats.benc", NULL );
}
static char*
-getFilename( const tr_handle * handle )
+getFilename( const tr_session * session )
{
- return tr_buildPath( tr_sessionGetConfigDir( handle ), "stats.json", NULL );
+ return tr_buildPath( tr_sessionGetConfigDir( session ), "stats.json", NULL );
}
static void
-loadCumulativeStats( const tr_handle * handle,
+loadCumulativeStats( const tr_session * session,
tr_session_stats * setme )
{
int loaded = FALSE;
char * filename;
tr_benc top;
- filename = getFilename( handle );
+ filename = getFilename( session );
loaded = !tr_bencLoadJSONFile( filename, &top );
tr_free( filename );
if( !loaded )
{
- filename = getOldFilename( handle );
+ filename = getOldFilename( session );
loaded = !tr_bencLoadFile( filename, &top );
tr_free( filename );
}
}
static void
-saveCumulativeStats( const tr_handle * handle,
+saveCumulativeStats( const tr_session * session,
const tr_session_stats * s )
{
char * filename;
tr_bencDictAddInt( &top, "session-count", s->sessionCount );
tr_bencDictAddInt( &top, "uploaded-bytes", s->uploadedBytes );
- filename = getFilename( handle );
+ filename = getFilename( session );
tr_deepLog( __FILE__, __LINE__, NULL, "Saving stats to \"%s\"", filename );
tr_bencSaveJSONFile( filename, &top );
***/
void
-tr_statsInit( tr_handle * handle )
+tr_statsInit( tr_session * session )
{
struct tr_stats_handle * stats = tr_new0( struct tr_stats_handle, 1 );
- loadCumulativeStats( handle, &stats->old );
+ loadCumulativeStats( session, &stats->old );
stats->single.sessionCount = 1;
stats->startTime = time( NULL );
- handle->sessionStats = stats;
+ session->sessionStats = stats;
}
void
-tr_statsClose( tr_handle * handle )
+tr_statsClose( tr_session * session )
{
tr_session_stats cumulative = STATS_INIT;
- tr_sessionGetCumulativeStats( handle, &cumulative );
- saveCumulativeStats( handle, &cumulative );
+ tr_sessionGetCumulativeStats( session, &cumulative );
+ saveCumulativeStats( session, &cumulative );
- tr_free( handle->sessionStats );
- handle->sessionStats = NULL;
+ tr_free( session->sessionStats );
+ session->sessionStats = NULL;
}
static struct tr_stats_handle *
-getStats( const tr_handle * handle )
+getStats( const tr_session * session )
{
- return handle ? handle->sessionStats : NULL;
+ return session ? session->sessionStats : NULL;
}
/***
}
void
-tr_sessionGetStats( const tr_handle * handle,
+tr_sessionGetStats( const tr_session * session,
tr_session_stats * setme )
{
- const struct tr_stats_handle * stats = getStats( handle );
+ const struct tr_stats_handle * stats = getStats( session );
if( stats )
{
*setme = stats->single;
}
void
-tr_sessionGetCumulativeStats( const tr_handle * handle,
+tr_sessionGetCumulativeStats( const tr_session * session,
tr_session_stats * setme )
{
- const struct tr_stats_handle * stats = getStats( handle );
+ const struct tr_stats_handle * stats = getStats( session );
tr_session_stats current = STATS_INIT;
if( stats )
{
- tr_sessionGetStats( handle, ¤t );
+ tr_sessionGetStats( session, ¤t );
addStats( setme, &stats->old, ¤t );
}
}
void
-tr_sessionClearStats( tr_handle * handle )
+tr_sessionClearStats( tr_session * session )
{
tr_session_stats zero;
zero.filesAdded = 0;
zero.sessionCount = 0;
zero.secondsActive = 0;
- handle->sessionStats->single = handle->sessionStats->old = zero;
- handle->sessionStats->startTime = time( NULL );
+ session->sessionStats->single = session->sessionStats->old = zero;
+ session->sessionStats->startTime = time( NULL );
}
/**
**/
void
-tr_statsAddUploaded( tr_handle * handle,
+tr_statsAddUploaded( tr_session * session,
uint32_t bytes )
{
struct tr_stats_handle * s;
- if( ( s = getStats( handle ) ) )
+ if( ( s = getStats( session ) ) )
s->single.uploadedBytes += bytes;
}
void
-tr_statsAddDownloaded( tr_handle * handle,
- uint32_t bytes )
+tr_statsAddDownloaded( tr_session * session,
+ uint32_t bytes )
{
struct tr_stats_handle * s;
- if( ( s = getStats( handle ) ) )
+ if( ( s = getStats( session ) ) )
s->single.downloadedBytes += bytes;
}
void
-tr_statsFileCreated( tr_handle * handle )
+tr_statsFileCreated( tr_session * session )
{
struct tr_stats_handle * s;
- if( ( s = getStats( handle ) ) )
+ if( ( s = getStats( session ) ) )
s->single.filesAdded++;
}
}
tr_torrent*
-tr_torrentFindFromId( tr_handle * handle,
- int id )
+tr_torrentFindFromId( tr_session * session, int id )
{
tr_torrent * tor = NULL;
- while( ( tor = tr_torrentNext( handle, tor ) ) )
+ while( ( tor = tr_torrentNext( session, tor ) ) )
if( tor->uniqueId == id )
return tor;
}
tr_torrent*
-tr_torrentFindFromHashString( tr_handle * handle,
- const char * str )
+tr_torrentFindFromHashString( tr_session * session, const char * str )
{
tr_torrent * tor = NULL;
- while( ( tor = tr_torrentNext( handle, tor ) ) )
+ while( ( tor = tr_torrentNext( session, tor ) ) )
if( !strcmp( str, tor->info.hashString ) )
return tor;
}
tr_bool
-tr_torrentExists( const tr_handle * handle,
- const uint8_t * torrentHash )
+tr_torrentExists( const tr_session * session, const uint8_t * torrentHash )
{
- return tr_torrentFindFromHash( (tr_handle*)handle,
- torrentHash ) != NULL;
+ return tr_torrentFindFromHash( (tr_session*)session, torrentHash ) != NULL;
}
tr_torrent*
-tr_torrentFindFromHash( tr_handle * handle,
- const uint8_t * torrentHash )
+tr_torrentFindFromHash( tr_session * session, const uint8_t * torrentHash )
{
tr_torrent * tor = NULL;
- while( ( tor = tr_torrentNext( handle, tor ) ) )
+ while( ( tor = tr_torrentNext( session, tor ) ) )
if( *tor->info.hash == *torrentHash )
if( !memcmp( tor->info.hash, torrentHash, SHA_DIGEST_LENGTH ) )
return tor;
}
tr_torrent*
-tr_torrentFindFromObfuscatedHash( tr_handle * handle,
+tr_torrentFindFromObfuscatedHash( tr_session * session,
const uint8_t * obfuscatedTorrentHash )
{
tr_torrent * tor = NULL;
- while( ( tor = tr_torrentNext( handle, tor ) ) )
+ while( ( tor = tr_torrentNext( session, tor ) ) )
if( !memcmp( tor->obfuscatedHash, obfuscatedTorrentHash,
SHA_DIGEST_LENGTH ) )
return tor;
}
static void
-torrentRealInit( tr_handle * h,
- tr_torrent * tor,
- const tr_ctor * ctor )
+torrentRealInit( tr_session * session,
+ tr_torrent * tor,
+ const tr_ctor * ctor )
{
int doStart;
uint64_t loaded;
tr_info * info = &tor->info;
static int nextUniqueId = 1;
- tr_globalLock( h );
+ tr_globalLock( session );
- tor->session = h;
+ tor->session = session;
tor->uniqueId = nextUniqueId++;
randomizeTiers( info );
- tor->bandwidth = tr_bandwidthNew( h, h->bandwidth );
+ tor->bandwidth = tr_bandwidthNew( session, session->bandwidth );
tor->blockSize = getBlockSize( info->pieceSize );
info->hash, SHA_DIGEST_LENGTH,
NULL );
- tr_peerMgrAddTorrent( h->peerMgr, tor );
-
- assert( h->isPortSet );
+ tr_peerMgrAddTorrent( session->peerMgr, tor );
+ assert( session->isPortSet );
assert( !tor->downloadedCur );
assert( !tor->uploadedCur );
{
tr_torrent * it = NULL;
tr_torrent * last = NULL;
- while( ( it = tr_torrentNext( h, it ) ) )
+ while( ( it = tr_torrentNext( session, it ) ) )
last = it;
if( !last )
- h->torrentList = tor;
+ session->torrentList = tor;
else
last->next = tor;
- ++h->torrentCount;
+ ++session->torrentCount;
}
- tr_globalUnlock( h );
+ tr_globalUnlock( session );
/* maybe save our own copy of the metainfo */
if( tr_ctorGetSave( ctor ) )
}
}
- tr_metainfoMigrate( h, &tor->info );
+ tr_metainfoMigrate( session, &tor->info );
if( doStart )
torrentStart( tor, FALSE );
}
int
-tr_torrentParse( const tr_handle * handle,
- const tr_ctor * ctor,
- tr_info * setmeInfo )
+tr_torrentParse( const tr_session * session,
+ const tr_ctor * ctor,
+ tr_info * setmeInfo )
{
int err = 0;
int doFree;
if( !err && tr_ctorGetMetainfo( ctor, &metainfo ) )
return TR_EINVALID;
- err = tr_metainfoParse( handle, setmeInfo, metainfo );
+ err = tr_metainfoParse( session, setmeInfo, metainfo );
doFree = !err && ( setmeInfo == &tmp );
if( !err && !getBlockSize( setmeInfo->pieceSize ) )
err = TR_EINVALID;
- if( !err && tr_torrentExists( handle, setmeInfo->hash ) )
+ if( !err && tr_torrentExists( session, setmeInfo->hash ) )
err = TR_EDUPLICATE;
if( doFree )
}
tr_torrent *
-tr_torrentNew( tr_handle * handle,
- const tr_ctor * ctor,
- int * setmeError )
+tr_torrentNew( tr_session * session,
+ const tr_ctor * ctor,
+ int * setmeError )
{
int err;
tr_info tmpInfo;
tr_torrent * tor = NULL;
- err = tr_torrentParse( handle, ctor, &tmpInfo );
+ err = tr_torrentParse( session, ctor, &tmpInfo );
if( !err )
{
tor = tr_new0( tr_torrent, 1 );
tor->info = tmpInfo;
- torrentRealInit( handle, tor, ctor );
+ torrentRealInit( session, tor, ctor );
}
else if( setmeError )
{
freeTorrent( tr_torrent * tor )
{
tr_torrent * t;
- tr_handle * h = tor->session;
+ tr_session * session = tor->session;
tr_info * inf = &tor->info;
assert( tor );
assert( !tor->isRunning );
- tr_globalLock( h );
+ tr_globalLock( session );
- tr_peerMgrRemoveTorrent( h->peerMgr, tor->info.hash );
+ tr_peerMgrRemoveTorrent( session->peerMgr, tor->info.hash );
tr_cpClose( tor->completion );
tr_free( tor->downloadDir );
tr_free( tor->peer_id );
- if( tor == h->torrentList )
- h->torrentList = tor->next;
- else for( t = h->torrentList; t != NULL; t = t->next )
- {
- if( t->next == tor )
- {
- t->next = tor->next;
- break;
- }
+ if( tor == session->torrentList )
+ session->torrentList = tor->next;
+ else for( t = session->torrentList; t != NULL; t = t->next ) {
+ if( t->next == tor ) {
+ t->next = tor->next;
+ break;
}
+ }
- assert( h->torrentCount >= 1 );
- h->torrentCount--;
+ assert( session->torrentCount >= 1 );
+ session->torrentCount--;
tr_bandwidthFree( tor->bandwidth );
tr_metainfoFree( inf );
tr_free( tor );
- tr_globalUnlock( h );
+ tr_globalUnlock( session );
}
/**
{
if( tor )
{
- tr_handle * handle = tor->session;
- tr_globalLock( handle );
+ tr_session * session = tor->session;
+ tr_globalLock( session );
tr_torrentClearCompletenessCallback( tor );
- tr_runInEventThread( handle, closeTorrent, tor );
+ tr_runInEventThread( session, closeTorrent, tor );
- tr_globalUnlock( handle );
+ tr_globalUnlock( session );
}
}
void tr_trackerFree( tr_tracker * );
-void tr_trackerSessionClose( tr_handle * );
+void tr_trackerSessionClose( tr_session * );
/**
*** Tracker Publish / Subscribe
uint8_t die;
int fds[2];
tr_lock * lock;
- tr_handle * h;
+ tr_session * session;
tr_thread * thread;
struct event_base * base;
struct event pipeEvent;
}
static void
-logFunc( int severity,
- const char * message )
+logFunc( int severity, const char * message )
{
if( severity >= _EVENT_LOG_ERR )
tr_err( "%s", message );
signal( SIGPIPE, SIG_IGN );
#endif
- eh->h->events = eh;
+ eh->session->events = eh;
/* listen to the pipe's read fd */
event_set( &eh->pipeEvent, eh->fds[0], EV_READ | EV_PERSIST,
tr_lockFree( eh->lock );
event_base_free( eh->base );
- eh->h->events = NULL;
+ eh->session->events = NULL;
tr_free( eh );
tr_dbg( "Closing libevent thread" );
}
void
-tr_eventInit( tr_handle * handle )
+tr_eventInit( tr_session * session )
{
tr_event_handle * eh;
eh = tr_new0( tr_event_handle, 1 );
eh->lock = tr_lockNew( );
pipe( eh->fds );
- eh->h = handle;
+ eh->session = session;
eh->base = event_init( );
eh->thread = tr_threadNew( libeventThreadFunc, eh );
}
void
-tr_eventClose( tr_handle * handle )
+tr_eventClose( tr_session * session )
{
- handle->events->die = TRUE;
+ session->events->die = TRUE;
tr_deepLog( __FILE__, __LINE__, NULL, "closing trevent pipe" );
- EVUTIL_CLOSESOCKET( handle->events->fds[1] );
+ EVUTIL_CLOSESOCKET( session->events->fds[1] );
}
/**
**/
int
-tr_amInEventThread( struct tr_handle * handle )
+tr_amInEventThread( tr_session * session )
{
- assert( handle );
- assert( handle->events );
+ assert( session );
+ assert( session->events );
- return tr_amInThread( handle->events->thread );
+ return tr_amInThread( session->events->thread );
}
/**
**/
static void
-timerCallback( int fd UNUSED,
- short event UNUSED,
- void * vtimer )
+timerCallback( int fd UNUSED,
+ short event UNUSED,
+ void * vtimer )
{
int more;
struct tr_timer * timer = vtimer;
/* destroy the timer directly or via the command queue */
if( timer && !timer->inCallback )
{
- assert( tr_amInEventThread( timer->eh->h ) );
+ assert( tr_amInEventThread( timer->eh->session ) );
event_del( &timer->event );
tr_free( timer );
}
}
tr_timer*
-tr_timerNew( struct tr_handle * handle,
- timer_func func,
- void * user_data,
- uint64_t interval_milliseconds )
+tr_timerNew( tr_session * session,
+ timer_func func,
+ void * user_data,
+ uint64_t interval_milliseconds )
{
tr_timer * timer;
- assert( handle );
- assert( handle->events );
+ assert( session );
+ assert( session->events );
timer = tr_new0( tr_timer, 1 );
tr_timevalMsec( interval_milliseconds, &timer->tv );
timer->func = func;
timer->user_data = user_data;
- timer->eh = handle->events;
+ timer->eh = session->events;
evtimer_set( &timer->event, timerCallback, timer );
- if( tr_amInThread( handle->events->thread ) )
+ if( tr_amInThread( session->events->thread ) )
{
evtimer_add( &timer->event, &timer->tv );
}
else
{
const char ch = 't';
- int fd = handle->events->fds[1];
- tr_lock * lock = handle->events->lock;
+ int fd = session->events->fds[1];
+ tr_lock * lock = session->events->lock;
tr_lockLock( lock );
pipewrite( fd, &ch, 1 );
}
void
-tr_runInEventThread( struct tr_handle * handle,
- void func( void* ),
- void * user_data )
+tr_runInEventThread( tr_session * session,
+ void func( void* ), void * user_data )
{
- assert( handle );
- assert( handle->events );
+ assert( session );
+ assert( session->events );
- if( tr_amInThread( handle->events->thread ) )
+ if( tr_amInThread( session->events->thread ) )
{
(func)( user_data );
}
else
{
const char ch = 'r';
- int fd = handle->events->fds[1];
- tr_lock * lock = handle->events->lock;
+ int fd = session->events->fds[1];
+ tr_lock * lock = session->events->lock;
struct tr_run_data data;
tr_lockLock( lock );
/**
**/
-void tr_eventInit( struct tr_handle * tr_handle );
+void tr_eventInit( tr_session * );
-void tr_eventClose( struct tr_handle * tr_handle );
+void tr_eventClose( tr_session * );
-struct event_base * tr_eventGetBase( struct tr_handle * tr_handle );
+struct event_base * tr_eventGetBase( tr_session * );
typedef struct tr_timer tr_timer;
* The timer is freed if timer_func returns zero.
* Otherwise, it's called again after the same interval.
*/
-tr_timer* tr_timerNew(
- struct tr_handle * handle,
- int func( void * user_data ),
- void * user_data,
- uint64_t
- timeout_milliseconds );
+tr_timer* tr_timerNew( tr_session * handle,
+ int func( void * user_data ),
+ void * user_data,
+ uint64_t timeout_milliseconds );
/**
* Frees a timer and sets the timer pointer to NULL.
void tr_timerFree( tr_timer ** timer );
-int tr_amInEventThread( struct tr_handle * handle );
+int tr_amInEventThread( tr_session * );
-void tr_runInEventThread( struct tr_handle * handle,
- void func( void* ),
- void * user_data );
+void tr_runInEventThread( tr_session * session,
+ void func( void* ),
+ void * user_data );
#endif
addTask( void * vtask )
{
struct tr_web_task * task = vtask;
- const tr_handle * session = task->session;
+ const tr_session * session = task->session;
if( session && session->web )
{
#ifndef TR_HTTP_H
#define TR_HTTP_H
-struct tr_handle;
typedef struct tr_web tr_web;
-tr_web* tr_webInit( struct tr_handle * session );
+tr_web* tr_webInit( tr_session * session );
void tr_webClose( tr_web ** );
-typedef void ( tr_web_done_func )( struct tr_handle * session,
+typedef void ( tr_web_done_func )( tr_session * session,
long response_code,
- const void * response,
+ const void * response,
size_t response_byte_count,
- void * user_data );
+ void * user_data );
const char * tr_webGetResponseStr( long response_code );
-void tr_webRun( struct tr_handle * session,
- const char * url,
- const char * range,
- tr_web_done_func done_func,
- void * done_func_user_data );
+void tr_webRun( tr_session * session,
+ const char * url,
+ const char * range,
+ tr_web_done_func done_func,
+ void * done_func_user_data );
#endif
static void requestNextChunk( tr_webseed * w );
static void
-webResponseFunc( tr_handle * session,
+webResponseFunc( tr_session * session,
long response_code,
const void * response,
size_t response_byte_count,