/* */ #ifndef D_DEFAULT_BT_INTERACTIVE_H #define D_DEFAULT_BT_INTERACTIVE_H #include "BtInteractive.h" #include #include #include "TimerA2.h" #include "Command.h" namespace aria2 { class DownloadContext; class BtRuntime; class PieceStorage; class PeerStorage; class Peer; class BtMessage; class BtMessageReceiver; class BtMessageDispatcher; class BtMessageFactory; class BtRequestFactory; class PeerConnection; class ExtensionMessageFactory; class ExtensionMessageRegistry; class DHTNode; class RequestGroupMan; class UTMetadataRequestFactory; class UTMetadataRequestTracker; class FloodingStat { private: int chokeUnchokeCount; int keepAliveCount; public: FloodingStat():chokeUnchokeCount(0), keepAliveCount(0) {} void incChokeUnchokeCount() { if(chokeUnchokeCount < INT_MAX) { ++chokeUnchokeCount; } } void incKeepAliveCount() { if(keepAliveCount < INT_MAX) { ++keepAliveCount; } } int getChokeUnchokeCount() const { return chokeUnchokeCount; } int getKeepAliveCount() const { return keepAliveCount; } void reset() { chokeUnchokeCount = 0; keepAliveCount = 0; } }; class DefaultBtInteractive : public BtInteractive { private: cuid_t cuid_; std::shared_ptr downloadContext_; std::shared_ptr btRuntime_; std::shared_ptr pieceStorage_; std::shared_ptr peerStorage_; std::shared_ptr peer_; std::shared_ptr btMessageReceiver_; std::shared_ptr dispatcher_; std::shared_ptr btRequestFactory_; // Although peerStorage_ is not used in this class, this object // holds the reference so that peerConnection_ is not deleted. std::shared_ptr peerConnection_; std::shared_ptr messageFactory_; std::unique_ptr extensionMessageFactory_; std::shared_ptr extensionMessageRegistry_; std::shared_ptr utMetadataRequestFactory_; std::shared_ptr utMetadataRequestTracker_; bool metadataGetMode_; DHTNode* localNode_; size_t allowedFastSetSize_; Timer haveTimer_; Timer keepAliveTimer_; Timer floodingTimer_; FloodingStat floodingStat_; Timer inactiveTimer_; Timer pexTimer_; Timer perSecTimer_; time_t keepAliveInterval_; bool utPexEnabled_; bool dhtEnabled_; size_t numReceivedMessage_; size_t maxOutstandingRequest_; RequestGroupMan* requestGroupMan_; uint16_t tcpPort_; std::vector haveIndexes_; Timer haveLastSent_; static const time_t FLOODING_CHECK_INTERVAL = 5; void addBitfieldMessageToQueue(); void addAllowedFastMessageToQueue(); void addHandshakeExtendedMessageToQueue(); void decideChoking(); void checkHave(); void sendKeepAlive(); void decideInterest(); void fillPiece(size_t maxMissingBlock); void addRequests(); void detectMessageFlooding(); void checkActiveInteraction(); void addPeerExchangeMessage(); void addPortMessageToQueue(); public: DefaultBtInteractive(const std::shared_ptr& downloadContext, const std::shared_ptr& peer); virtual ~DefaultBtInteractive(); virtual void initiateHandshake(); virtual std::unique_ptr receiveHandshake (bool quickReply = false); virtual std::unique_ptr receiveAndSendHandshake(); virtual void doPostHandshakeProcessing(); virtual void doInteractionProcessing(); virtual void cancelAllPiece(); virtual void sendPendingMessage(); size_t receiveMessages(); virtual size_t countPendingMessage(); virtual bool isSendingMessageInProgress(); virtual size_t countReceivedMessageInIteration() const; virtual size_t countOutstandingRequest(); void setCuid(cuid_t cuid) { cuid_ = cuid; } void setBtRuntime(const std::shared_ptr& btRuntime); void setPieceStorage(const std::shared_ptr& pieceStorage); void setPeerStorage(const std::shared_ptr& peerStorage); void setPeer(const std::shared_ptr& peer); void setBtMessageReceiver(const std::shared_ptr& receiver); void setDispatcher(const std::shared_ptr& dispatcher); void setBtRequestFactory(const std::shared_ptr& factory); void setPeerConnection(const std::shared_ptr& peerConnection); void setBtMessageFactory(const std::shared_ptr& factory); void setExtensionMessageFactory (std::unique_ptr factory); void setExtensionMessageRegistry (const std::shared_ptr& registry) { extensionMessageRegistry_ = registry; } void setKeepAliveInterval(time_t keepAliveInterval) { keepAliveInterval_ = keepAliveInterval; } void setUTPexEnabled(bool f) { utPexEnabled_ = f; } void setLocalNode(DHTNode* node); void setDHTEnabled(bool f) { dhtEnabled_ = f; } void setRequestGroupMan(RequestGroupMan* rgman); void setUTMetadataRequestTracker (const std::shared_ptr& tracker) { utMetadataRequestTracker_ = tracker; } void setUTMetadataRequestFactory (const std::shared_ptr& factory) { utMetadataRequestFactory_ = factory; } void enableMetadataGetMode() { metadataGetMode_ = true; } void setTcpPort(uint16_t port) { tcpPort_ = port; } }; } // namespace aria2 #endif // D_DEFAULT_BT_INTERACTIVE_H