class PeerState extends Object implements DataLoader
| Modifier and Type | Field and Description |
|---|---|
(package private) Set<Integer> | _expectedAllowedFastBEP 6: pieces a conforming sender may advertise, derived from our own destination hash;
null until computed, or when our destination cannot be determined. |
(package private) Set<Integer> | _peerAllowedFastBEP 6: pieces the peer advertised to us, requestable while it chokes us |
(package private) BitField | bitfieldThe pieces the peer has. |
(package private) boolean | chokedWhether the peer has choked us |
(package private) boolean | chokingWhether we are choking the peer |
(package private) PeerConnectionIn | inInbound connection |
(package private) boolean | interestedWhether the peer is interested in us |
(package private) boolean | interestingWhether we are interested in the peer |
(package private) PeerListener | listenerFixme, used by Peer.disconnect() to get to the coordinator |
static int | MAX_PARTSIZECap on a single piece request from a peer. |
static int | MAX_PIPELINEMaximum request pipeline depth for outbound requests. |
static int | MIN_PIPELINEMinimum request pipeline depth for outbound requests. |
(package private) PeerConnectionOut | outOutbound connection |
static int | PARTSIZEChunk size for outbound piece requests. |
| Constructor and Description |
|---|
PeerState(Peer peer,
PeerListener listener,
BandwidthListener bwl,
MetaInfo metainfo,
PeerConnectionIn in,
PeerConnectionOut out)Create the peer state for the torrent. |
| Modifier and Type | Method and Description |
|---|---|
(package private) void | addRequest()Adds a new request to the outstanding requests list. |
(package private) void | allowedFastMessage(int piece)Handle an allowed fast message (BEP 6): the peer will serve this piece even while it chokes
us. |
(package private) void | bitfieldMessage(byte[] bitmap)Handle a bitfield message from the peer |
(package private) void | cancelMessage(int piece,
int begin,
int length)Handle a cancel message from the peer |
(package private) void | cancelPiece(int piece)Tell the other side that we are no longer interested in any of the outstanding requests (if
any) for this piece. |
(package private) void | chokeMessage(boolean choke)Handle a choke or unchoke message from the peer |
(package private) void | dontHaveMessage(int piece)Handle a BEP 54 lt_donthave message: the peer no longer has a piece it advertised. |
(package private) Set<Integer> | expectedAllowedFast()The set of pieces a conforming sender may advertise, derived from our own destination hash,
or null when our destination cannot be determined. |
(package private) void | extensionMessage(int id,
byte[] bs)Handle an extension message from the peer. |
(package private) static Set<Integer> | generateAllowedFastSet(byte[] peerHash,
byte[] infohash,
int pieces)Generate the BEP 6 allowed fast set for a peer. |
(package private) BandwidthListener | getBandwidthListener()Returns the bandwidth listener for this peer state. |
(package private) Request | getOutstandingRequest(int piece,
int begin,
int length)Called when a piece message is being processed by the incoming connection. |
(package private) String | getRequests()Debug string for the peer, or null. |
(package private) void | haveMessage(boolean isAll)BEP 6 |
(package private) void | haveMessage(int piece)Handle a have message from the peer |
(package private) void | havePiece(int piece)We now have this piece. |
(package private) void | interestedMessage(boolean interest)Handle an interested or uninterested message from the peer |
(package private) boolean | isAllowedFast(int piece)Whether the piece is in our BEP 6 allowed fast set for this peer. |
(package private) boolean | isSnubbing()True if the peer is snubbing us: it is unchoked by us and interested but has sent no request
for SNUB_TIMEOUT, and nothing is queued to send it. |
(package private) void | keepAlive()Send a keepalive message to the peer. |
(package private) void | keepAliveMessage()Handle a keepalive message from the peer |
ByteArray | loadData(int piece,
int begin,
int length)This is the callback that PeerConnectionOut calls |
static int | maxPipelineBytes()Maximum bytes of pipelined inbound requests to a single peer. |
(package private) void | pieceMessage(Request req)Called when a full chunk (i.e. |
(package private) void | portMessage(int port)Handle a port message from the peer. |
(package private) void | rejectMessage(int piece,
int begin,
int length)BEP 6 |
(package private) void | requestMessage(int piece,
int begin,
int length)Handle a request message from the peer |
(package private) void | retransmitRequests()Retransmit outstanding requests if necessary. |
(package private) List<Request> | returnPartialPieces()Return partial pieces to the PeerCoordinator and clear the request queue. |
(package private) List<Request> | returnPartialPieces(int piece)Return the partial piece for a piece to the PeerCoordinator. |
(package private) void | sendAllowedFast(BitField ourBitfield)Generate and queue the BEP 6 allowed fast set for this peer. |
(package private) void | setChoking(boolean choke)Whether we are choking the peer. |
(package private) void | setInteresting(boolean interest)Whether we are interested in pieces from this peer. |
void | setMetaInfo(MetaInfo meta)Switch from magnet mode to normal mode. |
static void | setPipelineParams(int min,
int max,
int chunk,
int maxPart)Apply configurable pipeline bounds, chunk size, and per-request cap, clamped to safe
limits. |
(package private) void | suggestMessage(int piece)BEP 6 Suggest. |
(package private) void | unknownMessage(int type,
byte[] bs)Handle an unknown message type from the peer. |
(package private) void | uploaded(int size)Called when some bytes have left the outgoing connection. |
Set<Integer> _expectedAllowedFast
final Set<Integer> _peerAllowedFast
BitField bitfield
volatile boolean choked
volatile boolean choking
final PeerConnectionIn in
volatile boolean interested
volatile boolean interesting
final PeerListener listener
public static volatile int MAX_PARTSIZE
public static volatile int MAX_PIPELINE
public static volatile int MIN_PIPELINE
final PeerConnectionOut out
public static volatile int PARTSIZE
PeerState(Peer peer, PeerListener listener, BandwidthListener bwl, MetaInfo metainfo, PeerConnectionIn in, PeerConnectionOut out)
metainfo - null if in magnet modevoid addRequest()
This is called from several places:
By getOutstandingRequest() when the first part of a chunk comes in By havePiece() when somebody got a new piece completed By chokeMessage() when we receive an unchoke By setInteresting() when we are now interested By PeerCoordinator.updatePiecePriorities()
void allowedFastMessage(int piece)
Indices outside the deterministic set derived from our own destination hash are dropped, defending against buggy or malicious senders that invent pieces.
piece - the piece indexvoid bitfieldMessage(byte[] bitmap)
void cancelMessage(int piece,
int begin,
int length)void cancelPiece(int piece)
void chokeMessage(boolean choke)
void dontHaveMessage(int piece)
piece - the piece indexSet<Integer> expectedAllowedFast()
Computed once per connection; called only from the reader thread.
void extensionMessage(int id,
byte[] bs)id - the extension message IDbs - the message payloadstatic Set<Integer> generateAllowedFastSet(byte[] peerHash, byte[] infohash, int pieces)
The first four bytes of the peer's destination hash stand in for the masked IP address, so both ends of an I2P connection can compute the same up to ten piece indices from the torrent infohash.
peerHash - the 32-byte destination hash of the peerinfohash - the infohash of the torrentpieces - the number of pieces in the torrentBandwidthListener getBandwidthListener()
Request getOutstandingRequest(int piece, int begin, int length)
String getRequests()
void haveMessage(boolean isAll)
isAll - true for have_all, false for have_nonevoid haveMessage(int piece)
void havePiece(int piece)
void interestedMessage(boolean interest)
boolean isAllowedFast(int piece)
piece - the piece indexboolean isSnubbing()
void keepAlive()
void keepAliveMessage()
public ByteArray loadData(int piece, int begin, int length)
loadData in interface DataLoaderRuntimeException - on IOE getting the datapublic static int maxPipelineBytes()
void pieceMessage(Request req)
This may block quite a while if it is the last chunk for a piece, as it calls the listener, who stores the piece and then calls havePiece for every peer on the torrent (including us).
void portMessage(int port)
port - the port numbervoid rejectMessage(int piece,
int begin,
int length)void requestMessage(int piece,
int begin,
int length)void retransmitRequests()
List<Request> returnPartialPieces()
List<Request> returnPartialPieces(int piece)
piece - the piece indexvoid sendAllowedFast(BitField ourBitfield)
The set is derived from the peer's destination hash, so both ends compute the same pieces. Only pieces we actually have are advertised, and requests for them are served even while we choke the peer.
ourBitfield - the pieces we have, may be nullvoid setChoking(boolean choke)
choke - true to choke, false to unchokevoid setInteresting(boolean interest)
interest - true if interestedpublic void setMetaInfo(MetaInfo meta)
meta - non-nullpublic static void setPipelineParams(int min,
int max,
int chunk,
int maxPart)min - minimum pipeline depth, clamped to [1, max]max - maximum pipeline depth, clamped to [min, MAX_PIPELINE_CAP]chunk - request chunk size in bytes, clamped to [1KiB, maxPart]maxPart - cap on a single request in bytes, clamped to [chunk, 1MiB]void suggestMessage(int piece)
void unknownMessage(int type,
byte[] bs)type - the message type bytebs - the message payloadvoid uploaded(int size)