Changeset 3184


Ignore:
Timestamp:
Sep 26, 2007, 2:42:03 PM (15 years ago)
Author:
charles
Message:

peer i/o cleanup

Location:
trunk/libtransmission
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/handshake.c

    r3181 r3184  
    2222#include <event.h>
    2323
     24#include "clients.h"
    2425#include "transmission.h"
    2526#include "bencode.h"
     
    691692    tr_peerIoSetPeersId( handshake->io, handshake->peer_id );
    692693    handshake->havePeerID = TRUE;
    693     dbgmsg( handshake, "peer-id is [%*.*s]", PEER_ID_LEN, PEER_ID_LEN, handshake->peer_id );
     694    dbgmsg( handshake, "peer-id is [%s]", tr_clientForId(handshake->peer_id) );
    694695    if( !memcmp( handshake->peer_id, getPeerId(), PEER_ID_LEN ) ) {
    695696        dbgmsg( handshake, "streuth!  we've connected to ourselves." );
  • trunk/libtransmission/peer-io.c

    r3171 r3184  
    470470
    471471void
     472tr_peerIoWriteUint8( tr_peerIo        * io,
     473                     struct evbuffer  * outbuf,
     474                     uint8_t            writeme )
     475{
     476    tr_peerIoWriteBytes( io, outbuf, &writeme, sizeof(uint8_t) );
     477}
     478
     479void
    472480tr_peerIoWriteUint32( tr_peerIo        * io,
    473481                      struct evbuffer  * outbuf,
     
    517525
    518526void
     527tr_peerIoReadUint8( tr_peerIo         * io,
     528                    struct evbuffer   * inbuf,
     529                    uint8_t           * setme )
     530{
     531    tr_peerIoReadBytes( io, inbuf, setme, sizeof(uint8_t) );
     532}
     533
     534void
    519535tr_peerIoReadUint32( tr_peerIo         * io,
    520536                     struct evbuffer   * inbuf,
  • trunk/libtransmission/peer-io.h

    r3171 r3184  
    147147                            int                byteCount );
    148148
     149void tr_peerIoWriteUint8  ( tr_peerIo        * io,
     150                            struct evbuffer  * outbuf,
     151                            uint8_t            writeme );
     152
    149153void tr_peerIoWriteUint16 ( tr_peerIo        * io,
    150154                            struct evbuffer  * outbuf,
     
    159163                            void             * bytes,
    160164                            int                byteCount );
     165
     166void tr_peerIoReadUint8   ( tr_peerIo        * io,
     167                            struct evbuffer  * inbuf,
     168                            uint8_t          * setme );
    161169
    162170void tr_peerIoReadUint16  ( tr_peerIo        * io,
  • trunk/libtransmission/peer-mgr.c

    r3183 r3184  
    722722        tr_peerIoFree( io );
    723723        --manager->connectionCount;
    724         if( peer ) {
    725             tr_ptrArrayRemoveSorted( t->peers, peer, peerCompare );
    726             freePeer( peer );
    727         }
     724        if( peer )
     725            peer->doPurge = 1;
    728726        return;
    729727    }
     
    11791177        return TRUE;
    11801178
     1179    /* not enough peers to go around... might as well keep this one;
     1180     * they might unchoke us or give us a pex or something */
     1181    if( peerCount < MAX_CONNECTED_PEERS_PER_TORRENT )
     1182        return FALSE;
     1183
    11811184    /* when deciding whether or not to keep a peer, judge its responsiveness
    11821185       on a sliding scale that's based on how many other peers are available */
  • trunk/libtransmission/peer-msgs.c

    r3178 r3184  
    272272sendInterest( tr_peermsgs * msgs, int weAreInterested )
    273273{
    274     const uint32_t len = sizeof(uint8_t);
    275     const uint8_t bt_msgid = weAreInterested ? BT_INTERESTED : BT_NOT_INTERESTED;
    276 
    277274    assert( msgs != NULL );
    278275    assert( weAreInterested==0 || weAreInterested==1 );
     
    280277    msgs->info->clientIsInterested = weAreInterested;
    281278    dbgmsg( msgs, ": sending an %s message", (weAreInterested ? "INTERESTED" : "NOT_INTERESTED") );
    282     tr_peerIoWriteUint32( msgs->io, msgs->outMessages, len );
    283     tr_peerIoWriteBytes( msgs->io, msgs->outMessages, &bt_msgid, 1 );
     279
     280    tr_peerIoWriteUint32( msgs->io, msgs->outMessages, sizeof(uint8_t) );
     281    tr_peerIoWriteUint8 ( msgs->io, msgs->outMessages, weAreInterested ? BT_INTERESTED : BT_NOT_INTERESTED );
    284282}
    285283
     
    303301    if( msgs->info->peerIsChoked != choke )
    304302    {
    305         const uint32_t len = sizeof(uint8_t);
    306         const uint8_t bt_msgid = choke ? BT_CHOKE : BT_UNCHOKE;
    307 
    308303        msgs->info->peerIsChoked = choke ? 1 : 0;
    309304        if( msgs->info )
     
    314309
    315310        dbgmsg( msgs, "sending a %s message", (choke ? "CHOKE" : "UNCHOKE") );
    316         tr_peerIoWriteUint32( msgs->io, msgs->outMessages, len );
    317         tr_peerIoWriteBytes( msgs->io, msgs->outMessages, &bt_msgid, 1 );
     311        tr_peerIoWriteUint32( msgs->io, msgs->outMessages, sizeof(uint8_t) );
     312        tr_peerIoWriteUint8 ( msgs->io, msgs->outMessages, choke ? BT_CHOKE : BT_UNCHOKE );
    318313    }
    319314}
     
    343338    {
    344339        /* cancel the request */
    345         const uint8_t bt_msgid = BT_CANCEL;
    346         const uint32_t len = sizeof(uint8_t) + 3 * sizeof(uint32_t);
    347         tr_peerIoWriteUint32( msgs->io, msgs->outMessages, len );
    348         tr_peerIoWriteBytes( msgs->io, msgs->outMessages, &bt_msgid, 1 );
     340        tr_peerIoWriteUint32( msgs->io, msgs->outMessages, sizeof(uint8_t) + 3*sizeof(uint32_t) );
     341        tr_peerIoWriteUint8 ( msgs->io, msgs->outMessages, BT_CANCEL );
    349342        tr_peerIoWriteUint32( msgs->io, msgs->outMessages, pieceIndex );
    350343        tr_peerIoWriteUint32( msgs->io, msgs->outMessages, offset );
     
    364357                 uint32_t      pieceIndex )
    365358{
    366     const uint8_t bt_msgid = BT_HAVE;
    367     const uint32_t len = sizeof(uint8_t) + sizeof(uint32_t);
    368359    dbgmsg( msgs, "w00t telling them we HAVE piece #%d", pieceIndex );
    369     tr_peerIoWriteUint32( msgs->io, msgs->outMessages, len );
    370     tr_peerIoWriteBytes( msgs->io, msgs->outMessages, &bt_msgid, 1 );
     360
     361    tr_peerIoWriteUint32( msgs->io, msgs->outMessages, sizeof(uint8_t) + sizeof(uint32_t) );
     362    tr_peerIoWriteUint8 ( msgs->io, msgs->outMessages, BT_HAVE );
    371363    tr_peerIoWriteUint32( msgs->io, msgs->outMessages, pieceIndex );
    372364
     
    410402                       uint32_t      length )
    411403{
    412     const uint8_t bt_msgid = BT_REQUEST;
    413     const uint32_t len = sizeof(uint8_t) + 3 * sizeof(uint32_t);
    414404    struct peer_request * req;
    415405    int maxSize;
     
    433423
    434424    /* queue the request */
    435     tr_peerIoWriteUint32( msgs->io, msgs->outMessages, len );
    436     tr_peerIoWriteBytes( msgs->io, msgs->outMessages, &bt_msgid, 1 );
     425    tr_peerIoWriteUint32( msgs->io, msgs->outMessages, sizeof(uint8_t) + 3*sizeof(uint32_t) );
     426    tr_peerIoWriteUint8 ( msgs->io, msgs->outMessages, BT_REQUEST );
    437427    tr_peerIoWriteUint32( msgs->io, msgs->outMessages, index );
    438428    tr_peerIoWriteUint32( msgs->io, msgs->outMessages, offset );
     
    464454    const int port = tr_getPublicPort( msgs->handle );
    465455    struct evbuffer * outbuf = evbuffer_new( );
    466     uint32_t msglen;
    467     const uint8_t tr_msgid = 20; /* ltep extension id */
    468     const uint8_t ltep_msgid = 0; /* handshake id */
    469456
    470457    dbgmsg( msgs, "sending an ltep handshake" );
     
    480467    buf = tr_bencSaveMalloc( &val,  &len );
    481468
    482     msglen = sizeof(tr_msgid) + sizeof(ltep_msgid) + len;
    483     tr_peerIoWriteUint32( msgs->io, outbuf, msglen );
    484     tr_peerIoWriteBytes ( msgs->io, outbuf, &tr_msgid, 1 );
    485     tr_peerIoWriteBytes ( msgs->io, outbuf, &ltep_msgid, 1 );
     469    tr_peerIoWriteUint32( msgs->io, outbuf, 2*sizeof(uint8_t) + len );
     470    tr_peerIoWriteUint8 ( msgs->io, outbuf, BT_LTEP );
     471    tr_peerIoWriteUint8 ( msgs->io, outbuf, LTEP_HANDSHAKE );
    486472    tr_peerIoWriteBytes ( msgs->io, outbuf, buf, len );
    487473
    488474    tr_peerIoWriteBuf( msgs->io, outbuf );
    489 
    490475    msgs->hasSentLtepHandshake = 1;
    491476
     
    587572    uint8_t ltep_msgid;
    588573
    589     tr_peerIoReadBytes( msgs->io, inbuf, &ltep_msgid, 1 );
     574    tr_peerIoReadUint8( msgs->io, inbuf, &ltep_msgid );
    590575    msglen--;
    591576
     
    642627        return READ_MORE;
    643628
    644     tr_peerIoReadBytes( msgs->io, inbuf, &id, 1 );
     629    tr_peerIoReadUint8( msgs->io, inbuf, &id );
    645630    msglen--;
    646631    dbgmsg( msgs, "peer sent us a message... "
     
    10761061        struct peer_request * req = tr_list_pop_front( &msgs->peerAskedFor );
    10771062        uint8_t * tmp = tr_new( uint8_t, req->length );
    1078         const uint8_t msgid = BT_PIECE;
    1079         const uint32_t msglen = sizeof(uint8_t) + sizeof(uint32_t)*2 + req->length;
     1063        const uint32_t msglen = sizeof(uint8_t) + 2*sizeof(uint32_t) + req->length;
    10801064        tr_ioRead( msgs->torrent, req->index, req->offset, req->length, tmp );
    10811065        tr_peerIoWriteUint32( msgs->io, msgs->outBlock, msglen );
    1082         tr_peerIoWriteBytes ( msgs->io, msgs->outBlock, &msgid, 1 );
     1066        tr_peerIoWriteUint8 ( msgs->io, msgs->outBlock, BT_PIECE );
    10831067        tr_peerIoWriteUint32( msgs->io, msgs->outBlock, req->index );
    10841068        tr_peerIoWriteUint32( msgs->io, msgs->outBlock, req->offset );
     
    11081092{
    11091093    const tr_bitfield * bitfield = tr_cpPieceBitfield( msgs->torrent->completion );
    1110     const uint32_t len = sizeof(uint8_t) + bitfield->len;
    1111     const uint8_t bt_msgid = BT_BITFIELD;
    11121094
    11131095    dbgmsg( msgs, "sending peer a bitfield message\n", msgs );
    1114     tr_peerIoWriteUint32( msgs->io, msgs->outMessages, len );
    1115     tr_peerIoWriteBytes( msgs->io, msgs->outMessages, &bt_msgid, 1 );
    1116     tr_peerIoWriteBytes( msgs->io, msgs->outMessages, bitfield->bits, bitfield->len );
     1096    tr_peerIoWriteUint32( msgs->io, msgs->outMessages, sizeof(uint8_t) + bitfield->len );
     1097    tr_peerIoWriteUint8 ( msgs->io, msgs->outMessages, BT_BITFIELD );
     1098    tr_peerIoWriteBytes ( msgs->io, msgs->outMessages, bitfield->bits, bitfield->len );
    11171099}
    11181100
     
    11851167        char * benc;
    11861168        int bencLen;
    1187         const uint8_t bt_msgid = BT_LTEP;
    1188         const uint8_t ltep_msgid = OUR_LTEP_PEX;
    11891169
    11901170        /* build the diffs */
     
    12431223        /* write the pex message */
    12441224        benc = tr_bencSaveMalloc( &val, &bencLen );
    1245         tr_peerIoWriteUint32( msgs->io, msgs->outMessages, 1 + 1 + bencLen );
    1246         tr_peerIoWriteBytes ( msgs->io, msgs->outMessages, &bt_msgid, 1 );
    1247         tr_peerIoWriteBytes ( msgs->io, msgs->outMessages, &ltep_msgid, 1 );
     1225        tr_peerIoWriteUint32( msgs->io, msgs->outMessages, 2*sizeof(uint8_t) + bencLen );
     1226        tr_peerIoWriteUint8 ( msgs->io, msgs->outMessages, BT_LTEP );
     1227        tr_peerIoWriteUint8 ( msgs->io, msgs->outMessages, OUR_LTEP_PEX );
    12481228        tr_peerIoWriteBytes ( msgs->io, msgs->outMessages, benc, bencLen );
    12491229
Note: See TracChangeset for help on using the changeset viewer.