Search in sources :

Example 1 with GenericMessageConnectionListener

use of com.biglybt.pif.messaging.generic.GenericMessageConnectionListener in project BiglyBT by BiglySoftware.

the class SESTSConnectionImpl method receiveContent.

protected void receiveContent(PooledByteBuffer message) throws MessageException {
    boolean buffer_handled = false;
    try {
        if (incoming_cipher != null) {
            try {
                byte[] enc = message.toByteArray();
                byte[] plain = incoming_cipher.doFinal(enc);
                PooledByteBuffer temp = new PooledByteBufferImpl(plain);
                message.returnToPool();
                buffer_handled = true;
                message = temp;
            } catch (Throwable e) {
                throw (new MessageException("Failed to decrypt data", e));
            }
        } else if (block_crypto != SESecurityManager.BLOCK_ENCRYPTION_NONE) {
            throw (new MessageException("Crypto isn't setup"));
        }
        List<GenericMessageConnectionListener> listeners_ref = listeners.getList();
        MessageException last_error = null;
        for (int i = 0; i < listeners_ref.size(); i++) {
            PooledByteBuffer message_to_deliver;
            if (i == 0) {
                message_to_deliver = message;
            } else {
                // unlikely we'll ever have > 1 receiver....
                message_to_deliver = new PooledByteBufferImpl(message.toByteArray());
            }
            try {
                listeners_ref.get(i).receive(this, message_to_deliver);
                if (message_to_deliver == message) {
                    buffer_handled = true;
                }
            } catch (Throwable e) {
                message_to_deliver.returnToPool();
                if (message_to_deliver == message) {
                    buffer_handled = true;
                }
                if (e instanceof MessageException) {
                    last_error = (MessageException) e;
                } else {
                    last_error = new MessageException("Failed to process message", e);
                }
            }
        }
        if (last_error != null) {
            throw (last_error);
        }
    } finally {
        if (!buffer_handled) {
            message.returnToPool();
        }
    }
}
Also used : GenericMessageConnectionListener(com.biglybt.pif.messaging.generic.GenericMessageConnectionListener) PooledByteBufferImpl(com.biglybt.pifimpl.local.utils.PooledByteBufferImpl) MessageException(com.biglybt.pif.messaging.MessageException) PooledByteBuffer(com.biglybt.pif.utils.PooledByteBuffer) GenericMessageEndpoint(com.biglybt.pif.messaging.generic.GenericMessageEndpoint) GenericMessageStartpoint(com.biglybt.pif.messaging.generic.GenericMessageStartpoint)

Example 2 with GenericMessageConnectionListener

use of com.biglybt.pif.messaging.generic.GenericMessageConnectionListener in project BiglyBT by BiglySoftware.

the class GenericMessageConnectionImpl method receive.

protected void receive(GenericMessage message) {
    boolean handled = false;
    for (int i = 0; i < listeners.size(); i++) {
        PooledByteBuffer buffer = new PooledByteBufferImpl(message.getPayload());
        try {
            ((GenericMessageConnectionListener) listeners.get(i)).receive(this, buffer);
            handled = true;
        } catch (Throwable f) {
            buffer.returnToPool();
            if (!(f instanceof MessageException)) {
                Debug.printStackTrace(f);
            }
        }
    }
    if (!handled && !(closed || closing)) {
    // get this on some processing paths
    // Debug.out( "GenericMessage: incoming message not handled" );
    }
}
Also used : GenericMessageConnectionListener(com.biglybt.pif.messaging.generic.GenericMessageConnectionListener) PooledByteBufferImpl(com.biglybt.pifimpl.local.utils.PooledByteBufferImpl) MessageException(com.biglybt.pif.messaging.MessageException) PooledByteBuffer(com.biglybt.pif.utils.PooledByteBuffer) GenericMessageEndpoint(com.biglybt.pif.messaging.generic.GenericMessageEndpoint) GenericMessageStartpoint(com.biglybt.pif.messaging.generic.GenericMessageStartpoint)

Aggregations

MessageException (com.biglybt.pif.messaging.MessageException)2 GenericMessageConnectionListener (com.biglybt.pif.messaging.generic.GenericMessageConnectionListener)2 GenericMessageEndpoint (com.biglybt.pif.messaging.generic.GenericMessageEndpoint)2 GenericMessageStartpoint (com.biglybt.pif.messaging.generic.GenericMessageStartpoint)2 PooledByteBuffer (com.biglybt.pif.utils.PooledByteBuffer)2 PooledByteBufferImpl (com.biglybt.pifimpl.local.utils.PooledByteBufferImpl)2