class Message extends Object
This class encapsulates all BitTorrent protocol message types including:
Messages are created and queued for sending through a DataOutputStream. The sendMessage() method translates messages to wire format.
Message structure includes:
PeerState| Modifier and Type | Field and Description |
|---|---|
(package private) static byte | ALLOWED_FASTAllow request of a piece without an unchoke (BEP 6). |
(package private) int | beginThe offset within the piece |
(package private) static byte | BITFIELDBitfield of pieces the peer has. |
(package private) static byte | CANCELCancel a previously sent request. |
(package private) static byte | CHOKEStop sending piece data to the peer. |
(package private) byte[] | dataThe data payload |
(package private) static byte | EXTENSIONBEP 10 extended message, sub-type in the low byte. |
(package private) static byte | HASH_REJECTReject a hash request (BEP 52). |
(package private) static byte | HASH_REQUESTRequest merkle hash tree data (BEP 52). |
(package private) static byte | HASHESMerkle hash tree data (BEP 52). |
(package private) static byte | HAVEAnnounce possession of a single piece. |
(package private) static byte | HAVE_ALLAnnounce possession of every piece (BEP 6). |
(package private) static byte | HAVE_NONEAnnounce possession of no pieces (BEP 6). |
(package private) static byte | INTERESTEDSignal that the peer has pieces we want. |
(package private) static byte | KEEP_ALIVENo-op message sent periodically to keep the connection alive. |
(package private) int | lenThe payload length |
(package private) int | lengthThe data length |
(package private) int | offThe data offset |
(package private) int | pieceThe piece index |
(package private) static byte | PIECEChunk of a piece in response to a request. |
(package private) static byte | PORTAnnounce the DHT listening port (BEP 5). |
(package private) static byte | REJECTReject a previously accepted request (BEP 6). |
(package private) static byte | REQUESTRequest a chunk of a piece. |
(package private) static byte | SUGGESTSuggest a piece the peer may want (BEP 6). |
(package private) byte | typeThe message type |
(package private) static byte | UNCHOKEResume sending piece data to the peer. |
(package private) static byte | UNINTERESTEDSignal that we no longer want pieces from the peer. |
| Constructor and Description |
|---|
Message(byte type)For types KEEP_ALIVE, CHOKE, UNCHOKE, INTERESTED, UNINTERESTED, HAVE_ALL, HAVE_NONE |
Message(byte[] data)For type BITFIELD |
Message(byte type,
int piece)For types HAVE, PORT, SUGGEST, ALLOWED_FAST |
Message(byte type,
int piece,
int begin,
int length)For types REQUEST, REJECT, CANCEL |
Message(int id,
byte[] data)For type EXTENSION |
Message(int piece,
int begin,
int length,
DataLoader loader)For type PIECE with deferred data |
| Modifier and Type | Method and Description |
|---|---|
(package private) boolean | abortIfDiscarded()Abort a claimed-but-not-yet-started prefetch because the message was purged unsent,
saving the disk read entirely. |
(package private) boolean | claimLoad()Claim exclusive right to execute the loader for this message. |
(package private) void | completeLoad(ByteArray ba)Deliver the result of a loader run started after claimLoad() returned true. |
(package private) void | discard()Mark this message as dropped without sending and release any loaded buffer exactly once. |
(package private) boolean | isDataReady()Whether the deferred data has been loaded and not yet released. |
(package private) boolean | isDiscarded()Whether this message was dropped without sending. |
(package private) ByteArray | runLoader()Invoke the deferred loader. |
(package private) boolean | sendMessage(DataOutputStream dos)Sends this message through the given DataOutputStream. |
String | toString()A string representation of the message. |
static final byte ALLOWED_FAST
final int begin
static final byte BITFIELD
static final byte CANCEL
static final byte CHOKE
byte[] data
static final byte EXTENSION
static final byte HASH_REJECT
static final byte HASH_REQUEST
static final byte HASHES
static final byte HAVE
static final byte HAVE_ALL
static final byte HAVE_NONE
static final byte INTERESTED
static final byte KEEP_ALIVE
final int len
final int length
final int off
final int piece
static final byte PIECE
static final byte PORT
static final byte REJECT
static final byte REQUEST
static final byte SUGGEST
final byte type
static final byte UNCHOKE
static final byte UNINTERESTED
Message(byte type)
Message(byte[] data)
Message(byte type,
int piece)Message(byte type,
int piece,
int begin,
int length)Message(int id,
byte[] data)Message(int piece,
int begin,
int length,
DataLoader loader)boolean abortIfDiscarded()
boolean claimLoad()
completeLoad(ByteArray);
false if already claimed, completed, failed, discarded, or not loader-backedvoid completeLoad(ByteArray ba)
claimLoad() returned true.
Stores the buffer unless the message was discarded meanwhile, in which case the buffer
is released immediately; failure is recorded so the sender drops the message.ba - the loaded buffer, or null on load failurevoid discard()
boolean isDataReady()
boolean isDiscarded()
ByteArray runLoader()
claimLoad();
executes with no locks held so slow reads never block purge paths.boolean sendMessage(DataOutputStream dos) throws IOException
dos - the output stream to write toIOException - if an I/O error occurs