Changeset 8609


Ignore:
Timestamp:
Jun 4, 2009, 2:59:17 PM (13 years ago)
Author:
charles
Message:

(trunk libT) under very rare circumstances we wouldn't send a REJ message even if the protocol required it. also, avoid an unnecessary memcpy(). Thanks to geirha and Lacrocivious for helping test & confirm the fix.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/peer-msgs.c

    r8593 r8609  
    17221722            && tr_cpPieceIsComplete( &msgs->torrent->completion, req.index ) )
    17231723        {
    1724             /* FIXME(libevent2): we can eliminate "buf" and an extra memcpy if we create an evbuffer here, add the message header, then use evbuffer_reserve_space() + tr_ioRead() + evbuffer_commit_space() */
     1724            /* FIXME(libevent2) use evbuffer_reserve_space() + evbuffer_commit_space() */
    17251725            int err;
    1726             static uint8_t buf[MAX_BLOCK_SIZE];
    1727 
    1728             /* send a block */
    1729             if(( err = tr_ioRead( msgs->torrent, req.index, req.offset, req.length, buf ))) {
     1726            const uint32_t msglen = 4 + 1 + 4 + 4 + req.length;
     1727            struct evbuffer * out;
     1728            tr_peerIo * io = msgs->peer->io;
     1729
     1730            out = evbuffer_new( );
     1731            evbuffer_expand( out, msglen );
     1732
     1733            tr_peerIoWriteUint32( io, out, sizeof( uint8_t ) + 2 * sizeof( uint32_t ) + req.length );
     1734            tr_peerIoWriteUint8 ( io, out, BT_PIECE );
     1735            tr_peerIoWriteUint32( io, out, req.index );
     1736            tr_peerIoWriteUint32( io, out, req.offset );
     1737
     1738            err = tr_ioRead( msgs->torrent, req.index, req.offset, req.length, EVBUFFER_DATA(out)+EVBUFFER_LENGTH(out) );
     1739            if( err ) /* peer needs a reject message */
     1740            {
     1741                protocolSendReject( msgs, &req );
     1742            }
     1743            else
     1744            {
     1745                EVBUFFER_LENGTH(out) += req.length;
     1746                assert( EVBUFFER_LENGTH( out ) == msglen );
     1747                tr_peerIoWriteBuf( io, out, TRUE );
     1748                bytesWritten += EVBUFFER_LENGTH( out );
     1749                msgs->clientSentAnythingAt = now;
     1750            }
     1751
     1752            evbuffer_free( out );
     1753
     1754            if( err )
     1755            {
    17301756                fireError( msgs, err );
    17311757                bytesWritten = 0;
    17321758                msgs = NULL;
    1733             } else {
    1734                 tr_peerIo * io = msgs->peer->io;
    1735                 struct evbuffer * out = tr_getBuffer( );
    1736                 dbgmsg( msgs, "sending block %u:%u->%u", req.index, req.offset, req.length );
    1737                 tr_peerIoWriteUint32( io, out, sizeof( uint8_t ) + 2 * sizeof( uint32_t ) + req.length );
    1738                 tr_peerIoWriteUint8 ( io, out, BT_PIECE );
    1739                 tr_peerIoWriteUint32( io, out, req.index );
    1740                 tr_peerIoWriteUint32( io, out, req.offset );
    1741                 tr_peerIoWriteBytes ( io, out, buf, req.length );
    1742                 tr_peerIoWriteBuf( io, out, TRUE );
    1743                 bytesWritten += EVBUFFER_LENGTH( out );
    1744                 msgs->clientSentAnythingAt = now;
    1745                 tr_releaseBuffer( out );
    17461759            }
    17471760        }
Note: See TracChangeset for help on using the changeset viewer.