Search in sources :

Example 1 with DirectMessageReader

use of org.apache.ignite.internal.network.direct.DirectMessageReader in project ignite-3 by apache.

the class InboundDecoder method decode.

/**
 * {@inheritDoc}
 */
@Override
public void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
    ByteBuffer buffer = in.nioBuffer();
    Attribute<MessageReader> readerAttr = ctx.channel().attr(READER_KEY);
    MessageReader reader = readerAttr.get();
    if (reader == null) {
        reader = new DirectMessageReader(serializationService, ConnectionManager.DIRECT_PROTOCOL_VERSION);
        readerAttr.set(reader);
    }
    Attribute<MessageDeserializer<NetworkMessage>> messageAttr = ctx.channel().attr(DESERIALIZER_KEY);
    while (buffer.hasRemaining()) {
        int initialNioBufferPosition = buffer.position();
        MessageDeserializer<NetworkMessage> msg = messageAttr.get();
        try {
            // Read message type.
            if (msg == null) {
                if (buffer.remaining() >= NetworkMessage.MSG_TYPE_SIZE_BYTES) {
                    msg = serializationService.createMessageDeserializer(DirectMarshallingUtils.getShort(buffer), DirectMarshallingUtils.getShort(buffer));
                } else {
                    break;
                }
            }
            boolean finished = false;
            // Read message if buffer has remaining data.
            if (msg != null && buffer.hasRemaining()) {
                reader.setCurrentReadClass(msg.klass());
                reader.setBuffer(buffer);
                finished = msg.readMessage(reader);
            }
            int readBytes = buffer.position() - initialNioBufferPosition;
            // Set read position to Netty's ByteBuf.
            in.readerIndex(in.readerIndex() + readBytes);
            if (finished) {
                reader.reset();
                messageAttr.set(null);
                NetworkMessage message = msg.getMessage();
                if (message instanceof ClassDescriptorListMessage) {
                    onClassDescriptorMessage((ClassDescriptorListMessage) message);
                } else {
                    out.add(message);
                }
            } else {
                messageAttr.set(msg);
            }
            if (readBytes == 0) {
                // less than N bytes available.
                break;
            }
        } catch (Throwable e) {
            if (LOG.isDebugEnabled()) {
                LOG.debug(String.format("Failed to read message [msg=%s, buf=%s, reader=%s]: %s", msg, buffer, reader, e.getMessage()), e);
            }
            throw e;
        }
    }
}
Also used : ClassDescriptorListMessage(org.apache.ignite.internal.network.message.ClassDescriptorListMessage) DirectMessageReader(org.apache.ignite.internal.network.direct.DirectMessageReader) MessageDeserializer(org.apache.ignite.network.serialization.MessageDeserializer) MessageReader(org.apache.ignite.network.serialization.MessageReader) DirectMessageReader(org.apache.ignite.internal.network.direct.DirectMessageReader) NetworkMessage(org.apache.ignite.network.NetworkMessage) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ByteBuffer (java.nio.ByteBuffer)1 DirectMessageReader (org.apache.ignite.internal.network.direct.DirectMessageReader)1 ClassDescriptorListMessage (org.apache.ignite.internal.network.message.ClassDescriptorListMessage)1 NetworkMessage (org.apache.ignite.network.NetworkMessage)1 MessageDeserializer (org.apache.ignite.network.serialization.MessageDeserializer)1 MessageReader (org.apache.ignite.network.serialization.MessageReader)1