Search in sources :

Example 16 with ChannelPromise

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPromise in project qpid-broker-j by apache.

the class AbstractFrameTransport method sendBytes.

public ListenableFuture<Void> sendBytes(final byte[] bytes) {
    Preconditions.checkState(_channel != null, "Not connected");
    ChannelPromise promise = _channel.newPromise();
    ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
    buffer.writeBytes(bytes);
    _channel.write(buffer, promise);
    return JdkFutureAdapters.listenInPoolThread(promise);
}
Also used : ChannelPromise(io.netty.channel.ChannelPromise) ByteBuf(io.netty.buffer.ByteBuf)

Example 17 with ChannelPromise

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPromise in project qpid-broker-j by apache.

the class AbstractFrameTransport method sendPerformative.

public ListenableFuture<Void> sendPerformative(final Object data) throws Exception {
    Preconditions.checkState(_channel != null, "Not connected");
    ChannelPromise promise = _channel.newPromise();
    _channel.write(data, promise);
    return JdkFutureAdapters.listenInPoolThread(promise);
}
Also used : ChannelPromise(io.netty.channel.ChannelPromise)

Example 18 with ChannelPromise

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPromise in project rskj by rsksmart.

the class NettyTest method pipelineTest.

@Test
public void pipelineTest() {
    final int[] int2 = new int[1];
    final boolean[] exception = new boolean[1];
    final ByteToMessageDecoder decoder2 = new ByteToMessageDecoder() {

        @Override
        protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
            int i = in.readInt();
            System.out.println("decoder2 read int (4 bytes): " + Integer.toHexString(i));
            int2[0] = i;
            if (i == 0)
                out.add("aaa");
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            System.out.println("Decoder2 exception: " + cause);
        }
    };
    final MessageToMessageCodec decoder3 = new MessageToMessageCodec<Object, Object>() {

        @Override
        protected void decode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
            System.out.println("NettyTest.decode: msg = [" + msg + "]");
            if (msg == "aaa") {
                throw new RuntimeException("Test exception 3");
            }
        }

        @Override
        protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
            throw new RuntimeException("Test exception 4");
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            System.out.println("Decoder3 exception: " + cause);
            exception[0] = true;
        }
    };
    final ByteToMessageDecoder decoder1 = new ByteToMessageDecoder() {

        @Override
        protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
            int i = in.readInt();
            System.out.println("decoder1 read int (4 bytes). Needs no more: " + Integer.toHexString(i));
            ctx.pipeline().addAfter("decoder1", "decoder2", decoder2);
            ctx.pipeline().addAfter("decoder2", "decoder3", decoder3);
            ctx.pipeline().remove(this);
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            System.out.println("Decoder1 exception: " + cause);
        }
    };
    ChannelInboundHandlerAdapter initiator = new ChannelInboundHandlerAdapter() {

        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            ctx.pipeline().addFirst("decoder1", decoder1);
            System.out.println("NettyTest.channelActive");
        }
    };
    EmbeddedChannel channel0 = new EmbeddedChannel(new ChannelOutboundHandlerAdapter() {

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            throw new RuntimeException("Test");
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            System.out.println("Exception caught: " + cause);
        }
    });
    EmbeddedChannel channel = new EmbeddedChannel(initiator);
    ByteBuf buffer = Unpooled.buffer();
    buffer.writeInt(0x12345678);
    buffer.writeInt(0xabcdefff);
    channel.writeInbound(buffer);
    Assert.assertEquals(0xabcdefff, int2[0]);
    channel.writeInbound(Unpooled.buffer().writeInt(0));
    Assert.assertTrue(exception[0]);
// Need the following for the exception in outbound handler to be fired
// ctx.writeAndFlush(msg).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
// exception[0] = false;
// channel.writeOutbound("outMsg");
// Assert.assertTrue(exception[0]);
}
Also used : MessageToMessageCodec(io.netty.handler.codec.MessageToMessageCodec) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) ByteToMessageDecoder(io.netty.handler.codec.ByteToMessageDecoder) ByteBuf(io.netty.buffer.ByteBuf) List(java.util.List) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Example 19 with ChannelPromise

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPromise in project async-http-client by AsyncHttpClient.

the class NettyRequestSender method writeRequest.

