Changeset 7419 for trunk/libtransmission/peer-io.c
- Timestamp:
- Dec 16, 2008, 10:08:17 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/peer-io.c
r7404 r7419 28 28 #include "bandwidth.h" 29 29 #include "crypto.h" 30 #include "iobuf.h"31 30 #include "list.h" 32 31 #include "net.h" … … 80 79 struct tr_peerIo 81 80 { 82 tr_bool isEncrypted; 83 tr_bool isIncoming; 84 tr_bool peerIdIsSet; 85 tr_bool extendedProtocolSupported; 86 tr_bool fastExtensionSupported; 87 88 int magicNumber; 89 90 uint8_t encryptionMode; 91 uint8_t timeout; 92 tr_port port; 93 int socket; 94 95 uint8_t peerId[20]; 96 time_t timeCreated; 97 98 tr_session * session; 99 100 tr_address addr; 101 struct tr_iobuf * iobuf; 102 tr_list * output_datatypes; /* struct tr_datatype */ 103 104 tr_can_read_cb canRead; 105 tr_did_write_cb didWrite; 106 tr_net_error_cb gotError; 107 void * userData; 108 109 size_t bufferSize[2]; 110 111 tr_bandwidth * bandwidth; 112 tr_crypto * crypto; 81 tr_bool isEncrypted; 82 tr_bool isIncoming; 83 tr_bool peerIdIsSet; 84 tr_bool extendedProtocolSupported; 85 tr_bool fastExtensionSupported; 86 87 int magicNumber; 88 89 uint8_t encryptionMode; 90 uint8_t timeout; 91 tr_port port; 92 int socket; 93 94 uint8_t peerId[20]; 95 time_t timeCreated; 96 97 tr_session * session; 98 99 tr_address addr; 100 tr_list * output_datatypes; /* struct tr_datatype */ 101 102 tr_can_read_cb canRead; 103 tr_did_write_cb didWrite; 104 tr_net_error_cb gotError; 105 void * userData; 106 107 size_t bufferSize[2]; 108 109 tr_bandwidth * bandwidth; 110 tr_crypto * crypto; 111 112 struct evbuffer * inbuf; 113 struct evbuffer * outbuf; 113 114 }; 114 115 … … 118 119 119 120 static void 120 didWriteWrapper( struct tr_iobuf * iobuf,121 size_t 122 void 121 didWriteWrapper( void * unused UNUSED, 122 size_t bytes_transferred, 123 void * vio ) 123 124 { 124 125 tr_peerIo * io = vio; … … 143 144 tr_free( tr_list_pop_front( &io->output_datatypes ) ); 144 145 } 145 146 if( EVBUFFER_LENGTH( tr_iobuf_output( iobuf ) ) )147 tr_iobuf_enable( io->iobuf, EV_WRITE );148 146 } 149 147 150 148 static void 151 canReadWrapper( struct tr_iobuf * iobuf,152 size_t 153 void 149 canReadWrapper( void * unused UNUSED, 150 size_t bytes_transferred UNUSED, 151 void * vio ) 154 152 { 155 153 int done = 0; … … 168 166 { 169 167 size_t piece = 0; 170 const size_t oldLen = EVBUFFER_LENGTH( tr_iobuf_input( iobuf ));171 const int ret = io->canRead( io buf, io->userData, &piece );168 const size_t oldLen = EVBUFFER_LENGTH( io->inbuf ); 169 const int ret = io->canRead( io, io->userData, &piece ); 172 170 173 171 if( ret != READ_ERR ) 174 172 { 175 const size_t used = oldLen - EVBUFFER_LENGTH( tr_iobuf_input( iobuf ));173 const size_t used = oldLen - EVBUFFER_LENGTH( io->inbuf ); 176 174 if( piece ) 177 175 tr_bandwidthUsed( io->bandwidth, TR_DOWN, piece, TRUE ); … … 183 181 { 184 182 case READ_NOW: 185 if( EVBUFFER_LENGTH( tr_iobuf_input( iobuf )))183 if( EVBUFFER_LENGTH( io->inbuf ) ) 186 184 continue; 187 185 done = 1; … … 202 200 } 203 201 202 #if 0 204 203 static void 205 204 gotErrorWrapper( struct tr_iobuf * iobuf, … … 212 211 c->gotError( iobuf, what, c->userData ); 213 212 } 214 215 /** 216 *** 217 **/ 218 213 #endif 214 215 /** 216 *** 217 **/ 218 219 #if 0 219 220 static void 220 221 bufevNew( tr_peerIo * io ) … … 231 232 tr_iobuf_settimeout( io->iobuf, io->timeout, io->timeout ); 232 233 } 234 #endif 233 235 234 236 static int … … 267 269 io->timeout = IO_TIMEOUT_SECS; 268 270 io->timeCreated = time( NULL ); 271 io->inbuf = evbuffer_new( ); 272 io->outbuf = evbuffer_new( ); 273 #if 0 269 274 bufevNew( io ); 275 #endif 270 276 tr_peerIoSetBandwidth( io, session->bandwidth ); 271 277 return io; … … 310 316 311 317 tr_peerIoSetBandwidth( io, NULL ); 312 tr_iobuf_free( io->iobuf ); 318 evbuffer_free( io->outbuf ); 319 evbuffer_free( io->inbuf ); 313 320 tr_netClose( io->socket ); 314 321 tr_cryptoFree( io->crypto ); … … 371 378 } 372 379 380 #if 0 373 381 static void 374 382 tr_peerIoTryRead( tr_peerIo * io ) … … 377 385 (*canReadWrapper)( io->iobuf, ~0, io ); 378 386 } 387 #endif 379 388 380 389 void … … 390 399 io->userData = userData; 391 400 401 #if 0 392 402 tr_peerIoTryRead( io ); 393 } 394 395 int 403 #endif 404 } 405 406 tr_bool 396 407 tr_peerIoIsIncoming( const tr_peerIo * c ) 397 408 { 398 return c->isIncoming ? 1 :0;409 return c->isIncoming != 0; 399 410 } 400 411 … … 415 426 416 427 tr_netSetTOS( io->socket, io->session->peerSocketTOS ); 417 tr_iobuf_free( io->iobuf ); 428 #if 0 418 429 bufevNew( io ); 430 #endif 419 431 420 432 tr_peerIoSetBandwidth( io, bandwidth ); … … 425 437 } 426 438 439 #if 0 427 440 void 428 441 tr_peerIoSetTimeoutSecs( tr_peerIo * io, … … 433 446 tr_iobuf_enable( io->iobuf, EV_READ | EV_WRITE ); 434 447 } 448 #endif 435 449 436 450 /** … … 557 571 { 558 572 const size_t desiredLen = getDesiredOutputBufferSize( io ); 559 const size_t currentLen = EVBUFFER_LENGTH( tr_iobuf_output( io->iobuf ));573 const size_t currentLen = EVBUFFER_LENGTH( io->outbuf ); 560 574 size_t freeSpace = 0; 561 575 … … 573 587 574 588 if( io->bandwidth ) 575 tr_bandwidthRemove Buffer( io->bandwidth, io->iobuf);589 tr_bandwidthRemovePeer( io->bandwidth, io ); 576 590 577 591 io->bandwidth = bandwidth; 578 tr_iobuf_set_bandwidth( io->iobuf, bandwidth );579 592 580 593 if( io->bandwidth ) 581 tr_bandwidthAddBuffer( io->bandwidth, io->iobuf ); 582 583 tr_iobuf_enable( io->iobuf, EV_READ | EV_WRITE ); 594 tr_bandwidthAddPeer( io->bandwidth, io ); 584 595 } 585 596 … … 630 641 tr_list_append( &io->output_datatypes, datatype ); 631 642 632 evbuffer_add( tr_iobuf_output( io->iobuf ), writeme, writemeLen ); 633 tr_iobuf_enable( io->iobuf, EV_WRITE ); 643 evbuffer_add( io->outbuf, writeme, writemeLen ); 634 644 } 635 645 … … 777 787 return time( NULL ) - io->timeCreated; 778 788 } 789 790 /*** 791 **** 792 ***/ 793 794 static int 795 tr_peerIoTryRead( tr_peerIo * io, size_t howmuch ) 796 { 797 int res; 798 799 assert( isPeerIo( io ) ); 800 801 howmuch = tr_bandwidthClamp( io->bandwidth, TR_DOWN, howmuch ); 802 803 res = howmuch ? evbuffer_read( io->inbuf, io->socket, howmuch ) : 0; 804 805 dbgmsg( io, "read %d from peer (%s)", res, (res==-1?strerror(errno):"") ); 806 807 if( res > 0 ) 808 canReadWrapper( io, res, io ); 809 810 if( ( res <= 0 ) && ( io->gotError ) && ( errno != EAGAIN ) && ( errno != EINTR ) ) 811 { 812 short what = EVBUFFER_READ | EVBUFFER_ERROR; 813 if( res == 0 ) 814 what |= EVBUFFER_EOF; 815 io->gotError( io, what, io->userData ); 816 } 817 818 return res; 819 } 820 821 static int 822 tr_peerIoTryWrite( tr_peerIo * io, size_t howmuch ) 823 { 824 int n; 825 826 assert( isPeerIo( io ) ); 827 828 howmuch = tr_bandwidthClamp( io->bandwidth, TR_UP, howmuch ); 829 howmuch = MIN( howmuch, EVBUFFER_LENGTH( io->outbuf ) ); 830 n = (int) howmuch; 831 832 #ifdef WIN32 833 n = send( io->socket, EVBUFFER_DATA( io->outbuf ), n, 0 ); 834 #else 835 n = write( io->socket, EVBUFFER_DATA( io->outbuf ), n ); 836 #endif 837 dbgmsg( io, "wrote %d to peer (%s)", n, (n==-1?strerror(errno):"") ); 838 839 if( n > 0 ) 840 { 841 evbuffer_drain( io->outbuf, n ); 842 843 didWriteWrapper( NULL, n, io ); 844 } 845 846 if( ( n < 0 ) && ( io->gotError ) && ( errno != EPIPE ) && ( errno != EAGAIN ) && ( errno != EINTR ) && ( errno != EINPROGRESS ) ) 847 { 848 short what = EVBUFFER_WRITE | EVBUFFER_ERROR; 849 io->gotError( io, what, io->userData ); 850 } 851 852 return n; 853 } 854 855 int 856 tr_peerIoFlush( tr_peerIo * io, tr_direction dir, size_t limit ) 857 { 858 int ret; 859 860 assert( isPeerIo( io ) ); 861 assert( dir==TR_UP || dir==TR_DOWN ); 862 863 if( dir==TR_DOWN ) 864 ret = tr_peerIoTryRead( io, limit ); 865 else 866 ret = tr_peerIoTryWrite( io, limit ); 867 868 return ret; 869 } 870 871 struct evbuffer * 872 tr_peerIoGetReadBuffer( tr_peerIo * io ) 873 { 874 assert( isPeerIo( io ) ); 875 876 return io->inbuf; 877 }
Note: See TracChangeset
for help on using the changeset viewer.