use of com.biglybt.core.util.DirectByteBuffer in project BiglyBT by BiglySoftware.
the class AZRequestHint method getData.
@Override
public DirectByteBuffer[] getData() {
if (buffer == null) {
Map map = new HashMap();
map.put("piece", new Long(piece_number));
map.put("offset", new Long(offset));
map.put("length", new Long(length));
map.put("life", new Long(life));
buffer = MessagingUtil.convertPayloadToBencodedByteStream(map, DirectByteBuffer.AL_MSG);
}
return new DirectByteBuffer[] { buffer };
}
use of com.biglybt.core.util.DirectByteBuffer in project BiglyBT by BiglySoftware.
the class BTKeepAlive method getRawData.
// raw message
@Override
public DirectByteBuffer[] getRawData() {
if (buffer == null) {
DirectByteBuffer dbb = DirectByteBufferPool.getBuffer(DirectByteBuffer.AL_MSG_BT_KEEPALIVE, 4);
dbb.putInt(DirectByteBuffer.SS_BT, 0);
dbb.flip(DirectByteBuffer.SS_BT);
buffer = new DirectByteBuffer[] { dbb };
}
return buffer;
}
use of com.biglybt.core.util.DirectByteBuffer in project BiglyBT by BiglySoftware.
the class BTMessageDecoder method destroy.
@Override
public ByteBuffer destroy() {
if (destroyed) {
Debug.out("Trying to redestroy message decoder, stack trace follows: " + this);
Debug.outStackTrace();
}
is_paused = true;
destroyed = true;
// there's a concurrency issue with the decoder whereby it can be destroyed while will being messed with. Don't
// have the energy to look into it properly atm so just try to ensure that it doesn't bork too badly (parg: 29/04/2012)
// only occasional but does have potential to generate direct buffer mem leak ;(
int lbuff_read = 0;
int pbuff_read = 0;
length_buffer.limit(SS, 4);
DirectByteBuffer plb = payload_buffer;
if (reading_length_mode) {
lbuff_read = length_buffer.position(SS);
} else {
// reading payload
length_buffer.position(SS, 4);
lbuff_read = 4;
pbuff_read = plb == null ? 0 : plb.position(SS);
}
// TODO convert to direct?
ByteBuffer unused = ByteBuffer.allocate(lbuff_read + pbuff_read);
length_buffer.flip(SS);
unused.put(length_buffer.getBuffer(SS));
try {
if (plb != null) {
plb.flip(SS);
// Got a buffer overflow exception here in the past - related to PEX?
unused.put(plb.getBuffer(SS));
}
} catch (RuntimeException e) {
Debug.out("hit known threading issue");
}
unused.flip();
length_buffer.returnToPool();
if (plb != null) {
plb.returnToPool();
payload_buffer = null;
}
try {
for (int i = 0; i < messages_last_read.size(); i++) {
Message msg = (Message) messages_last_read.get(i);
msg.destroy();
}
} catch (RuntimeException e) {
// happens if messages modified by alt thread...
Debug.out("hit known threading issue");
}
messages_last_read.clear();
return unused;
}
use of com.biglybt.core.util.DirectByteBuffer in project BiglyBT by BiglySoftware.
the class BTMessageDecoder method postReadProcess.
private int postReadProcess() throws IOException {
int prot_bytes_read = 0;
int data_bytes_read = 0;
if (!reading_length_mode && !destroyed) {
// reading payload data mode
// ensure-restore proper buffer limits
payload_buffer.limit(SS, message_length);
length_buffer.limit(SS, 4);
int read = payload_buffer.position(SS) - pre_read_start_position;
if (payload_buffer.position(SS) > 0) {
// need to have read the message id first byte
if (BTMessageFactory.getMessageType(payload_buffer) == Message.TYPE_DATA_PAYLOAD) {
data_bytes_read += read;
} else {
prot_bytes_read += read;
}
}
if (!payload_buffer.hasRemaining(SS) && !is_paused) {
// full message received!
payload_buffer.position(SS, 0);
DirectByteBuffer ref_buff = payload_buffer;
payload_buffer = null;
if (reading_handshake_message) {
// decode handshake
reading_handshake_message = false;
DirectByteBuffer handshake_data = DirectByteBufferPool.getBuffer(DirectByteBuffer.AL_MSG_BT_HAND, 68);
handshake_data.putInt(SS, HANDSHAKE_FAKE_LENGTH);
handshake_data.put(SS, ref_buff);
handshake_data.flip(SS);
ref_buff.returnToPool();
try {
Message handshake = MessageManager.getSingleton().createMessage(BTMessage.ID_BT_HANDSHAKE_BYTES, handshake_data, (byte) 1);
messages_last_read.add(handshake);
} catch (MessageException me) {
handshake_data.returnToPool();
throw new IOException("BT message decode failed: " + me.getMessage());
}
// we need to auto-pause decoding until we're told to start again externally,
// as we don't want to accidentally read the next message on the stream if it's an AZ-format handshake
pauseDecoding();
} else {
// decode normal message
try {
messages_last_read.add(createMessage(ref_buff));
} catch (Throwable e) {
ref_buff.returnToPoolIfNotFree();
if (e instanceof RuntimeException) {
throw ((RuntimeException) e);
}
throw new IOException("BT message decode failed: " + e.getMessage());
}
}
// see if we've already read the next message's length
reading_length_mode = true;
// reset receive percentage
percent_complete = -1;
} else {
// only partial received so far
// compute receive percentage
percent_complete = (payload_buffer.position(SS) * 100) / message_length;
}
}
if (reading_length_mode && !destroyed) {
// ensure proper buffer limit
length_buffer.limit(SS, 4);
prot_bytes_read += (pre_read_start_buffer == 1) ? length_buffer.position(SS) - pre_read_start_position : length_buffer.position(SS);
if (!length_buffer.hasRemaining(SS)) {
// done reading the length
reading_length_mode = false;
length_buffer.position(SS, 0);
message_length = length_buffer.getInt(SS);
// reset it for next length read
length_buffer.position(SS, 0);
if (message_length == HANDSHAKE_FAKE_LENGTH) {
// handshake message
reading_handshake_message = true;
// restore 'real' length
message_length = 64;
payload_buffer = DirectByteBufferPool.getBuffer(DirectByteBuffer.AL_MSG_BT_HAND, message_length);
} else if (message_length == 0) {
// keep-alive message
reading_length_mode = true;
last_received_was_keepalive = true;
try {
Message keep_alive = MessageManager.getSingleton().createMessage(BTMessage.ID_BT_KEEP_ALIVE_BYTES, null, (byte) 1);
messages_last_read.add(keep_alive);
} catch (MessageException me) {
throw new IOException("BT message decode failed: " + me.getMessage());
}
} else if (message_length < MIN_MESSAGE_LENGTH || message_length > MAX_MESSAGE_LENGTH) {
throw new IOException("Invalid message length given for BT message decode: " + message_length);
} else {
// normal message
payload_buffer = DirectByteBufferPool.getBuffer(DirectByteBuffer.AL_MSG_BT_PAYLOAD, message_length);
}
}
}
protocol_bytes_last_read += prot_bytes_read;
data_bytes_last_read += data_bytes_read;
return prot_bytes_read + data_bytes_read;
}
use of com.biglybt.core.util.DirectByteBuffer in project BiglyBT by BiglySoftware.
the class AZStatReply method getData.
@Override
public DirectByteBuffer[] getData() {
if (buffer == null) {
Map map = new HashMap();
map.put("reply", reply);
buffer = MessagingUtil.convertPayloadToBencodedByteStream(map, DirectByteBuffer.AL_MSG);
}
return new DirectByteBuffer[] { buffer };
}
Aggregations