public <T> void writeRequest(NettyResponseFuture<T> future, Channel channel) {
    NettyRequest nettyRequest = future.getNettyRequest();
    HttpRequest httpRequest = nettyRequest.getHttpRequest();
    AsyncHandler<T> asyncHandler = future.getAsyncHandler();
    // we just let it go and the channelInactive do its work
    if (!Channels.isChannelActive(channel))
        return;
    try {
        if (asyncHandler instanceof TransferCompletionHandler) {
            configureTransferAdapter(asyncHandler, httpRequest);
        }
        boolean writeBody = !future.isDontWriteBodyBecauseExpectContinue() && httpRequest.method() != HttpMethod.CONNECT && nettyRequest.getBody() != null;
        if (!future.isHeadersAlreadyWrittenOnContinue()) {
            try {
                asyncHandler.onRequestSend(nettyRequest);
            } catch (Exception e) {
                LOGGER.error("onRequestSend crashed", e);
                abort(channel, future, e);
                return;
            }
            // if the request has a body, we want to track progress
            if (writeBody) {
                // FIXME does this really work??? the promise is for the request without body!!!
                ChannelProgressivePromise promise = channel.newProgressivePromise();
                ChannelFuture f = channel.write(httpRequest, promise);
                f.addListener(new WriteProgressListener(future, true, 0L));
            } else {
                // we can just track write completion
                ChannelPromise promise = channel.newPromise();
                ChannelFuture f = channel.writeAndFlush(httpRequest, promise);
                f.addListener(new WriteCompleteListener(future));
            }
        }
        if (writeBody)
            nettyRequest.getBody().write(channel, future);
        // don't bother scheduling read timeout if channel became invalid
        if (Channels.isChannelActive(channel)) {
            scheduleReadTimeout(future);
        }
    } catch (Exception e) {
        LOGGER.error("Can't write request", e);
        abort(channel, future, e);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) CONNECT(org.asynchttpclient.util.HttpConstants.Methods.CONNECT) GET(org.asynchttpclient.util.HttpConstants.Methods.GET) EXPECT(io.netty.handler.codec.http.HttpHeaderNames.EXPECT) TransferCompletionHandler(org.asynchttpclient.handler.TransferCompletionHandler) ChannelPromise(io.netty.channel.ChannelPromise) ChannelProgressivePromise(io.netty.channel.ChannelProgressivePromise) FilterException(org.asynchttpclient.filter.FilterException) IOException(java.io.IOException) PoolAlreadyClosedException(org.asynchttpclient.exception.PoolAlreadyClosedException) RemotelyClosedException(org.asynchttpclient.exception.RemotelyClosedException)

Example 20 with ChannelPromise

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPromise in project teiid by teiid.

the class PgBackendProtocol method sendSslResponse.

@Override
public void sendSslResponse() {
    SSLEngine engine = null;
    try {
        if (config != null) {
            engine = config.getServerSSLEngine();
        }
    } catch (IOException e) {
        LogManager.logError(LogConstants.CTX_ODBC, e, RuntimePlugin.Util.gs(secureData() ? RuntimePlugin.Event.TEIID40122 : RuntimePlugin.Event.TEIID40016));
    } catch (GeneralSecurityException e) {
        LogManager.logError(LogConstants.CTX_ODBC, e, RuntimePlugin.Util.gs(secureData() ? RuntimePlugin.Event.TEIID40122 : RuntimePlugin.Event.TEIID40016));
    }
    ByteBuf buffer = Unpooled.buffer(1);
    ChannelPromise promise = this.ctx.newPromise();
    if (engine == null) {
        if (secureData()) {
            sendErrorResponse(RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40124));
            return;
        }
        buffer.writeByte('N');
    } else {
        promise.addListener(new SSLEnabler(engine));
        buffer.writeByte('S');
    }
    this.ctx.writeAndFlush(buffer, promise);
}
Also used : SSLEngine(javax.net.ssl.SSLEngine) GeneralSecurityException(java.security.GeneralSecurityException) ChannelPromise(io.netty.channel.ChannelPromise) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

ChannelPromise (io.netty.channel.ChannelPromise)223 Test (org.junit.jupiter.api.Test)88 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)63 ChannelFuture (io.netty.channel.ChannelFuture)62 DefaultChannelPromise (io.netty.channel.DefaultChannelPromise)58 ByteBuf (io.netty.buffer.ByteBuf)56 ChannelOutboundHandlerAdapter (io.netty.channel.ChannelOutboundHandlerAdapter)30 Test (org.junit.Test)25 Channel (io.netty.channel.Channel)23 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)22 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)22 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)21 ClosedChannelException (java.nio.channels.ClosedChannelException)20 ChannelFutureListener (io.netty.channel.ChannelFutureListener)19 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)18 InvocationOnMock (org.mockito.invocation.InvocationOnMock)18 AsciiString (io.netty.util.AsciiString)15 IOException (java.io.IOException)14 CountDownLatch (java.util.concurrent.CountDownLatch)13 Bootstrap (io.netty.bootstrap.Bootstrap)12