]> granicus.if.org Git - transmission/commitdiff
(trunk libT) when reading piece data in from a socket, avoid two unnecessary calls...
authorJordan Lee <jordan@transmissionbt.com>
Mon, 4 Apr 2011 04:45:41 +0000 (04:45 +0000)
committerJordan Lee <jordan@transmissionbt.com>
Mon, 4 Apr 2011 04:45:41 +0000 (04:45 +0000)
libtransmission/peer-io.c
libtransmission/peer-io.h
libtransmission/peer-mgr.c
libtransmission/peer-msgs.c
libtransmission/session.c
libtransmission/session.h

index c3afd378f7f2ec5ae25709c3690ed0e2b6490c8a..63001880487967cb50084ff112f9d037527ccbb1 100644 (file)
@@ -1060,6 +1060,29 @@ evbuffer_add_uint64( struct evbuffer * outbuf, uint64_t addme_hll )
 ****
 ***/
 
+void
+tr_peerIoReadBytesToBuf( tr_peerIo * io, struct evbuffer * inbuf, struct evbuffer * outbuf, size_t byteCount )
+{
+    const size_t old_length = evbuffer_get_length( outbuf );
+
+    assert( tr_isPeerIo( io ) );
+    assert( evbuffer_get_length( inbuf )  >= byteCount );
+
+    /* append it to outbuf */
+    evbuffer_remove_buffer( inbuf, outbuf, byteCount );
+
+    /* decrypt if needed */
+    if( io->encryption_type == PEER_ENCRYPTION_RC4 ) {
+        struct evbuffer_ptr pos;
+        struct evbuffer_iovec iovec;
+        evbuffer_ptr_set( outbuf, &pos, old_length, EVBUFFER_PTR_SET );
+        do {
+           evbuffer_peek( outbuf, byteCount, &pos, &iovec, 1 );
+            tr_cryptoDecrypt( io->crypto, iovec.iov_len, iovec.iov_base, iovec.iov_base );
+        } while( !evbuffer_ptr_set( outbuf, &pos, iovec.iov_len, EVBUFFER_PTR_ADD ) );
+    }
+}
+
 void
 tr_peerIoReadBytes( tr_peerIo * io, struct evbuffer * inbuf, void * bytes, size_t byteCount )
 {
@@ -1106,8 +1129,8 @@ tr_peerIoDrain( tr_peerIo       * io,
                 struct evbuffer * inbuf,
                 size_t            byteCount )
 {
-    void * buf = tr_sessionGetBuffer( io->session );
-    const size_t buflen = SESSION_BUFFER_SIZE;
+    char buf[4096];
+    const size_t buflen = sizeof( buf );
 
     while( byteCount > 0 )
     {
@@ -1115,8 +1138,6 @@ tr_peerIoDrain( tr_peerIo       * io,
         tr_peerIoReadBytes( io, inbuf, buf, thisPass );
         byteCount -= thisPass;
     }
-
-    tr_sessionReleaseBuffer( io->session );
 }
 
 /***
index eff3ac2de6a7a7df4f6c0baed844e6ea44606976..69299a94f11683a07e766fb2017096c9693d38c1 100644 (file)
@@ -316,6 +316,11 @@ evbuffer_add_hton_64( struct evbuffer * buf, uint64_t val )
    evbuffer_add_uint64( buf, val );
 }
 
+void tr_peerIoReadBytesToBuf( tr_peerIo       * io,
+                              struct evbuffer * inbuf,
+                              struct evbuffer * outbuf,
+                              size_t            byteCount );
+
 void tr_peerIoReadBytes( tr_peerIo        * io,
                          struct evbuffer  * inbuf,
                          void             * bytes,
index 14f2faf2e9fabe7f7ba207f26e7646c119dfd0de..ae7843f86a9462936b70aac90c31159242e18114 100644 (file)
@@ -3157,7 +3157,7 @@ rechokePulse( int foo UNUSED, short bar UNUSED, void * vmgr )
     while(( tor = tr_torrentNext( mgr->session, tor ))) {
         if( tor->isRunning ) {
             Torrent * t = tor->torrentPeers;
-            if( tr_ptrArraySize( &t->peers ) == 0 )
+            if( tr_ptrArrayEmpty( &t->peers ) )
                 continue;
             rechokeUploads( t, now );
             if( !tr_torrentIsSeed( tor ) )
index ad776acf3d3c882817f64c5ca50ab81f23669827..f780d352c9d8c5ed29cbd23f219f77621867962a 100644 (file)
@@ -1308,20 +1308,8 @@ readBtPiece( tr_peermsgs      * msgs,
         /* read in another chunk of data */
         const size_t nLeft = req->length - evbuffer_get_length( msgs->incoming.block );
         size_t n = MIN( nLeft, inlen );
-        size_t i = n;
-        void * buf = tr_sessionGetBuffer( getSession( msgs ) );
-        const size_t buflen = SESSION_BUFFER_SIZE;
 
-        while( i > 0 )
-        {
-            const size_t thisPass = MIN( i, buflen );
-            tr_peerIoReadBytes( msgs->peer->io, inbuf, buf, thisPass );
-            evbuffer_add( msgs->incoming.block, buf, thisPass );
-            i -= thisPass;
-        }
-
-        tr_sessionReleaseBuffer( getSession( msgs ) );
-        buf = NULL;
+        tr_peerIoReadBytesToBuf( msgs->peer->io, inbuf, msgs->incoming.block, n );
 
         fireClientGotData( msgs, n, true );
         *setme_piece_bytes_read += n;
index ead336230a0dba150b7cb6be4da82b9fcb3c8ebe..71a6de9b06d37c8c7da97e5e5fc8b745e474824d 100644 (file)
@@ -571,7 +571,6 @@ tr_sessionInit( const char  * tag,
     session->cache = tr_cacheNew( 1024*1024*2 );
     session->tag = tr_strdup( tag );
     session->magicNumber = SESSION_MAGIC_NUMBER;
-    session->buffer = tr_valloc( SESSION_BUFFER_SIZE );
     tr_bandwidthConstruct( &session->bandwidth, session, NULL );
     tr_peerIdInit( session->peer_id );
     tr_bencInitList( &session->removedTorrents, 0 );
@@ -1009,27 +1008,6 @@ tr_sessionIsIncompleteDirEnabled( const tr_session * session )
 ****
 ***/
 
-void*
-tr_sessionGetBuffer( tr_session * session )
-{
-    assert( tr_isSession( session ) );
-    assert( !session->bufferInUse );
-    assert( tr_amInEventThread( session ) );
-
-    session->bufferInUse = true;
-    return session->buffer;
-}
-
-void
-tr_sessionReleaseBuffer( tr_session * session )
-{
-    assert( tr_isSession( session ) );
-    assert( session->bufferInUse );
-    assert( tr_amInEventThread( session ) );
-
-    session->bufferInUse = false;
-}
-
 void
 tr_sessionLock( tr_session * session )
 {
@@ -1862,7 +1840,6 @@ tr_sessionClose( tr_session * session )
         tr_free( session->metainfoLookup );
     }
     tr_free( session->torrentDoneScript );
-    tr_free( session->buffer );
     tr_free( session->tag );
     tr_free( session->configDir );
     tr_free( session->resumeDir );
index 698ee40370d49ae4a08940df91984144cc77e5e3..a6426822264e2705f7f953bd7546fa8e30afb517 100644 (file)
@@ -206,12 +206,6 @@ struct tr_session
     struct tr_bindinfo         * public_ipv4;
     struct tr_bindinfo         * public_ipv6;
 
-    /* a page-aligned buffer for use by the libtransmission thread.
-     * @see SESSION_BUFFER_SIZE */
-    void * buffer;
-
-    bool bufferInUse;
-
     tr_web_config_func          * curl_easy_config_func;
 
     uint8_t peer_id[PEER_ID_LEN+1];
@@ -261,15 +255,8 @@ int tr_sessionCountTorrents( const tr_session * session );
 enum
 {
     SESSION_MAGIC_NUMBER = 3845,
-
-    /* @see tr_session.buffer */
-    SESSION_BUFFER_SIZE = (16*1024)
 };
 
-void* tr_sessionGetBuffer( tr_session * session );
-
-void tr_sessionReleaseBuffer( tr_session * session );
-
 static inline bool tr_isSession( const tr_session * session )
 {
     return ( session != NULL ) && ( session->magicNumber == SESSION_MAGIC_NUMBER );