2006-03-21 14:12:51 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
2006-09-21 15:31:24 +00:00
|
|
|
* aria2 - The high speed download utility
|
2006-03-21 14:12:51 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2010-01-05 16:01:46 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2006-09-21 15:31:24 +00:00
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link the code of portions of this program with the
|
|
|
|
* OpenSSL library under certain conditions as described in each
|
|
|
|
* individual source file, and distribute linked combinations
|
|
|
|
* including the two.
|
|
|
|
* You must obey the GNU General Public License in all respects
|
|
|
|
* for all of the code used other than OpenSSL. If you modify
|
|
|
|
* file(s) with this exception, you may extend this exception to your
|
|
|
|
* version of the file(s), but you are not obligated to do so. If you
|
|
|
|
* do not wish to do so, delete this exception statement from your
|
|
|
|
* version. If you delete this exception statement from all source
|
|
|
|
* files in the program, then also delete it here.
|
2006-03-21 14:12:51 +00:00
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "PeerInteractionCommand.h"
|
2008-11-03 06:49:02 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "DownloadEngine.h"
|
2006-03-21 14:12:51 +00:00
|
|
|
#include "PeerInitiateConnectionCommand.h"
|
2006-12-24 06:25:21 +00:00
|
|
|
#include "DefaultBtInteractive.h"
|
2006-03-21 14:12:51 +00:00
|
|
|
#include "DlAbortEx.h"
|
|
|
|
#include "message.h"
|
Added new class SendMessageQueue that includes PendingMessages
and
RequestSlotMan.
* src/SendMessageQueue.h: New class.
* src/SendMessageQueue.cc: New class.
* src/PendingMessage.h: Added new member variable blockIndex and
its
accessors.
(createRequestMessage): Updated.
* src/PendingMessage.cc (createRequestMessage): Updated.
* src/PeerInteractionCommand.cc (executeInternal): Updated with
SendMessageQueue.
(checkLongTimePeerChoking): Updated with SendMessageQueue.
(receiveMessage): Updated with SendMessageQueue.
(deletePendingPieceMessage): Removed.
(getNewPieceAndSendInterest): Updated with SendMessageQueue.
(sendInterest): Updated with SendMessageQueue.
(createRequestPendingMessage): Updated with SendMessageQueue.
(sendMessages): Updated with SendMessageQueue.
(onAbort): Updated with SendMessageQueue.
(keepAlive): Updated with SendMessageQueue.
(beforeSocketCheck): Updated SendMessageQueue.
* src/PeerInteractionCommand (sendMessages): Shuffle
missingBLockIndexes before using it.
Added its own timeout for peer connection.
* src/PeerAbstractCommand.h: Added member variable timeout and
its
setter.
* src/prefs.h: Added PREF_PEER_CONNECTION_TIMEOUT.
* src/PeerInteractionCommand.cc (PeerInteractionCommand):
Added setTimeout() call.
(executeInternal): Added setTimeout() call.
* src/PeerAbstractCommand.cc (PeerAbstractCommand):
Added timeout.
(isTimeoutDetected): Updated.
* src/main.cc (main): Added PREF_PEER_CONNECTION_TIMEOUT entry
to
option.
Added *simple* message flooding checker.
* src/PeerInteractionCommand.cc (executeInternal):
Added detectMessageFlooding() call.
(detectMessageFlooding): New function.
(receiveMessage): Count up CHOKE, UNCHOKE, HAVE message.
(beforeSocketCheck): Added detectMessageFlooding() call.
* src/PeerInteractionCommand.h: Added sendMessageQueue,
chokeUnchokeCount, haveCount, detectMessageFlooding().
Removed deletePendingPieceMessage(), getRequestSlot(),
deleteRequestSlot(), deleteAllRequestSlot().
* src/PeerInteractionCommand.cc (beforeSocketCheck):
Added checkLongTimePeerChoking() call.
* src/RequestSlotMan.h: Renamed deleteTimeoutRequestSlot().
* src/TorrentMan.cc (addPeer): Delete at most MAX_PEER_LIST_SIZE
peers
if duplicate == false.
The parameter "uploaded" and "downloaded" in the tracker request
are
the size since the client sent the "started" event to the
tracker.
* src/TorrentMan.cc (setup): Assigned saved downloaded Size and
uploaded size to preDownloadedSize, preUploadedSize
respectively.
* src/TorrentMan.h: Added preDownloadedSize, preUploadedSize,
getSessionDownloadedSize(), getSessionUploadedSize().
* src/TrackerInitCommand.cc (execute): Use
getSessionDownloadedSize(),
getSessionUploadedSize() instead of getDownloadedSize(),
getUploadedSize().
* src/PendingMessage.cc (processMessage): Do not send request
message
if the peer is choking the client.
* src/TrackerUpdateCommand.cc (execute): Check wtheher
minInterval is
less than interval.
2006-03-28 15:23:51 +00:00
|
|
|
#include "prefs.h"
|
2012-09-28 14:27:46 +00:00
|
|
|
#include "SocketCore.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Option.h"
|
2009-06-28 10:37:15 +00:00
|
|
|
#include "DownloadContext.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Peer.h"
|
|
|
|
#include "BtMessage.h"
|
2013-07-04 13:35:41 +00:00
|
|
|
#include "BtHandshakeMessage.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "BtRuntime.h"
|
|
|
|
#include "PeerStorage.h"
|
2006-12-24 06:25:21 +00:00
|
|
|
#include "DefaultBtMessageDispatcher.h"
|
|
|
|
#include "DefaultBtMessageReceiver.h"
|
|
|
|
#include "DefaultBtRequestFactory.h"
|
|
|
|
#include "DefaultBtMessageFactory.h"
|
|
|
|
#include "DefaultBtInteractive.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "PeerConnection.h"
|
|
|
|
#include "ExtensionMessageFactory.h"
|
2008-02-10 15:57:00 +00:00
|
|
|
#include "DHTRoutingTable.h"
|
2008-02-01 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Added DHT functionality, compatible with mainline.
DHT is disabled by default. To enable it, give --enable-dht to
aria2c.
You may need to specify entry point to DHT network using
--dht-entry-point. DHT uses UDP port to listen incoming message.
Use --dht-listen-port to specify port number. Make sure that
your
firewall configuration can pass through UDP traffic to the port.
The routing table is saved in $HOME/.aria2/dht.dat.
* src/DHT*
* src/BNode.{h, cc}
* src/PeerInteractionCommand.cc: enable DHT functionality for a
particular torrent.
* src/Data.cc: Rewritten ctor.
* src/OptionHandlerFactory.cc: Added --enable-dht,
--dht-listen-port,
--dht-entry-point.
* src/DefaultBtInteractive.cc: Send port message if dht is
enabled.
* src/RequestGroup.cc: Initialize DHT functionality. When
download
ends, remove BtContext from DHTPeerAnnounceStorage.
* src/BtPortMessage.{h, cc}: Rewritten.
* src/message.h
* src/OptionHandlerImpl.cc
* src/option_processing.cc: Added --enable-dht,
--dht-listen-port,
--dht-entry-point.
* src/Dictionary.{h, cc} (remove): New function.
* src/prefs.h
* src/DefaultBtMessageFactory.h
* src/BtHandshakeMessage.cc
* src/ActivePeerConnectionCommand.cc
* src/SocketCore.{h, cc}: Added datagram socket support.
* src/DefaultBtMessageFactory.cc
* src/BtSetup.cc: Add BtContext to DHTPeerAnnounceStorage here.
Create DHT commands.
* src/BtMessageFactory.h
* src/PeerMessageUtil.{h, cc}
2008-02-01 17:36:33 +00:00
|
|
|
#include "DHTTaskQueue.h"
|
|
|
|
#include "DHTTaskFactory.h"
|
|
|
|
#include "DHTNode.h"
|
|
|
|
#include "DHTRegistry.h"
|
2010-08-07 14:15:21 +00:00
|
|
|
#include "DHTPeerAnnounceStorage.h"
|
|
|
|
#include "DHTTokenTracker.h"
|
|
|
|
#include "DHTMessageDispatcher.h"
|
|
|
|
#include "DHTMessageReceiver.h"
|
|
|
|
#include "DHTMessageFactory.h"
|
|
|
|
#include "DHTMessageCallback.h"
|
2008-05-11 01:22:32 +00:00
|
|
|
#include "PieceStorage.h"
|
2008-06-16 15:11:41 +00:00
|
|
|
#include "RequestGroup.h"
|
2008-11-03 06:49:02 +00:00
|
|
|
#include "DefaultExtensionMessageFactory.h"
|
2008-12-10 14:14:11 +00:00
|
|
|
#include "RequestGroupMan.h"
|
2009-04-25 10:48:00 +00:00
|
|
|
#include "ExtensionMessageRegistry.h"
|
2009-06-28 10:37:15 +00:00
|
|
|
#include "bittorrent_helper.h"
|
2009-11-23 13:17:48 +00:00
|
|
|
#include "UTMetadataRequestFactory.h"
|
|
|
|
#include "UTMetadataRequestTracker.h"
|
2011-07-21 14:42:41 +00:00
|
|
|
#include "BtRegistry.h"
|
2006-03-21 14:12:51 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
|
|
|
|
2008-11-03 06:49:02 +00:00
|
|
|
PeerInteractionCommand::PeerInteractionCommand
|
2010-03-20 14:30:36 +00:00
|
|
|
(cuid_t cuid,
|
2008-11-03 06:49:02 +00:00
|
|
|
RequestGroup* requestGroup,
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::shared_ptr<Peer>& p,
|
2008-11-03 06:49:02 +00:00
|
|
|
DownloadEngine* e,
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::shared_ptr<BtRuntime>& btRuntime,
|
|
|
|
const std::shared_ptr<PieceStorage>& pieceStorage,
|
|
|
|
const std::shared_ptr<PeerStorage>& peerStorage,
|
|
|
|
const std::shared_ptr<SocketCore>& s,
|
2008-11-03 06:49:02 +00:00
|
|
|
Seq sequence,
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::shared_ptr<PeerConnection>& passedPeerConnection)
|
2007-10-11 16:58:24 +00:00
|
|
|
:PeerAbstractCommand(cuid, p, e, s),
|
2010-06-21 13:51:56 +00:00
|
|
|
requestGroup_(requestGroup),
|
|
|
|
btRuntime_(btRuntime),
|
|
|
|
pieceStorage_(pieceStorage),
|
|
|
|
peerStorage_(peerStorage),
|
|
|
|
sequence_(sequence)
|
2006-12-24 06:25:21 +00:00
|
|
|
{
|
|
|
|
// TODO move following bunch of processing to separate method, like init()
|
2010-06-21 13:51:56 +00:00
|
|
|
if(sequence_ == INITIATOR_SEND_HANDSHAKE) {
|
2006-07-19 17:07:45 +00:00
|
|
|
disableReadCheckSocket();
|
2010-06-09 14:02:09 +00:00
|
|
|
setWriteCheckSocket(getSocket());
|
2009-05-08 16:41:21 +00:00
|
|
|
setTimeout(getOption()->getAsInt(PREF_PEER_CONNECTION_TIMEOUT));
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
2010-08-07 14:15:21 +00:00
|
|
|
|
|
|
|
int family;
|
|
|
|
unsigned char compact[COMPACT_LEN_IPV6];
|
|
|
|
int compactlen = bittorrent::packcompact
|
|
|
|
(compact, getPeer()->getIPAddress(), getPeer()->getPort());
|
|
|
|
if(compactlen == COMPACT_LEN_IPV6) {
|
|
|
|
family = AF_INET6;
|
|
|
|
} else {
|
|
|
|
family = AF_INET;
|
|
|
|
}
|
2012-10-01 14:52:22 +00:00
|
|
|
|
2013-06-22 15:59:55 +00:00
|
|
|
auto torrentAttrs =
|
2010-06-21 13:51:56 +00:00
|
|
|
bittorrent::getTorrentAttrs(requestGroup_->getDownloadContext());
|
2010-06-18 14:47:09 +00:00
|
|
|
bool metadataGetMode = torrentAttrs->metadata.empty();
|
2009-11-23 13:17:48 +00:00
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<ExtensionMessageRegistry> exMsgRegistry
|
2009-04-25 10:48:00 +00:00
|
|
|
(new ExtensionMessageRegistry());
|
2012-09-26 13:02:48 +00:00
|
|
|
exMsgRegistry->setExtensionMessageID(ExtensionMessageRegistry::UT_PEX, 8);
|
|
|
|
// http://www.bittorrent.org/beps/bep_0009.html
|
|
|
|
exMsgRegistry->setExtensionMessageID(ExtensionMessageRegistry::UT_METADATA,
|
|
|
|
9);
|
2009-04-25 10:48:00 +00:00
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<UTMetadataRequestFactory> utMetadataRequestFactory;
|
|
|
|
std::shared_ptr<UTMetadataRequestTracker> utMetadataRequestTracker;
|
2009-11-23 13:17:48 +00:00
|
|
|
if(metadataGetMode) {
|
|
|
|
utMetadataRequestFactory.reset(new UTMetadataRequestFactory());
|
|
|
|
utMetadataRequestTracker.reset(new UTMetadataRequestTracker());
|
|
|
|
}
|
|
|
|
|
2013-07-01 12:42:51 +00:00
|
|
|
auto extensionMessageFactory =
|
|
|
|
make_unique<DefaultExtensionMessageFactory>(getPeer(),
|
|
|
|
exMsgRegistry.get());
|
|
|
|
extensionMessageFactory->setPeerStorage(peerStorage.get());
|
|
|
|
extensionMessageFactory->setDownloadContext
|
|
|
|
(requestGroup_->getDownloadContext().get());
|
|
|
|
extensionMessageFactory->setUTMetadataRequestTracker
|
2010-11-12 12:48:48 +00:00
|
|
|
(utMetadataRequestTracker.get());
|
2009-11-23 13:17:48 +00:00
|
|
|
// PieceStorage will be set later.
|
2012-09-28 13:00:46 +00:00
|
|
|
|
|
|
|
DefaultBtMessageFactory* factoryPtr(new DefaultBtMessageFactory());
|
|
|
|
factoryPtr->setCuid(cuid);
|
2013-06-22 15:01:40 +00:00
|
|
|
factoryPtr->setDownloadContext(requestGroup_->getDownloadContext().get());
|
|
|
|
factoryPtr->setPieceStorage(pieceStorage.get());
|
|
|
|
factoryPtr->setPeerStorage(peerStorage.get());
|
2013-07-01 12:42:51 +00:00
|
|
|
factoryPtr->setExtensionMessageFactory(extensionMessageFactory.get());
|
2012-09-28 13:00:46 +00:00
|
|
|
factoryPtr->setPeer(getPeer());
|
2010-08-07 14:15:21 +00:00
|
|
|
if(family == AF_INET) {
|
2012-09-28 13:00:46 +00:00
|
|
|
factoryPtr->setLocalNode(DHTRegistry::getData().localNode.get());
|
|
|
|
factoryPtr->setRoutingTable(DHTRegistry::getData().routingTable.get());
|
|
|
|
factoryPtr->setTaskQueue(DHTRegistry::getData().taskQueue.get());
|
|
|
|
factoryPtr->setTaskFactory(DHTRegistry::getData().taskFactory.get());
|
2010-08-07 14:15:21 +00:00
|
|
|
} else {
|
2012-09-28 13:00:46 +00:00
|
|
|
factoryPtr->setLocalNode(DHTRegistry::getData6().localNode.get());
|
|
|
|
factoryPtr->setRoutingTable(DHTRegistry::getData6().routingTable.get());
|
|
|
|
factoryPtr->setTaskQueue(DHTRegistry::getData6().taskQueue.get());
|
|
|
|
factoryPtr->setTaskFactory(DHTRegistry::getData6().taskFactory.get());
|
2010-08-07 14:15:21 +00:00
|
|
|
}
|
2009-11-23 13:17:48 +00:00
|
|
|
if(metadataGetMode) {
|
2012-09-28 13:00:46 +00:00
|
|
|
factoryPtr->enableMetadataGetMode();
|
2009-11-23 13:17:48 +00:00
|
|
|
}
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<BtMessageFactory> factory(factoryPtr);
|
2012-09-28 13:00:46 +00:00
|
|
|
|
2007-01-16 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To decrease CPU usage in bittorrent download, calculation
results in
BitfieldMan were cached and realtime fetching PeerObject was
removed
with WeakHandle introduced. Option values are set to the objects
by setter before download begins.
* src/DefaultBtRequestFactory.cc: Use messageFactory member.
* src/DefaultBtRequestFactory.h
(dispatcher): BtMessageDispatcherHandle ->
BtMessageDispatcherWeakHandle.
(messageFactory): New variable.
(setBtMessageDispatcher): BtMessageDispatcherHandle ->
BtMessageDispatcherWeakHandle.
(setBtMessageFactory): New function.
* src/DefaultBtMessageDispatcher.cc:
(sendMessages): Use maxUploadSpeedLimit instead of fetching the
value
from Option.
(checkRequestSlotAndDoNecessaryThing): Use requestTimeout
instead of
feating the value from Option.
Use messageFactory member.
* src/PeerInteractionCommand.cc
(PeerInteractionCommand): Added maxDownloadSpeedLimit.
Add reverse dependencies to factory object.
Set maxUploadSpeedLimit and requestTimeout and messageFactory to
dispatcher.
Set messageFactory to receiver.
Set keepAliveInterval and maxDownloadSpeedLimit and
messageFactory to
btInteractive.
Set receiver to peerObject.
Set maxDownloadSpeedLimit to this.
(executeInternal): Use maxDownloadSpeedLimit member.
* src/BtChokeMessage.cc
(doReceivedAction): Use dispatcher, requestFactory member.
(onSendComplete): Use dispatcher member.
* src/PeerInteractionCommand.h
(maxDownloadSpeedLimit): New variable.
* src/DefaultBtMessageReceiver.h
(peerConnection): PeerConnectionHandle ->
PeerConnectionWeakHandle
(dispatcher):
BtMessageDispatcherHandle -> BtMessageDispatcherWeakHandle
(messageFactory): New variable.
(setPeerConnection): PeerConnectionHandle ->
PeerConnectionWeakHandle
(getPeerConnection): PeerConnectionHandle ->
PeerConnectionWeakHandle
(setDispatcher):
BtMessageDispatcherHandle -> BtMessageDispatcherWeakHandle
(setBtMessageFactory): New function.
* src/DefaultBtInteractive.cc
(initiateHandshake): Use messageFactory member.
(addBitfieldMessageToQueue): Use messageFactory member.
(addAllowedFastMessageToQueue): Use messageFactory member.
(decideChoking): Use messageFactory member.
(checkHave): Use messageFactory member.
(sendKeepAlive): Use keepAliveInterval, messageFactory member.
(receiveMessages): Use maxDownloadSpeedLimit member.
(decideInterest): Use messageFactory member.
* src/BtRequestMessage.cc
(doReceivedAction): Use messageFactory, dispatcher member.
(onQueued): Use dispatcher member.
* src/BtPieceMessage.cc
(doReceivedAction): Use dispatcher member.
(send): Use peerConnection member.
(onWrongPiece): Use requestFactory member.
(handleChokingEvent): Use messageFactory, dispatcher member.
(handleCancelSendingPieceEvent): Use messageFactory, dispatcher
member.
* src/BtMessageDispatcher.h
(BtMessageDispatcherWeakHandle): New type definition.
* src/SimpleBtMessage.cc
(send): Use peerConnection member.
* src/BtRejectMessage.cc
(doReceivedAction): Use dispatcher member.
* src/DefaultBtMessageDispatcher.h
(Option.h): Removed include.
(messageFactory): New variable.
(option): Removed.
(maxUploadSpeedLimit): New variable.
(requestTimeout): New variable.
(DefaultBtMessageDispatcher): Removed option.
Added maxUploadSpeedLimit, requestTimeout.
(setOption): Removed.
(getOption): Removed.
(setMaxUploadSpeedLimit): New function.
(setRequestTimeout): New function.
(setBtMessageFactory): New function.
* src/DefaultBtInteractive.h
(btMessageReceiver):
BtMessageReceiverHandle -> BtMessageReceiverWeakHandle
(dispatcher):
BtMessageDispatcherHandle -> BtMessageReceiverWeakHandle
(btRequestFactory):
BtRequestFactoryHandle -> BtRequestFactoryWeakHandle
(peerConnection):
PeerConnectionHandle -> PeerConnectionWeakHandle
(messageFactory): New variable.
(option): Removed.
(keepAliveInterval): New variable.
(maxDownloadSpeedLimit): New variable.
(DefaultBtInteractive): Added keepAliveInterval,
maxDownloadSpeedLimit.
(setBtMessageReceiver):
BtMessageReceiverHandle -> BtMessageReceiverWeakHandle
(setDispatcher):
BtMessageDispatcherHandle -> BtMessageReceiverWeakHandle
(setBtRequestFactory):
BtRequestFactoryHandle -> BtRequestFactoryWeakHandle
(setPeerConnection):
PeerConnectionHandle -> PeerConnectionWeakHandle
(setOption): Removed.
(setKeepAliveInterval): New function.
(setMaxDownloadSpeedLimit): New function.
(setBtMessageFactory): New function.
* src/BitfieldMan.h
(cachedNumMissingBlock): New variable.
(cachedNumFilteredBlock): New variable.
(cachedCompletedLength): New variable.
(cachedFilteredComletedLength): New variable.
(cachedFilteredTotalLength): New variable.
(countMissingBlockNow): New function.
(countFilteredBlockNow): New function.
(getFilteredTotalLengthNow): New function.
(getCompletedLengthNow): New function.
(getFilteredCompletedLengthNow): New function.
(updateCache): New function.
* src/AbstractBtMessage.h
(BtMessageDispatcher.h): New include.
(PeerConnection.h): New include.
(BtRequestFactory.h): New include
(BtMessageFactory.h): New include.
(dispatcher): New variable.
(messageFactory): New variable.
(peerConnection: New variable.
(setBtMessageDispatcher): New function.
(setPeerConnection): New function.
(setBtMessageFactory): New function.
(setBtRequestFactory): New function.
* src/DefaultBtMessageFactory.cc
(setCommonProperty): Set dispatcher, requestFactory, this,
peerConnection to msg.
* src/BtRegistry.h
(BT_MESSAGE_RECEIVER): New macro.
* src/PeerConnection.h
(PeerConnectionWeakHandle): New type definition.
* src/BtMessageFactory.h
(BtMessageFactoryWeakHandle): New type definition.
* src/BitfieldMan.cc
(BitfieldMan): Added cachedNumMissingBlock,
cachedNumFilteredBlock,
cachedCompletedLength, cachedFilteredComletedLength,
cachedFilteredTotalLength.
Call updateCache().
(countMissingBlock): Return cachedNumMissingBlock.
(countMissingBlockNow): New function.
(countBlock): Return cachedNumFilteredBlock if filterEnabled is
true.
(countFilteredBlockNow): New function.
(setBit): Call updateCache().
(unsetBit): Call updateCache().
(setBitfield): Call updateCache().
(clearAllBit): Call updateCache().
(setAllBit): Use setBitInternal instead of setBit.
Call updateCache().
(addFilter): Call updateCache().
(enableFilter): Call updateCache().
(disableFilter): Call updateCache().
(clearFilter): Call updateCache().
(getFilteredTotalLength): Return cachedFilteredTotalLength.
(getFilteredTotalLengthNow): New function.
(getCompletedLength): Return cachedCompletedLength.
(getCompletedLengthNow): New function.
(getFilteredCompletedLength): Return
cachedFilteredComletedLength.
(getFilteredCompletedLengthNow): New function.
(updateCache): New function.
* src/BtMessageReceiver.h
(BtMessageReceiverWeakHandle): New type definition.
* src/DefaultBtMessageReceiver.cc
(receiveHandshake): Use messageFactory member.
(sendHandshake): Use messageFactory member.
(receiveMessage): Use messageFactory member.
* src/DefaultBtMessageFactory.h
(dispatcher): New variable.
(requestFactory): New variable.
(peerConnection): New variablle.
(setBtMessageDispatcher): New function.
(setBtRequestFactory): New function.
(setPeerConnection): New function.
* src/SharedHandle.h
(RefCount): New class.
(WeakHandle): New class.
* src/PeerObject.h
(BtMessageReceiver.h): New include.
(PeerObject): Added btMessageReceiver.
(btMessageReceiver): New variable.
* src/Util.cc
(countBit): Simplified.
* src/BtCancelMessage.cc
(doReceivedAction): Use dispatcher member.
* src/BtRequestFactory.h
(BtRequestFactoryWeakHandle): New type definition.
* src/PeerStorage.h
(downloadSpeed): int -> uint32_t
(uploadSpeed): int -> uint32_t
(sessionDownloadLength): long long int -> uint64_t
(sessionUploadLength): long long int -> uint64_t
2007-01-16 15:20:26 +00:00
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<PeerConnection> peerConnection;
|
2010-11-12 12:48:48 +00:00
|
|
|
if(!passedPeerConnection) {
|
2010-08-04 15:00:38 +00:00
|
|
|
peerConnection.reset(new PeerConnection(cuid, getPeer(), getSocket()));
|
2008-04-20 00:50:22 +00:00
|
|
|
} else {
|
|
|
|
peerConnection = passedPeerConnection;
|
2011-01-08 08:32:16 +00:00
|
|
|
if(sequence_ == RECEIVER_WAIT_HANDSHAKE &&
|
|
|
|
peerConnection->getBufferLength() > 0) {
|
|
|
|
setStatus(Command::STATUS_ONESHOT_REALTIME);
|
|
|
|
getDownloadEngine()->setNoWait(true);
|
|
|
|
}
|
2008-04-20 00:50:22 +00:00
|
|
|
}
|
2012-02-08 16:51:42 +00:00
|
|
|
// If the number of pieces gets bigger, the length of Bitfield
|
|
|
|
// message payload exceeds the initial buffer capacity of
|
|
|
|
// PeerConnection, which is MAX_PAYLOAD_LEN. We expand buffer as
|
|
|
|
// necessary so that PeerConnection can receive the Bitfield
|
|
|
|
// message.
|
|
|
|
size_t bitfieldPayloadSize =
|
|
|
|
1+(requestGroup_->getDownloadContext()->getNumPieces()+7)/8;
|
|
|
|
peerConnection->reserveBuffer(bitfieldPayloadSize);
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2012-09-28 13:00:46 +00:00
|
|
|
DefaultBtMessageDispatcher* dispatcherPtr(new DefaultBtMessageDispatcher());
|
|
|
|
dispatcherPtr->setCuid(cuid);
|
|
|
|
dispatcherPtr->setPeer(getPeer());
|
2013-06-23 03:25:58 +00:00
|
|
|
dispatcherPtr->setDownloadContext(requestGroup_->getDownloadContext().get());
|
|
|
|
dispatcherPtr->setPieceStorage(pieceStorage.get());
|
|
|
|
dispatcherPtr->setPeerStorage(peerStorage.get());
|
2012-09-28 13:00:46 +00:00
|
|
|
dispatcherPtr->setRequestTimeout(getOption()->
|
|
|
|
getAsInt(PREF_BT_REQUEST_TIMEOUT));
|
|
|
|
dispatcherPtr->setBtMessageFactory(factoryPtr);
|
|
|
|
dispatcherPtr->setRequestGroupMan
|
2010-11-12 12:48:48 +00:00
|
|
|
(getDownloadEngine()->getRequestGroupMan().get());
|
2013-06-23 03:25:58 +00:00
|
|
|
dispatcherPtr->setPeerConnection(peerConnection.get());
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<BtMessageDispatcher> dispatcher(dispatcherPtr);
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2012-09-28 13:00:46 +00:00
|
|
|
DefaultBtMessageReceiver* receiverPtr(new DefaultBtMessageReceiver());
|
2013-06-23 03:29:18 +00:00
|
|
|
std::shared_ptr<BtMessageReceiver> receiver(receiverPtr);
|
|
|
|
receiverPtr->setDownloadContext(requestGroup_->getDownloadContext().get());
|
2012-09-28 13:00:46 +00:00
|
|
|
receiverPtr->setPeerConnection(peerConnection.get());
|
|
|
|
receiverPtr->setDispatcher(dispatcherPtr);
|
|
|
|
receiverPtr->setBtMessageFactory(factoryPtr);
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2012-09-28 13:00:46 +00:00
|
|
|
DefaultBtRequestFactory* reqFactoryPtr(new DefaultBtRequestFactory());
|
|
|
|
reqFactoryPtr->setPeer(getPeer());
|
2013-06-23 03:02:36 +00:00
|
|
|
reqFactoryPtr->setPieceStorage(pieceStorage.get());
|
2012-09-28 13:00:46 +00:00
|
|
|
reqFactoryPtr->setBtMessageDispatcher(dispatcherPtr);
|
|
|
|
reqFactoryPtr->setBtMessageFactory(factoryPtr);
|
|
|
|
reqFactoryPtr->setCuid(cuid);
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<BtRequestFactory> reqFactory(reqFactoryPtr);
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2013-07-01 12:42:51 +00:00
|
|
|
// reverse depends
|
|
|
|
factoryPtr->setBtMessageDispatcher(dispatcherPtr);
|
|
|
|
factoryPtr->setBtRequestFactory(reqFactoryPtr);
|
|
|
|
factoryPtr->setPeerConnection(peerConnection.get());
|
|
|
|
|
|
|
|
extensionMessageFactory->setBtMessageDispatcher(dispatcherPtr);
|
|
|
|
extensionMessageFactory->setBtMessageFactory(factoryPtr);
|
|
|
|
|
|
|
|
if(metadataGetMode) {
|
|
|
|
utMetadataRequestFactory->setCuid(cuid);
|
|
|
|
utMetadataRequestFactory->setDownloadContext
|
|
|
|
(requestGroup_->getDownloadContext().get());
|
|
|
|
utMetadataRequestFactory->setBtMessageDispatcher(dispatcherPtr);
|
|
|
|
utMetadataRequestFactory->setBtMessageFactory(factoryPtr);
|
|
|
|
utMetadataRequestFactory->setPeer(getPeer());
|
|
|
|
utMetadataRequestFactory->setUTMetadataRequestTracker
|
|
|
|
(utMetadataRequestTracker.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
getPeer()->allocateSessionResource
|
|
|
|
(requestGroup_->getDownloadContext()->getPieceLength(),
|
|
|
|
requestGroup_->getDownloadContext()->getTotalLength());
|
|
|
|
getPeer()->setBtMessageDispatcher(dispatcherPtr);
|
|
|
|
|
2012-09-28 13:00:46 +00:00
|
|
|
DefaultBtInteractive* btInteractivePtr
|
2010-06-21 13:51:56 +00:00
|
|
|
(new DefaultBtInteractive(requestGroup_->getDownloadContext(), getPeer()));
|
2012-09-28 13:00:46 +00:00
|
|
|
btInteractivePtr->setBtRuntime(btRuntime_);
|
|
|
|
btInteractivePtr->setPieceStorage(pieceStorage_);
|
|
|
|
btInteractivePtr->setPeerStorage(peerStorage); // Note: Not a member variable.
|
|
|
|
btInteractivePtr->setCuid(cuid);
|
|
|
|
btInteractivePtr->setBtMessageReceiver(receiver);
|
|
|
|
btInteractivePtr->setDispatcher(dispatcher);
|
|
|
|
btInteractivePtr->setBtRequestFactory(reqFactory);
|
|
|
|
btInteractivePtr->setPeerConnection(peerConnection);
|
2013-07-01 12:42:51 +00:00
|
|
|
btInteractivePtr->setExtensionMessageFactory
|
|
|
|
(std::move(extensionMessageFactory));
|
2012-09-28 13:00:46 +00:00
|
|
|
btInteractivePtr->setExtensionMessageRegistry(exMsgRegistry);
|
|
|
|
btInteractivePtr->setKeepAliveInterval
|
2009-05-08 16:41:21 +00:00
|
|
|
(getOption()->getAsInt(PREF_BT_KEEP_ALIVE_INTERVAL));
|
2012-09-28 13:00:46 +00:00
|
|
|
btInteractivePtr->setRequestGroupMan
|
2010-11-12 12:48:48 +00:00
|
|
|
(getDownloadEngine()->getRequestGroupMan().get());
|
2012-09-28 13:00:46 +00:00
|
|
|
btInteractivePtr->setBtMessageFactory(factory);
|
2010-06-18 14:47:09 +00:00
|
|
|
if((metadataGetMode || !torrentAttrs->privateTorrent) &&
|
2010-06-09 14:02:09 +00:00
|
|
|
!getPeer()->isLocalPeer()) {
|
2009-05-08 16:41:21 +00:00
|
|
|
if(getOption()->getAsBool(PREF_ENABLE_PEER_EXCHANGE)) {
|
2012-09-28 13:00:46 +00:00
|
|
|
btInteractivePtr->setUTPexEnabled(true);
|
2008-02-01 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Added DHT functionality, compatible with mainline.
DHT is disabled by default. To enable it, give --enable-dht to
aria2c.
You may need to specify entry point to DHT network using
--dht-entry-point. DHT uses UDP port to listen incoming message.
Use --dht-listen-port to specify port number. Make sure that
your
firewall configuration can pass through UDP traffic to the port.
The routing table is saved in $HOME/.aria2/dht.dat.
* src/DHT*
* src/BNode.{h, cc}
* src/PeerInteractionCommand.cc: enable DHT functionality for a
particular torrent.
* src/Data.cc: Rewritten ctor.
* src/OptionHandlerFactory.cc: Added --enable-dht,
--dht-listen-port,
--dht-entry-point.
* src/DefaultBtInteractive.cc: Send port message if dht is
enabled.
* src/RequestGroup.cc: Initialize DHT functionality. When
download
ends, remove BtContext from DHTPeerAnnounceStorage.
* src/BtPortMessage.{h, cc}: Rewritten.
* src/message.h
* src/OptionHandlerImpl.cc
* src/option_processing.cc: Added --enable-dht,
--dht-listen-port,
--dht-entry-point.
* src/Dictionary.{h, cc} (remove): New function.
* src/prefs.h
* src/DefaultBtMessageFactory.h
* src/BtHandshakeMessage.cc
* src/ActivePeerConnectionCommand.cc
* src/SocketCore.{h, cc}: Added datagram socket support.
* src/DefaultBtMessageFactory.cc
* src/BtSetup.cc: Add BtContext to DHTPeerAnnounceStorage here.
Create DHT commands.
* src/BtMessageFactory.h
* src/PeerMessageUtil.{h, cc}
2008-02-01 17:36:33 +00:00
|
|
|
}
|
2010-08-07 14:15:21 +00:00
|
|
|
if(family == AF_INET) {
|
|
|
|
if(DHTRegistry::isInitialized()) {
|
2012-09-28 13:00:46 +00:00
|
|
|
btInteractivePtr->setDHTEnabled(true);
|
|
|
|
factoryPtr->setDHTEnabled(true);
|
|
|
|
btInteractivePtr->setLocalNode(DHTRegistry::getData().localNode.get());
|
2010-08-07 14:15:21 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(DHTRegistry::isInitialized6()) {
|
2012-09-28 13:00:46 +00:00
|
|
|
btInteractivePtr->setDHTEnabled(true);
|
|
|
|
factoryPtr->setDHTEnabled(true);
|
|
|
|
btInteractivePtr->setLocalNode(DHTRegistry::getData6().localNode.get());
|
2010-08-07 14:15:21 +00:00
|
|
|
}
|
2008-02-01 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Added DHT functionality, compatible with mainline.
DHT is disabled by default. To enable it, give --enable-dht to
aria2c.
You may need to specify entry point to DHT network using
--dht-entry-point. DHT uses UDP port to listen incoming message.
Use --dht-listen-port to specify port number. Make sure that
your
firewall configuration can pass through UDP traffic to the port.
The routing table is saved in $HOME/.aria2/dht.dat.
* src/DHT*
* src/BNode.{h, cc}
* src/PeerInteractionCommand.cc: enable DHT functionality for a
particular torrent.
* src/Data.cc: Rewritten ctor.
* src/OptionHandlerFactory.cc: Added --enable-dht,
--dht-listen-port,
--dht-entry-point.
* src/DefaultBtInteractive.cc: Send port message if dht is
enabled.
* src/RequestGroup.cc: Initialize DHT functionality. When
download
ends, remove BtContext from DHTPeerAnnounceStorage.
* src/BtPortMessage.{h, cc}: Rewritten.
* src/message.h
* src/OptionHandlerImpl.cc
* src/option_processing.cc: Added --enable-dht,
--dht-listen-port,
--dht-entry-point.
* src/Dictionary.{h, cc} (remove): New function.
* src/prefs.h
* src/DefaultBtMessageFactory.h
* src/BtHandshakeMessage.cc
* src/ActivePeerConnectionCommand.cc
* src/SocketCore.{h, cc}: Added datagram socket support.
* src/DefaultBtMessageFactory.cc
* src/BtSetup.cc: Add BtContext to DHTPeerAnnounceStorage here.
Create DHT commands.
* src/BtMessageFactory.h
* src/PeerMessageUtil.{h, cc}
2008-02-01 17:36:33 +00:00
|
|
|
}
|
2007-12-22 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Added uTorrent compatible Peer Exchange.
* src/BencodeVisitor.{h, cc}
* test/BencodeVisitorTest.cc
* src/BtConstants.h
* src/BtContext.h: Added 'private' flag.
* src/BtExtendedMessage.{h, cc}
* test/BtExtendedMessageTest.cc
* src/BtHandshakeMessage.{h, cc}: Set extended messaging bit in
reserved field.
* test/BtHandshakeMessageTest.cc
* src/BtMessageFactory.h
* src/BtRegistry.h
* src/BtRuntime.h: This class holds default extension message
IDs for
aria2. By default, aria2 uses ID 8 for ut_pex.
* src/DefaultBtContext.cc
* src/DefaultBtInteractive.{h, cc}: This class holds
_utPexEnabled.
When it is true, aria2 enables ut_pex. This value is set by
PeerInteractionCommand.
* src/DefaultBtMessageFactory.{h, cc}
* test/DefaultBtMessageFactoryTest.cc
* src/DefaultBtMessageReceiver.cc: Moved the code of fast
extension
handling to DefaultBtInteractive class.
* src/DefaultExtensionMessageFactory.{h, cc}
* test/DefaultExtensionMessageFactoryTest.cc
* src/DefaultPeerStorage.cc: Returns false if a peer is already
in
the container(peers and incomingPeers. The equality is
determined by
Peer::id).
* test/DefaultPeerStorageTest.cc
* src/ExtensionMessage.h
* test/MockExtensionMessage.h
* src/ExtensionMessageFactory.h
* test/MockExtensionMessageFactory.h
* src/HandshakeExtensionMessage.{h, cc}
* test/HandshakeExtensionMessageTest.cc
* src/MetaEntry.h
* src/Peer.{h, cc}
* src/PeerInteractionCommand.cc
* src/PeerReceiveHandshakeCommand.cc: Evaluate the return value
of
addIncomingPeer.
* src/PeerMessageUtil.{h, cc}
* src/PeerObject.h
* src/UTPexExtensionMessage.{h, cc}
* test/UTPexExtensionMessageTest.cc
* src/message.h
* src/prefs.h
Fixed the bug that returns incomplete data when it contains null
character. A convenient constructor was also added.
* src/Data.{h, cc}
Rewritten.
* src/CompactPeerListProcessor.cc
Fixed typos.
* src/message.h
* src/MetaFileUtil.cc
2007-12-22 03:57:55 +00:00
|
|
|
}
|
2012-09-28 13:00:46 +00:00
|
|
|
btInteractivePtr->setUTMetadataRequestFactory(utMetadataRequestFactory);
|
|
|
|
btInteractivePtr->setUTMetadataRequestTracker(utMetadataRequestTracker);
|
|
|
|
btInteractivePtr->setTcpPort(e->getBtRegistry()->getTcpPort());
|
2009-11-23 13:17:48 +00:00
|
|
|
if(metadataGetMode) {
|
2012-09-28 13:00:46 +00:00
|
|
|
btInteractivePtr->enableMetadataGetMode();
|
2009-11-23 13:17:48 +00:00
|
|
|
}
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<BtInteractive> btInteractive(btInteractivePtr);
|
2009-11-23 13:17:48 +00:00
|
|
|
|
2010-06-21 13:51:56 +00:00
|
|
|
btInteractive_ = btInteractive;
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2007-01-16 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To decrease CPU usage in bittorrent download, calculation
results in
BitfieldMan were cached and realtime fetching PeerObject was
removed
with WeakHandle introduced. Option values are set to the objects
by setter before download begins.
* src/DefaultBtRequestFactory.cc: Use messageFactory member.
* src/DefaultBtRequestFactory.h
(dispatcher): BtMessageDispatcherHandle ->
BtMessageDispatcherWeakHandle.
(messageFactory): New variable.
(setBtMessageDispatcher): BtMessageDispatcherHandle ->
BtMessageDispatcherWeakHandle.
(setBtMessageFactory): New function.
* src/DefaultBtMessageDispatcher.cc:
(sendMessages): Use maxUploadSpeedLimit instead of fetching the
value
from Option.
(checkRequestSlotAndDoNecessaryThing): Use requestTimeout
instead of
feating the value from Option.
Use messageFactory member.
* src/PeerInteractionCommand.cc
(PeerInteractionCommand): Added maxDownloadSpeedLimit.
Add reverse dependencies to factory object.
Set maxUploadSpeedLimit and requestTimeout and messageFactory to
dispatcher.
Set messageFactory to receiver.
Set keepAliveInterval and maxDownloadSpeedLimit and
messageFactory to
btInteractive.
Set receiver to peerObject.
Set maxDownloadSpeedLimit to this.
(executeInternal): Use maxDownloadSpeedLimit member.
* src/BtChokeMessage.cc
(doReceivedAction): Use dispatcher, requestFactory member.
(onSendComplete): Use dispatcher member.
* src/PeerInteractionCommand.h
(maxDownloadSpeedLimit): New variable.
* src/DefaultBtMessageReceiver.h
(peerConnection): PeerConnectionHandle ->
PeerConnectionWeakHandle
(dispatcher):
BtMessageDispatcherHandle -> BtMessageDispatcherWeakHandle
(messageFactory): New variable.
(setPeerConnection): PeerConnectionHandle ->
PeerConnectionWeakHandle
(getPeerConnection): PeerConnectionHandle ->
PeerConnectionWeakHandle
(setDispatcher):
BtMessageDispatcherHandle -> BtMessageDispatcherWeakHandle
(setBtMessageFactory): New function.
* src/DefaultBtInteractive.cc
(initiateHandshake): Use messageFactory member.
(addBitfieldMessageToQueue): Use messageFactory member.
(addAllowedFastMessageToQueue): Use messageFactory member.
(decideChoking): Use messageFactory member.
(checkHave): Use messageFactory member.
(sendKeepAlive): Use keepAliveInterval, messageFactory member.
(receiveMessages): Use maxDownloadSpeedLimit member.
(decideInterest): Use messageFactory member.
* src/BtRequestMessage.cc
(doReceivedAction): Use messageFactory, dispatcher member.
(onQueued): Use dispatcher member.
* src/BtPieceMessage.cc
(doReceivedAction): Use dispatcher member.
(send): Use peerConnection member.
(onWrongPiece): Use requestFactory member.
(handleChokingEvent): Use messageFactory, dispatcher member.
(handleCancelSendingPieceEvent): Use messageFactory, dispatcher
member.
* src/BtMessageDispatcher.h
(BtMessageDispatcherWeakHandle): New type definition.
* src/SimpleBtMessage.cc
(send): Use peerConnection member.
* src/BtRejectMessage.cc
(doReceivedAction): Use dispatcher member.
* src/DefaultBtMessageDispatcher.h
(Option.h): Removed include.
(messageFactory): New variable.
(option): Removed.
(maxUploadSpeedLimit): New variable.
(requestTimeout): New variable.
(DefaultBtMessageDispatcher): Removed option.
Added maxUploadSpeedLimit, requestTimeout.
(setOption): Removed.
(getOption): Removed.
(setMaxUploadSpeedLimit): New function.
(setRequestTimeout): New function.
(setBtMessageFactory): New function.
* src/DefaultBtInteractive.h
(btMessageReceiver):
BtMessageReceiverHandle -> BtMessageReceiverWeakHandle
(dispatcher):
BtMessageDispatcherHandle -> BtMessageReceiverWeakHandle
(btRequestFactory):
BtRequestFactoryHandle -> BtRequestFactoryWeakHandle
(peerConnection):
PeerConnectionHandle -> PeerConnectionWeakHandle
(messageFactory): New variable.
(option): Removed.
(keepAliveInterval): New variable.
(maxDownloadSpeedLimit): New variable.
(DefaultBtInteractive): Added keepAliveInterval,
maxDownloadSpeedLimit.
(setBtMessageReceiver):
BtMessageReceiverHandle -> BtMessageReceiverWeakHandle
(setDispatcher):
BtMessageDispatcherHandle -> BtMessageReceiverWeakHandle
(setBtRequestFactory):
BtRequestFactoryHandle -> BtRequestFactoryWeakHandle
(setPeerConnection):
PeerConnectionHandle -> PeerConnectionWeakHandle
(setOption): Removed.
(setKeepAliveInterval): New function.
(setMaxDownloadSpeedLimit): New function.
(setBtMessageFactory): New function.
* src/BitfieldMan.h
(cachedNumMissingBlock): New variable.
(cachedNumFilteredBlock): New variable.
(cachedCompletedLength): New variable.
(cachedFilteredComletedLength): New variable.
(cachedFilteredTotalLength): New variable.
(countMissingBlockNow): New function.
(countFilteredBlockNow): New function.
(getFilteredTotalLengthNow): New function.
(getCompletedLengthNow): New function.
(getFilteredCompletedLengthNow): New function.
(updateCache): New function.
* src/AbstractBtMessage.h
(BtMessageDispatcher.h): New include.
(PeerConnection.h): New include.
(BtRequestFactory.h): New include
(BtMessageFactory.h): New include.
(dispatcher): New variable.
(messageFactory): New variable.
(peerConnection: New variable.
(setBtMessageDispatcher): New function.
(setPeerConnection): New function.
(setBtMessageFactory): New function.
(setBtRequestFactory): New function.
* src/DefaultBtMessageFactory.cc
(setCommonProperty): Set dispatcher, requestFactory, this,
peerConnection to msg.
* src/BtRegistry.h
(BT_MESSAGE_RECEIVER): New macro.
* src/PeerConnection.h
(PeerConnectionWeakHandle): New type definition.
* src/BtMessageFactory.h
(BtMessageFactoryWeakHandle): New type definition.
* src/BitfieldMan.cc
(BitfieldMan): Added cachedNumMissingBlock,
cachedNumFilteredBlock,
cachedCompletedLength, cachedFilteredComletedLength,
cachedFilteredTotalLength.
Call updateCache().
(countMissingBlock): Return cachedNumMissingBlock.
(countMissingBlockNow): New function.
(countBlock): Return cachedNumFilteredBlock if filterEnabled is
true.
(countFilteredBlockNow): New function.
(setBit): Call updateCache().
(unsetBit): Call updateCache().
(setBitfield): Call updateCache().
(clearAllBit): Call updateCache().
(setAllBit): Use setBitInternal instead of setBit.
Call updateCache().
(addFilter): Call updateCache().
(enableFilter): Call updateCache().
(disableFilter): Call updateCache().
(clearFilter): Call updateCache().
(getFilteredTotalLength): Return cachedFilteredTotalLength.
(getFilteredTotalLengthNow): New function.
(getCompletedLength): Return cachedCompletedLength.
(getCompletedLengthNow): New function.
(getFilteredCompletedLength): Return
cachedFilteredComletedLength.
(getFilteredCompletedLengthNow): New function.
(updateCache): New function.
* src/BtMessageReceiver.h
(BtMessageReceiverWeakHandle): New type definition.
* src/DefaultBtMessageReceiver.cc
(receiveHandshake): Use messageFactory member.
(sendHandshake): Use messageFactory member.
(receiveMessage): Use messageFactory member.
* src/DefaultBtMessageFactory.h
(dispatcher): New variable.
(requestFactory): New variable.
(peerConnection): New variablle.
(setBtMessageDispatcher): New function.
(setBtRequestFactory): New function.
(setPeerConnection): New function.
* src/SharedHandle.h
(RefCount): New class.
(WeakHandle): New class.
* src/PeerObject.h
(BtMessageReceiver.h): New include.
(PeerObject): Added btMessageReceiver.
(btMessageReceiver): New variable.
* src/Util.cc
(countBit): Simplified.
* src/BtCancelMessage.cc
(doReceivedAction): Use dispatcher member.
* src/BtRequestFactory.h
(BtRequestFactoryWeakHandle): New type definition.
* src/PeerStorage.h
(downloadSpeed): int -> uint32_t
(uploadSpeed): int -> uint32_t
(sessionDownloadLength): long long int -> uint64_t
(sessionUploadLength): long long int -> uint64_t
2007-01-16 15:20:26 +00:00
|
|
|
|
2010-06-21 13:51:56 +00:00
|
|
|
btRuntime_->increaseConnections();
|
|
|
|
requestGroup_->increaseNumCommand();
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PeerInteractionCommand::~PeerInteractionCommand() {
|
2010-06-09 14:02:09 +00:00
|
|
|
if(getPeer()->getCompletedLength() > 0) {
|
2010-06-21 13:51:56 +00:00
|
|
|
pieceStorage_->subtractPieceStats(getPeer()->getBitfield(),
|
2010-06-09 14:02:09 +00:00
|
|
|
getPeer()->getBitfieldLength());
|
2008-05-17 05:12:14 +00:00
|
|
|
}
|
2010-06-09 14:02:09 +00:00
|
|
|
getPeer()->releaseSessionResource();
|
2009-04-23 15:00:09 +00:00
|
|
|
|
2010-06-21 13:51:56 +00:00
|
|
|
requestGroup_->decreaseNumCommand();
|
|
|
|
btRuntime_->decreaseConnections();
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PeerInteractionCommand::executeInternal() {
|
2006-07-28 14:06:47 +00:00
|
|
|
setNoCheck(false);
|
2011-01-08 08:32:16 +00:00
|
|
|
bool done = false;
|
|
|
|
while(!done) {
|
|
|
|
switch(sequence_) {
|
|
|
|
case INITIATOR_SEND_HANDSHAKE:
|
|
|
|
if(!getSocket()->isWritable(0)) {
|
|
|
|
done = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
disableWriteCheckSocket();
|
|
|
|
setReadCheckSocket(getSocket());
|
|
|
|
//socket->setBlockingMode();
|
|
|
|
setTimeout(getOption()->getAsInt(PREF_BT_TIMEOUT));
|
|
|
|
btInteractive_->initiateHandshake();
|
|
|
|
sequence_ = INITIATOR_WAIT_HANDSHAKE;
|
2006-08-11 12:29:55 +00:00
|
|
|
break;
|
2011-01-08 08:32:16 +00:00
|
|
|
case INITIATOR_WAIT_HANDSHAKE: {
|
2010-06-21 13:51:56 +00:00
|
|
|
if(btInteractive_->countPendingMessage() > 0) {
|
2011-01-08 08:32:16 +00:00
|
|
|
btInteractive_->sendPendingMessage();
|
|
|
|
if(btInteractive_->countPendingMessage() > 0) {
|
|
|
|
done = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-07-04 13:35:41 +00:00
|
|
|
auto handshakeMessage = btInteractive_->receiveHandshake();
|
2011-01-08 08:32:16 +00:00
|
|
|
if(!handshakeMessage) {
|
|
|
|
done = true;
|
2010-01-05 16:01:46 +00:00
|
|
|
break;
|
2006-05-18 17:08:29 +00:00
|
|
|
}
|
2011-01-08 08:32:16 +00:00
|
|
|
btInteractive_->doPostHandshakeProcessing();
|
|
|
|
sequence_ = WIRED;
|
2006-03-21 14:12:51 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-01-08 08:32:16 +00:00
|
|
|
case RECEIVER_WAIT_HANDSHAKE: {
|
2013-07-04 13:35:41 +00:00
|
|
|
auto handshakeMessage = btInteractive_->receiveAndSendHandshake();
|
2011-01-08 08:32:16 +00:00
|
|
|
if(!handshakeMessage) {
|
|
|
|
done = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
btInteractive_->doPostHandshakeProcessing();
|
|
|
|
sequence_ = WIRED;
|
2006-03-21 14:12:51 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-01-08 08:32:16 +00:00
|
|
|
case WIRED:
|
|
|
|
// See the comment for writable check below.
|
|
|
|
disableWriteCheckSocket();
|
2008-06-16 16:06:24 +00:00
|
|
|
|
2011-01-08 08:32:16 +00:00
|
|
|
btInteractive_->doInteractionProcessing();
|
|
|
|
if(btInteractive_->countReceivedMessageInIteration() > 0) {
|
|
|
|
updateKeepAlive();
|
2008-11-13 15:39:22 +00:00
|
|
|
}
|
2011-01-08 08:32:16 +00:00
|
|
|
if((getPeer()->amInterested() && !getPeer()->peerChoking()) ||
|
|
|
|
btInteractive_->countOutstandingRequest() ||
|
|
|
|
(getPeer()->peerInterested() && !getPeer()->amChoking())) {
|
2008-06-16 16:06:24 +00:00
|
|
|
|
2011-01-08 08:32:16 +00:00
|
|
|
// Writable check to avoid slow seeding
|
|
|
|
if(btInteractive_->isSendingMessageInProgress()) {
|
|
|
|
setWriteCheckSocket(getSocket());
|
|
|
|
}
|
|
|
|
|
|
|
|
if(getDownloadEngine()->getRequestGroupMan()->
|
|
|
|
doesOverallDownloadSpeedExceed() ||
|
|
|
|
requestGroup_->doesDownloadSpeedExceed()) {
|
|
|
|
disableReadCheckSocket();
|
|
|
|
setNoCheck(true);
|
|
|
|
} else {
|
|
|
|
setReadCheckSocket(getSocket());
|
|
|
|
}
|
2008-02-08 18:27:28 +00:00
|
|
|
} else {
|
2011-01-08 08:32:16 +00:00
|
|
|
disableReadCheckSocket();
|
2008-02-08 18:27:28 +00:00
|
|
|
}
|
2011-01-08 08:32:16 +00:00
|
|
|
done = true;
|
|
|
|
break;
|
2008-02-08 18:27:28 +00:00
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
2010-06-21 13:51:56 +00:00
|
|
|
if(btInteractive_->countPendingMessage() > 0) {
|
2006-07-28 14:06:47 +00:00
|
|
|
setNoCheck(true);
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
2013-06-23 12:55:52 +00:00
|
|
|
addCommandSelf();
|
2006-12-24 06:25:21 +00:00
|
|
|
return false;
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO this method removed when PeerBalancerCommand is implemented
|
2008-03-09 12:24:01 +00:00
|
|
|
bool PeerInteractionCommand::prepareForNextPeer(time_t wait) {
|
2010-06-21 13:51:56 +00:00
|
|
|
if(peerStorage_->isPeerAvailable() && btRuntime_->lessThanEqMinPeers()) {
|
2013-01-26 08:50:06 +00:00
|
|
|
cuid_t ncuid = getDownloadEngine()->newCUID();
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<Peer> peer = peerStorage_->checkoutPeer(ncuid);
|
2013-01-26 08:50:06 +00:00
|
|
|
// sanity check
|
|
|
|
if(peer) {
|
2013-06-23 12:55:52 +00:00
|
|
|
auto command = make_unique<PeerInitiateConnectionCommand>
|
|
|
|
(ncuid, requestGroup_, peer, getDownloadEngine(), btRuntime_);
|
2013-01-26 08:50:06 +00:00
|
|
|
command->setPeerStorage(peerStorage_);
|
|
|
|
command->setPieceStorage(pieceStorage_);
|
2013-06-23 12:55:52 +00:00
|
|
|
getDownloadEngine()->addCommand(std::move(command));
|
2013-01-26 08:50:06 +00:00
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-04-27 02:22:14 +00:00
|
|
|
void PeerInteractionCommand::onAbort() {
|
2010-06-21 13:51:56 +00:00
|
|
|
btInteractive_->cancelAllPiece();
|
|
|
|
peerStorage_->returnPeer(getPeer());
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2010-11-28 16:37:32 +00:00
|
|
|
void PeerInteractionCommand::onFailure(const Exception& err)
|
2008-06-16 15:11:41 +00:00
|
|
|
{
|
2010-11-28 16:37:32 +00:00
|
|
|
requestGroup_->setLastErrorCode(err.getErrorCode());
|
2010-06-21 13:51:56 +00:00
|
|
|
requestGroup_->setHaltRequested(true);
|
2011-05-15 14:35:06 +00:00
|
|
|
getDownloadEngine()->setRefreshInterval(0);
|
2008-06-16 15:11:41 +00:00
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
bool PeerInteractionCommand::exitBeforeExecute()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return btRuntime_->isHalt();
|
2008-11-03 06:49:02 +00:00
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::shared_ptr<Option>& PeerInteractionCommand::getOption() const
|
2009-05-08 16:41:21 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return requestGroup_->getOption();
|
2009-05-08 16:41:21 +00:00
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|