use of com.biglybt.core.peermanager.messaging.Message in project BiglyBT by BiglySoftware.
the class AZMessageDecoder method destroy.
@Override
public ByteBuffer destroy() {
is_paused = true;
destroyed = true;
/*
int lbuff_read = 0;
int pbuff_read = 0;
length_buffer.limit( SS, 4 );
if( reading_length_mode ) {
lbuff_read = length_buffer.position( SS );
}
else { //reading payload
length_buffer.position( SS, 4 );
lbuff_read = 4;
pbuff_read = payload_buffer == null ? 0 : payload_buffer.position( SS );
}
ByteBuffer unused = ByteBuffer.allocate( lbuff_read + pbuff_read );
length_buffer.flip( SS );
unused.put( length_buffer.getBuffer( SS ) );
if ( payload_buffer != null ) {
payload_buffer.flip( SS );
unused.put( payload_buffer.getBuffer( SS ) );
}
unused.flip();
*/
length_buffer.returnToPool();
if (payload_buffer != null) {
payload_buffer.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 (IndexOutOfBoundsException e) {
// as access to messages_last_read isn't synchronized we can get this error if we destroy the
// decoder in parallel with messages being removed. We don't really want to synchronize access
// to this so we'll take the hit here
}
messages_last_read.clear();
// NOTE: we don't bother returning any already-read data
return null;
}
use of com.biglybt.core.peermanager.messaging.Message 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.peermanager.messaging.Message in project BiglyBT by BiglySoftware.
the class BTMessageDecoder method destroy.
@Override
public ByteBuffer destroy() {
if (destroyed) {
// Not like we're goig to do anything about this - parg 2018/11/17
// 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;
DirectByteBuffer lb = length_buffer;
if (lb != null && lb.hasBeenReturnedToPool()) {
lb = null;
}
DirectByteBuffer plb = payload_buffer;
if (plb != null && plb.hasBeenReturnedToPool()) {
plb = null;
}
if (lb != null) {
lb.limit(SS, 4);
if (reading_length_mode) {
lbuff_read = lb.position(SS);
} else {
// reading payload
lb.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);
if (lb != null) {
lb.flip(SS);
unused.put(lb.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();
lb.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.peermanager.messaging.Message in project BiglyBT by BiglySoftware.
the class PEPeerTransportProtocol method sendMainlineDHTPort.
private void sendMainlineDHTPort() {
if (!this.ml_dht_enabled) {
return;
}
MainlineDHTProvider provider = getDHTProvider();
if (provider == null) {
return;
}
Message message = new BTDHTPort(provider.getDHTPort());
connection.getOutgoingMessageQueue().addMessage(message, false);
}
use of com.biglybt.core.peermanager.messaging.Message in project BiglyBT by BiglySoftware.
the class GenericMessageConnectionDirect method startProcessing.
protected void startProcessing() {
connection.getIncomingMessageQueue().registerQueueListener(new IncomingMessageQueue.MessageQueueListener() {
@Override
public boolean messageReceived(Message _message) {
GenericMessage message = (GenericMessage) _message;
dispatcher.dispatch(new AERunnable() {
@Override
public void runSupport() {
if (dispatcher.getQueueSize() > 10) {
try {
close();
} catch (Throwable e) {
}
} else {
owner.receive(message);
}
}
});
return (true);
}
@Override
public void protocolBytesReceived(int byte_count) {
}
@Override
public void dataBytesReceived(int byte_count) {
}
@Override
public boolean isPriority() {
return false;
}
});
connection.getOutgoingMessageQueue().registerQueueListener(new OutgoingMessageQueue.MessageQueueListener() {
@Override
public boolean messageAdded(Message message) {
return (true);
}
@Override
public void messageQueued(Message message) {
// System.out.println( " queued: " + message );
}
@Override
public void messageRemoved(Message message) {
// System.out.println( " removed: " + message );
}
@Override
public void messageSent(Message message) {
// System.out.println( " sent: " + message );
}
@Override
public void protocolBytesSent(int byte_count) {
}
@Override
public void dataBytesSent(int byte_count) {
}
@Override
public void flush() {
}
});
connection.startMessageProcessing();
connection.enableEnhancedMessageProcessing(true, -1);
synchronized (this) {
if (inbound_rls != null) {
for (int i = 0; i < inbound_rls.size(); i++) {
connection.addRateLimiter((LimitedRateGroup) inbound_rls.get(i), false);
}
inbound_rls = null;
}
if (outbound_rls != null) {
for (int i = 0; i < outbound_rls.size(); i++) {
connection.addRateLimiter((LimitedRateGroup) outbound_rls.get(i), true);
}
outbound_rls = null;
}
processing = true;
}
}
Aggregations