****
***/
-enum
-{
- HISTORY_MSEC = 2000,
- INTERVAL_MSEC = HISTORY_MSEC,
- GRANULARITY_MSEC = 50,
- HISTORY_SIZE = ( INTERVAL_MSEC / GRANULARITY_MSEC ),
- MAGIC_NUMBER = 43143
-};
-
-struct bratecontrol
-{
- int newest;
- struct { uint64_t date, size; } transfers[HISTORY_SIZE];
-};
-
static float
getSpeed( const struct bratecontrol * r, int interval_msec )
{
*******
******/
-struct tr_band
-{
- tr_bool isLimited;
- tr_bool honorParentLimits;
- size_t bytesLeft;
- double desiredSpeed;
- struct bratecontrol raw;
- struct bratecontrol piece;
-};
-
-struct tr_bandwidth
-{
- struct tr_band band[2];
- struct tr_bandwidth * parent;
- int magicNumber;
- tr_session * session;
- tr_ptrArray children; /* struct tr_bandwidth */
- tr_ptrArray peers; /* tr_peerIo */
-};
-
-/***
-****
-***/
-
static inline int
comparePointers( const void * a, const void * b )
{
return 0;
}
-tr_bool
-tr_isBandwidth( const tr_bandwidth * b )
-{
- return ( b != NULL ) && ( b->magicNumber == MAGIC_NUMBER );
-}
-
/***
****
***/
****
***/
-void
-tr_bandwidthSetDesiredSpeed( tr_bandwidth * b,
- tr_direction dir,
- double desiredSpeed )
-{
- assert( tr_isBandwidth( b ) );
- assert( tr_isDirection( dir ) );
-
- b->band[dir].desiredSpeed = desiredSpeed;
-}
-
-double
-tr_bandwidthGetDesiredSpeed( const tr_bandwidth * b,
- tr_direction dir )
-{
- assert( tr_isBandwidth( b ) );
- assert( tr_isDirection( dir ) );
-
- return b->band[dir].desiredSpeed;
-}
-
-void
-tr_bandwidthSetLimited( tr_bandwidth * b,
- tr_direction dir,
- tr_bool isLimited )
-{
- assert( tr_isBandwidth( b ) );
- assert( tr_isDirection( dir ) );
-
- b->band[dir].isLimited = isLimited;
-}
-
-tr_bool
-tr_bandwidthIsLimited( const tr_bandwidth * b,
- tr_direction dir )
-{
- assert( tr_isBandwidth( b ) );
- assert( tr_isDirection( dir ) );
-
- return b->band[dir].isLimited;
-}
-
#if 0
#warning do not check the code in with this enabled
#define DEBUG_DIRECTION TR_UP
assert( tr_isBandwidth( b ) );
assert( tr_isPeerIo( peerIo ) );
- tr_ptrArrayInsertSorted( &b->peers, peerIo, comparePointers );
}
void
#ifndef TR_BANDWIDTH_H
#define TR_BANDWIDTH_H
+#include "transmission.h"
+#include "ptrarray.h"
+
+struct tr_peerIo;
+
+/* these are PRIVATE IMPLEMENTATION details that should not be touched.
+ * it's included in the header for inlining and composition. */
+enum
+{
+ HISTORY_MSEC = 2000,
+ INTERVAL_MSEC = HISTORY_MSEC,
+ GRANULARITY_MSEC = 50,
+ HISTORY_SIZE = ( INTERVAL_MSEC / GRANULARITY_MSEC ),
+ MAGIC_NUMBER = 43143
+};
+
+/* these are PRIVATE IMPLEMENTATION details that should not be touched.
+ * it's included in the header for inlining and composition. */
+struct bratecontrol
+{
+ int newest;
+ struct { uint64_t date, size; } transfers[HISTORY_SIZE];
+};
+
+/* these are PRIVATE IMPLEMENTATION details that should not be touched.
+ * it's included in the header for inlining and composition. */
+struct tr_band
+{
+ tr_bool isLimited;
+ tr_bool honorParentLimits;
+ size_t bytesLeft;
+ double desiredSpeed;
+ struct bratecontrol raw;
+ struct bratecontrol piece;
+};
+
/**
* Bandwidth is an object for measuring and constraining bandwidth speeds.
*
* and call tr_bandwidthClamp() before performing I/O to see how much
* bandwidth they can safely use.
*/
+typedef struct tr_bandwidth
+{
+ /* these are PRIVATE IMPLEMENTATION details that should not be touched.
+ * it's included in the header for inlining and composition. */
-typedef struct tr_bandwidth tr_bandwidth;
+ struct tr_band band[2];
+ struct tr_bandwidth * parent;
+ int magicNumber;
+ tr_session * session;
+ tr_ptrArray children; /* struct tr_bandwidth */
+ tr_ptrArray peers; /* tr_peerIo */
+}
+tr_bandwidth;
-struct tr_peerIo;
/**
***
void tr_bandwidthFree ( tr_bandwidth * bandwidth );
/** @brief test to see if the pointer refers to a live bandwidth object */
-extern inline tr_bool
- tr_isBandwidth ( const tr_bandwidth * bandwidth );
+static inline tr_bool tr_isBandwidth( const tr_bandwidth * b )
+{
+ return ( b != NULL ) && ( b->magicNumber == MAGIC_NUMBER );
+}
/******
*******
* @see tr_bandwidthAllocate
* @see tr_bandwidthGetDesiredSpeed
*/
-extern inline void
- tr_bandwidthSetDesiredSpeed ( tr_bandwidth * bandwidth,
- tr_direction direction,
- double desiredSpeed );
+static inline void tr_bandwidthSetDesiredSpeed( tr_bandwidth * bandwidth,
+ tr_direction dir,
+ double desiredSpeed )
+{
+ bandwidth->band[dir].desiredSpeed = desiredSpeed;
+}
/**
* @brief Get the desired speed (in KiB/s) for ths bandwidth subtree.
* @see tr_bandwidthSetDesiredSpeed
*/
-extern inline double
- tr_bandwidthGetDesiredSpeed ( const tr_bandwidth * bandwidth,
- tr_direction direction );
+static inline double
+tr_bandwidthGetDesiredSpeed( const tr_bandwidth * bandwidth,
+ tr_direction dir )
+{
+ return bandwidth->band[dir].desiredSpeed;
+}
/**
* @brief Set whether or not this bandwidth should throttle its peer-io's speeds
*/
-extern inline void
- tr_bandwidthSetLimited ( tr_bandwidth * bandwidth,
- tr_direction direction,
- tr_bool isLimited );
+static inline void tr_bandwidthSetLimited( tr_bandwidth * bandwidth,
+ tr_direction dir,
+ tr_bool isLimited )
+{
+ bandwidth->band[dir].isLimited = isLimited;
+}
/**
* @return nonzero if this bandwidth throttles its peer-ios speeds
*/
-extern inline tr_bool
- tr_bandwidthIsLimited ( const tr_bandwidth * bandwidth,
- tr_direction direction );
+static inline tr_bool tr_bandwidthIsLimited( const tr_bandwidth * bandwidth,
+ tr_direction dir )
+{
+ return bandwidth->band[dir].isLimited;
+}
/**
* @brief allocate the next period_msec's worth of bandwidth for the peer-ios to consume
*******
******/
-/**
- * @brief Get the raw total of bytes read or sent by this bandwidth subtree.
- */
-extern inline double
- tr_bandwidthGetRawSpeed ( const tr_bandwidth * bandwidth,
- tr_direction direction );
+/** @brief Get the raw total of bytes read or sent by this bandwidth subtree. */
+double tr_bandwidthGetRawSpeed( const tr_bandwidth * bandwidth,
+ tr_direction direction );
-/**
- * @brief Get the number of piece data bytes read or sent by this bandwidth subtree.
- */
-extern inline double
- tr_bandwidthGetPieceSpeed ( const tr_bandwidth * bandwidth,
- tr_direction direction );
+/** @brief Get the number of piece data bytes read or sent by this bandwidth subtree. */
+double tr_bandwidthGetPieceSpeed( const tr_bandwidth * bandwidth,
+ tr_direction direction );
/**
* @brief Notify the bandwidth object that some of its allocated bandwidth has been consumed.
* @brief add a tr_peerIo to this bandwidth's list.
* They will be notified when more bandwidth is made available for them to consume.
*/
-extern inline void
- tr_bandwidthAddPeer ( tr_bandwidth * bandwidth,
- struct tr_peerIo * peerIo );
+void tr_bandwidthAddPeer( tr_bandwidth * bandwidth,
+ struct tr_peerIo * peerIo );
/**
* @brief remove a peer-io from this bandwidth's list.
*/
-extern inline void
- tr_bandwidthRemovePeer ( tr_bandwidth * bandwidth,
- struct tr_peerIo * peerIo );
+void tr_bandwidthRemovePeer( tr_bandwidth * bandwidth,
+ struct tr_peerIo * peerIo );
#endif
struct __tr_list head;
};
-struct tr_peerIo
-{
- tr_bool isEncrypted;
- tr_bool isIncoming;
- tr_bool peerIdIsSet;
- tr_bool extendedProtocolSupported;
- tr_bool fastExtensionSupported;
-
- int magicNumber;
-
- uint8_t encryptionMode;
- tr_port port;
- int socket;
-
- uint8_t peerId[SHA_DIGEST_LENGTH];
- time_t timeCreated;
-
- tr_session * session;
-
- tr_address addr;
-
- tr_can_read_cb canRead;
- tr_did_write_cb didWrite;
- tr_net_error_cb gotError;
- void * userData;
-
- tr_bandwidth * bandwidth;
- tr_crypto * crypto;
-
- struct evbuffer * inbuf;
- struct evbuffer * outbuf;
- struct __tr_list outbuf_datatypes; /* struct tr_datatype */
-
- struct event event_read;
- struct event event_write;
-};
-
/***
****
***/
}
}
-void
-tr_peerIoWriteUint8( tr_peerIo * io,
- struct evbuffer * outbuf,
- uint8_t writeme )
-{
- tr_peerIoWriteBytes( io, outbuf, &writeme, sizeof( uint8_t ) );
-}
-
-void
-tr_peerIoWriteUint16( tr_peerIo * io,
- struct evbuffer * outbuf,
- uint16_t writeme )
-{
- uint16_t tmp = htons( writeme );
-
- tr_peerIoWriteBytes( io, outbuf, &tmp, sizeof( uint16_t ) );
-}
-
-void
-tr_peerIoWriteUint32( tr_peerIo * io,
- struct evbuffer * outbuf,
- uint32_t writeme )
-{
- uint32_t tmp = htonl( writeme );
-
- tr_peerIoWriteBytes( io, outbuf, &tmp, sizeof( uint32_t ) );
-}
-
/***
****
***/
}
}
-void
-tr_peerIoReadUint8( tr_peerIo * io,
- struct evbuffer * inbuf,
- uint8_t * setme )
-{
- tr_peerIoReadBytes( io, inbuf, setme, sizeof( uint8_t ) );
-}
-
-void
-tr_peerIoReadUint16( tr_peerIo * io,
- struct evbuffer * inbuf,
- uint16_t * setme )
-{
- uint16_t tmp;
- tr_peerIoReadBytes( io, inbuf, &tmp, sizeof( uint16_t ) );
- *setme = ntohs( tmp );
-}
-
-void
-tr_peerIoReadUint32( tr_peerIo * io,
- struct evbuffer * inbuf,
- uint32_t * setme )
-{
- uint32_t tmp;
- tr_peerIoReadBytes( io, inbuf, &tmp, sizeof( uint32_t ) );
- *setme = ntohl( tmp );
-}
-
void
tr_peerIoDrain( tr_peerIo * io,
struct evbuffer * inbuf,
}
}
-int
-tr_peerIoGetAge( const tr_peerIo * io )
-{
- return time( NULL ) - io->timeCreated;
-}
-
/***
****
***/
return ret;
}
-struct evbuffer *
-tr_peerIoGetReadBuffer( tr_peerIo * io )
-{
- assert( tr_isPeerIo( io ) );
-
- return io->inbuf;
-}
-
-tr_bool
-tr_peerIoHasBandwidthLeft( const tr_peerIo * io, tr_direction dir )
-{
- assert( tr_isPeerIo( io ) );
-
- return tr_bandwidthClamp( io->bandwidth, dir, 1024 ) > 0;
-}
-
/***
****
****/
else
event_disable( io, event );
}
+
+tr_bool
+tr_peerIoHasBandwidthLeft( const tr_peerIo * io,
+ tr_direction dir )
+{
+ assert( tr_isPeerIo( io ) );
+
+ return tr_bandwidthClamp( io->bandwidth, dir, 1024 ) > 0;
+}
+
***
**/
+#include <assert.h>
+
+#include <event.h>
+
+#include "transmission.h"
+#include "list.h" /* __tr_list */
+#include "net.h" /* tr_address */
+
struct evbuffer;
-struct tr_address;
struct tr_bandwidth;
struct tr_crypto;
-typedef struct tr_peerIo tr_peerIo;
+struct tr_peerIo;
+
+typedef enum
+{
+ READ_NOW,
+ READ_LATER,
+ READ_ERR
+}
+ReadState;
+
+typedef ReadState ( *tr_can_read_cb )( struct tr_peerIo * io,
+ void * user_data,
+ size_t * setme_piece_byte_count );
+
+typedef void ( *tr_did_write_cb )( struct tr_peerIo * io,
+ size_t bytesWritten,
+ int wasPieceData,
+ void * userData );
+
+typedef void ( *tr_net_error_cb )( struct tr_peerIo * io,
+ short what,
+ void * userData );
+
+typedef struct tr_peerIo
+{
+ tr_bool isEncrypted;
+ tr_bool isIncoming;
+ tr_bool peerIdIsSet;
+ tr_bool extendedProtocolSupported;
+ tr_bool fastExtensionSupported;
+
+ int magicNumber;
+
+ uint8_t encryptionMode;
+ tr_port port;
+ int socket;
+
+ uint8_t peerId[SHA_DIGEST_LENGTH];
+ time_t timeCreated;
+
+ tr_session * session;
+
+ tr_address addr;
+
+ tr_can_read_cb canRead;
+ tr_did_write_cb didWrite;
+ tr_net_error_cb gotError;
+ void * userData;
+
+ struct tr_bandwidth * bandwidth;
+ struct tr_crypto * crypto;
+
+ struct evbuffer * inbuf;
+ struct evbuffer * outbuf;
+ struct __tr_list outbuf_datatypes; /* struct tr_datatype */
+
+ struct event event_read;
+ struct event event_write;
+}
+tr_peerIo;
/**
***
tr_bool tr_peerIoIsIncoming( const tr_peerIo * io );
-extern inline int tr_peerIoGetAge( const tr_peerIo * io );
+static inline int tr_peerIoGetAge( const tr_peerIo * io )
+{
+ return time( NULL ) - io->timeCreated;
+}
/**
***
**/
-typedef enum
-{
- READ_NOW,
- READ_LATER,
- READ_ERR
-}
-ReadState;
-
-typedef ReadState ( *tr_can_read_cb )( tr_peerIo * io,
- void * user_data,
- size_t * setme_piece_byte_count );
-
-typedef void ( *tr_did_write_cb )( tr_peerIo * io,
- size_t bytesWritten,
- int wasPieceData,
- void * userData );
-
-typedef void ( *tr_net_error_cb )( tr_peerIo * io,
- short what,
- void * userData );
-
void tr_peerIoSetIOFuncs ( tr_peerIo * io,
tr_can_read_cb readcb,
tr_did_write_cb writecb,
const void * bytes,
size_t byteCount );
-void tr_peerIoWriteUint8( tr_peerIo * io,
- struct evbuffer * outbuf,
- uint8_t writeme );
+static inline void tr_peerIoWriteUint8( tr_peerIo * io,
+ struct evbuffer * outbuf,
+ uint8_t writeme )
+{
+ tr_peerIoWriteBytes( io, outbuf, &writeme, sizeof( uint8_t ) );
+}
-void tr_peerIoWriteUint16( tr_peerIo * io,
- struct evbuffer * outbuf,
- uint16_t writeme );
+static inline void tr_peerIoWriteUint16( tr_peerIo * io,
+ struct evbuffer * outbuf,
+ uint16_t writeme )
+{
+ const uint16_t tmp = htons( writeme );
+ tr_peerIoWriteBytes( io, outbuf, &tmp, sizeof( uint16_t ) );
+}
-void tr_peerIoWriteUint32( tr_peerIo * io,
- struct evbuffer * outbuf,
- uint32_t writeme );
+static inline void tr_peerIoWriteUint32( tr_peerIo * io,
+ struct evbuffer * outbuf,
+ uint32_t writeme )
+{
+ const uint32_t tmp = htonl( writeme );
+ tr_peerIoWriteBytes( io, outbuf, &tmp, sizeof( uint32_t ) );
+}
void tr_peerIoReadBytes( tr_peerIo * io,
struct evbuffer * inbuf,
void * bytes,
size_t byteCount );
-extern inline void
- tr_peerIoReadUint8( tr_peerIo * io,
- struct evbuffer * inbuf,
- uint8_t * setme );
+static inline void tr_peerIoReadUint8( tr_peerIo * io,
+ struct evbuffer * inbuf,
+ uint8_t * setme )
+{
+ tr_peerIoReadBytes( io, inbuf, setme, sizeof( uint8_t ) );
+}
-extern inline void
- tr_peerIoReadUint16( tr_peerIo * io,
- struct evbuffer * inbuf,
- uint16_t * setme );
+static inline void tr_peerIoReadUint16( tr_peerIo * io,
+ struct evbuffer * inbuf,
+ uint16_t * setme )
+{
+ uint16_t tmp;
+ tr_peerIoReadBytes( io, inbuf, &tmp, sizeof( uint16_t ) );
+ *setme = ntohs( tmp );
+}
-extern inline void
- tr_peerIoReadUint32( tr_peerIo * io,
- struct evbuffer * inbuf,
- uint32_t * setme );
+static inline void tr_peerIoReadUint32( tr_peerIo * io,
+ struct evbuffer * inbuf,
+ uint32_t * setme )
+{
+ uint32_t tmp;
+ tr_peerIoReadBytes( io, inbuf, &tmp, sizeof( uint32_t ) );
+ *setme = ntohl( tmp );
+}
void tr_peerIoDrain( tr_peerIo * io,
struct evbuffer * inbuf,
size_t byteCount,
int isPieceData );
+tr_bool tr_peerIoHasBandwidthLeft( const tr_peerIo * io,
+ tr_direction dir );
+
/**
***
**/
-extern inline tr_bool tr_peerIoHasBandwidthLeft( const tr_peerIo * io,
- tr_direction direction );
-
void tr_peerIoSetEnabled( tr_peerIo * io,
tr_direction dir,
tr_bool isEnabled );
tr_direction dir,
size_t byteLimit );
-extern inline struct evbuffer * tr_peerIoGetReadBuffer( tr_peerIo * io );
+/**
+***
+**/
+static inline struct evbuffer * tr_peerIoGetReadBuffer( tr_peerIo * io )
+{
+ assert( tr_isPeerIo( io ) );
+ return io->inbuf;
+}
#endif
void tr_peerMsgsHave( tr_peermsgs * msgs,
uint32_t pieceIndex );
-extern inline void
- tr_peerMsgsPulse( tr_peermsgs * msgs );
+void tr_peerMsgsPulse( tr_peermsgs * msgs );
void tr_peerMsgsCancel( tr_peermsgs * msgs,
uint32_t pieceIndex,
uint32_t offset,
uint32_t length );
-extern inline void
- tr_peerMsgsUnsubscribe( tr_peermsgs * peer,
+void tr_peerMsgsUnsubscribe( tr_peermsgs * peer,
tr_publisher_tag tag );
size_t tr_generateAllowedSet( tr_piece_index_t * setmePieces,