Search in sources :

Example 1 with Signal

use of io.netty.util.Signal in project netty by netty.

the class ReplayingDecoderByteBufTest method testGetByte.

/**
 * See https://github.com/netty/netty/issues/445
 */
@Test
public void testGetByte() {
    ByteBuf buf = Unpooled.copiedBuffer("TestBuffer", CharsetUtil.ISO_8859_1);
    ReplayingDecoderByteBuf buffer = new ReplayingDecoderByteBuf(buf);
    boolean error;
    int i = 0;
    try {
        for (; ; ) {
            buffer.getByte(i);
            i++;
        }
    } catch (Signal e) {
        error = true;
    }
    assertTrue(error);
    assertEquals(10, i);
    buf.release();
}
Also used : Signal(io.netty.util.Signal) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 2 with Signal

use of io.netty.util.Signal in project netty by netty.

the class ReplayingDecoderByteBufTest method testGetUnsignedByte.

/**
 * See https://github.com/netty/netty/issues/445
 */
@Test
public void testGetUnsignedByte() {
    ByteBuf buf = Unpooled.copiedBuffer("TestBuffer", CharsetUtil.ISO_8859_1);
    ReplayingDecoderByteBuf buffer = new ReplayingDecoderByteBuf(buf);
    boolean error;
    int i = 0;
    try {
        for (; ; ) {
            buffer.getUnsignedByte(i);
            i++;
        }
    } catch (Signal e) {
        error = true;
    }
    assertTrue(error);
    assertEquals(10, i);
    buf.release();
}
Also used : Signal(io.netty.util.Signal) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 3 with Signal

use of io.netty.util.Signal in project netty by netty.

the class ReplayingDecoderByteBufTest method testGetBoolean.

/**
 * See https://github.com/netty/netty/issues/445
 */
@Test
public void testGetBoolean() {
    ByteBuf buf = Unpooled.buffer(10);
    while (buf.isWritable()) {
        buf.writeBoolean(true);
    }
    ReplayingDecoderByteBuf buffer = new ReplayingDecoderByteBuf(buf);
    boolean error;
    int i = 0;
    try {
        for (; ; ) {
            buffer.getBoolean(i);
            i++;
        }
    } catch (Signal e) {
        error = true;
    }
    assertTrue(error);
    assertEquals(10, i);
    buf.release();
}
Also used : Signal(io.netty.util.Signal) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 4 with Signal

use of io.netty.util.Signal in project infinispan by infinispan.

