Changeset 7283
- Timestamp:
- Dec 5, 2008, 4:02:06 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/1.3x/libtransmission/peer-msgs.c
r6565 r7283 1818 1818 if( msgs->peerSupportsPex && tr_torrentAllowsPex( msgs->torrent ) ) 1819 1819 { 1820 int i;1820 PexDiffs diffs; 1821 1821 tr_pex * newPex = NULL; 1822 const int newCount = tr_peerMgrGetPeers( msgs->handle->peerMgr, msgs->torrent->info.hash, &newPex ); 1823 PexDiffs diffs; 1824 tr_benc val, *added, *dropped; 1825 uint8_t *tmp, *walk; 1826 char * benc; 1827 int bencLen; 1828 struct evbuffer * out = msgs->outMessages; 1822 const int newCount = tr_peerMgrGetPeers( msgs->handle->peerMgr, 1823 msgs->torrent->info.hash, 1824 &newPex ); 1829 1825 1830 1826 /* build the diffs */ … … 1839 1835 tr_pexCompare, sizeof(tr_pex), 1840 1836 pexDroppedCb, pexAddedCb, pexElementCb, &diffs ); 1841 dbgmsg( msgs, "pex: old peer count %d, new peer count %d, added %d, removed %d", msgs->pexCount, newCount, diffs.addedCount, diffs.droppedCount ); 1842 1843 /* update peer */ 1844 tr_free( msgs->pex ); 1845 msgs->pex = diffs.elements; 1846 msgs->pexCount = diffs.elementCount; 1847 1848 /* build the pex payload */ 1849 tr_bencInitDict( &val, 3 ); 1850 1851 /* "added" */ 1852 added = tr_bencDictAdd( &val, "added" ); 1853 tmp = walk = tr_new( uint8_t, diffs.addedCount * 6 ); 1854 for( i=0; i<diffs.addedCount; ++i ) { 1855 memcpy( walk, &diffs.added[i].in_addr, 4 ); walk += 4; 1856 memcpy( walk, &diffs.added[i].port, 2 ); walk += 2; 1837 dbgmsg( msgs, 1838 "pex: old peer count %d, new peer count %d, added %d, removed %d", 1839 msgs->pexCount, newCount, diffs.addedCount, diffs.droppedCount ); 1840 1841 if( !diffs.addedCount && !diffs.droppedCount ) 1842 { 1843 tr_free( diffs.elements ); 1857 1844 } 1858 assert( ( walk - tmp ) == diffs.addedCount * 6 ); 1859 tr_bencInitStr( added, tmp, walk-tmp, FALSE ); 1860 1861 /* "added.f" */ 1862 tmp = walk = tr_new( uint8_t, diffs.addedCount ); 1863 for( i=0; i<diffs.addedCount; ++i ) 1864 *walk++ = diffs.added[i].flags; 1865 assert( ( walk - tmp ) == diffs.addedCount ); 1866 tr_bencDictAddRaw( &val, "added.f", tmp, walk-tmp ); 1867 tr_free( tmp ); 1868 1869 /* "dropped" */ 1870 dropped = tr_bencDictAdd( &val, "dropped" ); 1871 tmp = walk = tr_new( uint8_t, diffs.droppedCount * 6 ); 1872 for( i=0; i<diffs.droppedCount; ++i ) { 1873 memcpy( walk, &diffs.dropped[i].in_addr, 4 ); walk += 4; 1874 memcpy( walk, &diffs.dropped[i].port, 2 ); walk += 2; 1845 else 1846 { 1847 int i; 1848 tr_benc val, *added, *dropped; 1849 uint8_t *tmp, *walk; 1850 char * benc; 1851 int bencLen; 1852 struct evbuffer * out = msgs->outMessages; 1853 1854 /* update peer */ 1855 tr_free( msgs->pex ); 1856 msgs->pex = diffs.elements; 1857 msgs->pexCount = diffs.elementCount; 1858 1859 /* build the pex payload */ 1860 tr_bencInitDict( &val, 3 ); 1861 1862 /* "added" */ 1863 added = tr_bencDictAdd( &val, "added" ); 1864 tmp = walk = tr_new( uint8_t, diffs.addedCount * 6 ); 1865 for( i=0; i<diffs.addedCount; ++i ) { 1866 memcpy( walk, &diffs.added[i].in_addr, 4 ); walk += 4; 1867 memcpy( walk, &diffs.added[i].port, 2 ); walk += 2; 1868 } 1869 assert( ( walk - tmp ) == diffs.addedCount * 6 ); 1870 tr_bencInitStr( added, tmp, walk-tmp, FALSE ); 1871 1872 /* "added.f" */ 1873 tmp = walk = tr_new( uint8_t, diffs.addedCount ); 1874 for( i=0; i<diffs.addedCount; ++i ) 1875 *walk++ = diffs.added[i].flags; 1876 assert( ( walk - tmp ) == diffs.addedCount ); 1877 tr_bencDictAddRaw( &val, "added.f", tmp, walk-tmp ); 1878 tr_free( tmp ); 1879 1880 /* "dropped" */ 1881 dropped = tr_bencDictAdd( &val, "dropped" ); 1882 tmp = walk = tr_new( uint8_t, diffs.droppedCount * 6 ); 1883 for( i=0; i<diffs.droppedCount; ++i ) { 1884 memcpy( walk, &diffs.dropped[i].in_addr, 4 ); walk += 4; 1885 memcpy( walk, &diffs.dropped[i].port, 2 ); walk += 2; 1886 } 1887 assert( ( walk - tmp ) == diffs.droppedCount * 6 ); 1888 tr_bencInitStr( dropped, tmp, walk-tmp, FALSE ); 1889 1890 /* write the pex message */ 1891 benc = tr_bencSave( &val, &bencLen ); 1892 tr_peerIoWriteUint32( msgs->io, out, 2*sizeof(uint8_t) + bencLen ); 1893 tr_peerIoWriteUint8 ( msgs->io, out, BT_LTEP ); 1894 tr_peerIoWriteUint8 ( msgs->io, out, msgs->ut_pex_id ); 1895 tr_peerIoWriteBytes ( msgs->io, out, benc, bencLen ); 1896 pokeBatchPeriod( msgs, IMMEDIATE_PRIORITY_INTERVAL_SECS ); 1897 dbgmsg( msgs, "outMessage size is now %d", (int)EVBUFFER_LENGTH(out) ); 1898 1899 tr_free( benc ); 1900 tr_bencFree( &val ); 1875 1901 } 1876 assert( ( walk - tmp ) == diffs.droppedCount * 6 );1877 tr_bencInitStr( dropped, tmp, walk-tmp, FALSE );1878 1879 /* write the pex message */1880 benc = tr_bencSave( &val, &bencLen );1881 tr_peerIoWriteUint32( msgs->io, out, 2*sizeof(uint8_t) + bencLen );1882 tr_peerIoWriteUint8 ( msgs->io, out, BT_LTEP );1883 tr_peerIoWriteUint8 ( msgs->io, out, msgs->ut_pex_id );1884 tr_peerIoWriteBytes ( msgs->io, out, benc, bencLen );1885 pokeBatchPeriod( msgs, IMMEDIATE_PRIORITY_INTERVAL_SECS );1886 dbgmsg( msgs, "outMessage size is now %d", (int)EVBUFFER_LENGTH(out) );1887 1902 1888 1903 /* cleanup */ 1889 tr_free( benc );1890 tr_bencFree( &val );1891 1904 tr_free( diffs.added ); 1892 1905 tr_free( diffs.dropped );
Note: See TracChangeset
for help on using the changeset viewer.