i2psnark: finals for Message class

recognize BiglyBT
cleanups
This commit is contained in:
zzz
2017-08-22 12:34:38 +00:00
parent 3d385228f0
commit 51d4827657
5 changed files with 126 additions and 102 deletions

View File

@@ -52,26 +52,26 @@ class Message
// Not all fields are used for every message.
// KEEP_ALIVE doesn't have a real wire representation
byte type;
final byte type;
// Used for HAVE, REQUEST, PIECE and CANCEL messages.
// Also SUGGEST, REJECT, ALLOWED_FAST
// low byte used for EXTENSION message
// low two bytes used for PORT message
int piece;
final int piece;
// Used for REQUEST, PIECE and CANCEL messages.
// Also REJECT
int begin;
int length;
final int begin;
final int length;
// Used for PIECE and BITFIELD and EXTENSION messages
byte[] data;
int off;
int len;
final int off;
final int len;
// Used to do deferred fetch of data
DataLoader dataLoader;
private final DataLoader dataLoader;
// now unused
//SimpleTimer.TimedEvent expireEvent;
@@ -79,6 +79,79 @@ class Message
private static final int BUFSIZE = PeerState.PARTSIZE;
private static final ByteCache _cache = ByteCache.getInstance(16, BUFSIZE);
/**
* For types KEEP_ALIVE, CHOKE, UNCHOKE, INTERESTED, UNINTERESTED, HAVE_ALL, HAVE_NONE
* @since 0.9.32
*/
Message(byte type) {
this(type, 0, 0, 0, null, 0, 0, null);
}
/**
* For types HAVE, PORT, SUGGEST, ALLOWED_FAST
* @since 0.9.32
*/
Message(byte type, int piece) {
this(type, piece, 0, 0, null, 0, 0, null);
}
/**
* For types REQUEST, REJECT, CANCEL
* @since 0.9.32
*/
Message(byte type, int piece, int begin, int length) {
this(type, piece, begin, length, null, 0, 0, null);
}
/**
* For type BITFIELD
* @since 0.9.32
*/
Message(byte[] data) {
this(BITFIELD, 0, 0, 0, data, 0, data.length, null);
}
/**
* For type EXTENSION
* @since 0.9.32
*/
Message(int id, byte[] data) {
this(EXTENSION, id, 0, 0, data, 0, data.length, null);
}
/**
* For type PIECE with deferred data
* @since 0.9.32
*/
Message(int piece, int begin, int length, DataLoader loader) {
this(PIECE, piece, begin, length, null, 0, length, loader);
}
/**
* For type PIECE with data
* We don't use this anymore.
* @since 0.9.32
*/
/****
Message(int piece, int begin, int length, byte[] data) {
this(PIECE, piece, begin, length, data, 0, length, null);
}
****/
/**
* @since 0.9.32
*/
private Message(byte type, int piece, int begin, int length, byte[] data, int off, int len, DataLoader loader) {
this.type = type;
this.piece = piece;
this.begin = begin;
this.length = length;
this.data = data;
this.off = off;
this.len = len;
dataLoader = loader;
}
/** Utility method for sending a message through a DataStream. */
void sendMessage(DataOutputStream dos) throws IOException
{
@@ -121,10 +194,10 @@ class Message
datalen += 4;
// msg type is 1 byte
if (type == EXTENSION)
else if (type == EXTENSION)
datalen += 1;
if (type == PORT)
else if (type == PORT)
datalen += 2;
// add length of data for piece or bitfield array.
@@ -150,10 +223,10 @@ class Message
type == REJECT)
dos.writeInt(length);
if (type == EXTENSION)
else if (type == EXTENSION)
dos.writeByte((byte) piece & 0xff);
if (type == PORT)
else if (type == PORT)
dos.writeShort(piece & 0xffff);
// Send actual data