the class HeaderDecoder method decode.

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
    try {
        switch(state()) {
            case READ_MESSAGE_ID:
                long messageId = codec.readMessageId(in);
                receivedOpCode = codec.readOpCode(in);
                switch(receivedOpCode) {
                    case CACHE_ENTRY_CREATED_EVENT_RESPONSE:
                    case CACHE_ENTRY_MODIFIED_EVENT_RESPONSE:
                    case CACHE_ENTRY_REMOVED_EVENT_RESPONSE:
                    case CACHE_ENTRY_EXPIRED_EVENT_RESPONSE:
                        if (codec.allowOperationsAndEvents()) {
                            operation = messageId == 0 ? null : incomplete.get(messageId);
                        } else if (incomplete.size() == 1) {
                            operation = incomplete.values().iterator().next();
                            messageId = operation.header().messageId();
                        } else if (incomplete.size() > 1) {
                            throw new IllegalStateException("Too many incomplete operations: " + incomplete);
                        } else {
                            operation = null;
                            messageId = 0;
                        }
                        // complete earlier.
                        if (operation != null && !(operation instanceof AddClientListenerOperation)) {
                            throw HOTROD.operationIsNotAddClientListener(messageId, operation.toString());
                        } else if (log.isTraceEnabled()) {
                            log.tracef("Received event for request %d", messageId, operation);
                        }
                        checkpoint(State.READ_CACHE_EVENT);
                        // the loop in HintedReplayingDecoder will call decode again
                        return;
                    case COUNTER_EVENT_RESPONSE:
                        checkpoint(State.READ_COUNTER_EVENT);
                        // the loop in HintedReplayingDecoder will call decode again
                        return;
                }
                if (messageId == 0) {
                    // let's read the header even at this stage; it should throw an error and the other throw statement
                    // won't be reached
                    codec.readHeader(in, receivedOpCode, null, channelFactory, ctx.channel().remoteAddress());
                    throw new IllegalStateException("Should be never reached");
                }
                // we can remove the operation at this point since we'll read no more in this state
                operation = incomplete.remove(messageId);
                if (operation == null) {
                    throw HOTROD.unknownMessageId(messageId);
                }
                if (log.isTraceEnabled()) {
                    log.tracef("Received response for request %d, %s", messageId, operation);
                }
                checkpoint(State.READ_HEADER);
            // fall through
            case READ_HEADER:
                if (log.isTraceEnabled()) {
                    log.tracef("Decoding header for message %s", HotRodConstants.Names.of(receivedOpCode));
                }
                status = codec.readHeader(in, receivedOpCode, operation.header(), channelFactory, ctx.channel().remoteAddress());
                checkpoint(State.READ_PAYLOAD);
            // fall through
            case READ_PAYLOAD:
                if (log.isTraceEnabled()) {
                    log.tracef("Decoding payload for message %s", HotRodConstants.Names.of(receivedOpCode));
                }
                operation.acceptResponse(in, status, this);
                checkpoint(State.READ_MESSAGE_ID);
                break;
            case READ_CACHE_EVENT:
                if (log.isTraceEnabled()) {
                    log.tracef("Decoding cache event %s", HotRodConstants.Names.of(receivedOpCode));
                }
                AbstractClientEvent cacheEvent;
                try {
                    cacheEvent = codec.readCacheEvent(in, listenerNotifier::getCacheDataFormat, receivedOpCode, configuration.getClassAllowList(), ctx.channel().remoteAddress());
                } catch (Signal signal) {
                    throw signal;
                } catch (Throwable t) {
                    log.unableToReadEventFromServer(t, ctx.channel().remoteAddress());
                    throw t;
                }
                if (operation != null) {
                    ((AddClientListenerOperation) operation).postponeTimeout(ctx.channel());
                }
                invokeEvent(cacheEvent.getListenerId(), cacheEvent);
                checkpoint(State.READ_MESSAGE_ID);
                break;
            case READ_COUNTER_EVENT:
                if (log.isTraceEnabled()) {
                    log.tracef("Decoding counter event %s", HotRodConstants.Names.of(receivedOpCode));
                }
                HotRodCounterEvent counterEvent;
                try {
                    counterEvent = codec.readCounterEvent(in);
                } catch (Signal signal) {
                    throw signal;
                } catch (Throwable t) {
                    HOTROD.unableToReadEventFromServer(t, ctx.channel().remoteAddress());
                    throw t;
                }
                invokeEvent(counterEvent.getListenerId(), counterEvent);
                checkpoint(State.READ_MESSAGE_ID);
                break;
        }
    } catch (Signal signal) {
        throw signal;
    } catch (Exception e) {
        // If this is server error make sure to restart the state of decoder
        checkpoint(State.READ_MESSAGE_ID);
        throw e;
    }
}
Also used : AbstractClientEvent(org.infinispan.client.hotrod.event.impl.AbstractClientEvent) Signal(io.netty.util.Signal) HotRodCounterEvent(org.infinispan.client.hotrod.counter.impl.HotRodCounterEvent) AddClientListenerOperation(org.infinispan.client.hotrod.impl.operations.AddClientListenerOperation) TransportException(org.infinispan.client.hotrod.exceptions.TransportException)

Example 5 with Signal

use of io.netty.util.Signal in project infinispan by infinispan.

the class HintedReplayingDecoder method callDecode.

@Override
protected void callDecode(ChannelHandlerContext ctx, ByteBuf in, List<Object> xxx) {
    if (in.readableBytes() < requiredReadableBytes) {
        // noop, wait for further reads
        return;
    }
    replayable.setCumulation(in);
    try {
        while (in.isReadable() && !ctx.isRemoved() && ctx.channel().isActive()) {
            checkpoint = in.readerIndex();
            try {
                decode(ctx, replayable, NO_WRITE_LIST);
                requiredReadableBytes = 0;
            // TODO: unset cumulation
            } catch (Signal replay) {
                replay.expect(REPLAY);
                // See https://github.com/netty/netty/issues/1664
                if (ctx.isRemoved()) {
                    break;
                }
                // Return to the checkpoint (or oldPosition) and retry.
                int checkpoint = this.checkpoint;
                if (checkpoint >= 0) {
                    in.readerIndex(checkpoint);
                } else {
                // Called by cleanup() - no need to maintain the readerIndex
                // anymore because the buffer has been released already.
                }
                break;
            } catch (Throwable t) {
                requiredReadableBytes = 0;
                throw t;
            }
        }
    } catch (DecoderException e) {
        throw e;
    } catch (Throwable cause) {
        throw new DecoderException(cause);
    }
}
Also used : DecoderException(io.netty.handler.codec.DecoderException) Signal(io.netty.util.Signal)

Aggregations

Signal (io.netty.util.Signal)5 ByteBuf (io.netty.buffer.ByteBuf)3 Test (org.junit.jupiter.api.Test)3 DecoderException (io.netty.handler.codec.DecoderException)1 HotRodCounterEvent (org.infinispan.client.hotrod.counter.impl.HotRodCounterEvent)1 AbstractClientEvent (org.infinispan.client.hotrod.event.impl.AbstractClientEvent)1 TransportException (org.infinispan.client.hotrod.exceptions.TransportException)1 AddClientListenerOperation (org.infinispan.client.hotrod.impl.operations.AddClientListenerOperation)1