Java Code Examples for io.netty.channel.ChannelPromise#setFailure()

The following examples show how to use io.netty.channel.ChannelPromise#setFailure() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultHttp2FrameWriter.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
public ChannelFuture writeSettings(ChannelHandlerContext ctx, Http2Settings settings,
        ChannelPromise promise) {
    try {
        checkNotNull(settings, "settings");
        int payloadLength = SETTING_ENTRY_LENGTH * settings.size();
        ByteBuf buf = ctx.alloc().buffer(FRAME_HEADER_LENGTH + settings.size() * SETTING_ENTRY_LENGTH);
        writeFrameHeaderInternal(buf, payloadLength, SETTINGS, new Http2Flags(), 0);
        for (Http2Settings.PrimitiveEntry<Long> entry : settings.entries()) {
            buf.writeChar(entry.key());
            buf.writeInt(entry.value().intValue());
        }
        return ctx.write(buf, promise);
    } catch (Throwable t) {
        return promise.setFailure(t);
    }
}
 
Example 2
Source File: NioSctpServerChannel.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Override
public ChannelFuture bindAddress(final InetAddress localAddress, final ChannelPromise promise) {
    if (eventLoop().inEventLoop()) {
        try {
            javaChannel().bindAddress(localAddress);
            promise.setSuccess();
        } catch (Throwable t) {
            promise.setFailure(t);
        }
    } else {
        eventLoop().execute(new Runnable() {
            @Override
            public void run() {
                bindAddress(localAddress, promise);
            }
        });
    }
    return promise;
}
 
Example 3
Source File: EmbeddedChannelWriteAccumulatingHandlerContext.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
public final ChannelFuture writeAndFlush(Object msg, ChannelPromise promise) {
    try {
        if (msg instanceof ByteBuf) {
            ByteBuf buf = (ByteBuf) msg;
            if (cumulation == null) {
                cumulation = buf;
            } else {
                cumulation = cumulator.cumulate(alloc(), cumulation, buf);
            }
            promise.setSuccess();
        } else {
            channel().writeAndFlush(msg, promise);
        }
    } catch (Exception e) {
        promise.setFailure(e);
        handleException(e);
    }
    return promise;
}
 
Example 4
Source File: OioSctpServerChannel.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
public ChannelFuture unbindAddress(final InetAddress localAddress, final ChannelPromise promise) {
    if (eventLoop().inEventLoop()) {
        try {
            sch.unbindAddress(localAddress);
            promise.setSuccess();
        } catch (Throwable t) {
            promise.setFailure(t);
        }
    } else {
        eventLoop().execute(new Runnable() {
            @Override
            public void run() {
                unbindAddress(localAddress, promise);
            }
        });
    }
    return promise;
}
 
Example 5
Source File: StreamBufferingEncoder.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
public ChannelFuture writeHeaders(ChannelHandlerContext ctx, int streamId, Http2Headers headers,
                                  int streamDependency, short weight, boolean exclusive,
                                  int padding, boolean endOfStream, ChannelPromise promise) {
    if (closed) {
        return promise.setFailure(new Http2ChannelClosedException());
    }
    if (isExistingStream(streamId) || connection().goAwayReceived()) {
        return super.writeHeaders(ctx, streamId, headers, streamDependency, weight,
                exclusive, padding, endOfStream, promise);
    }
    if (canCreateStream()) {
        return super.writeHeaders(ctx, streamId, headers, streamDependency, weight,
                exclusive, padding, endOfStream, promise);
    }
    PendingStream pendingStream = pendingStreams.get(streamId);
    if (pendingStream == null) {
        pendingStream = new PendingStream(ctx, streamId);
        pendingStreams.put(streamId, pendingStream);
    }
    pendingStream.frames.add(new HeadersFrame(headers, streamDependency, weight, exclusive,
            padding, endOfStream, promise));
    return promise;
}
 
Example 6
Source File: EpollDatagramChannel.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
public ChannelFuture block(
        final InetAddress multicastAddress, final NetworkInterface networkInterface,
        final InetAddress sourceToBlock, final ChannelPromise promise) {
    if (multicastAddress == null) {
        throw new NullPointerException("multicastAddress");
    }
    if (sourceToBlock == null) {
        throw new NullPointerException("sourceToBlock");
    }

    if (networkInterface == null) {
        throw new NullPointerException("networkInterface");
    }
    promise.setFailure(new UnsupportedOperationException("Multicast not supported"));
    return promise;
}
 
Example 7
Source File: OioDatagramChannel.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public ChannelFuture joinGroup(InetAddress multicastAddress, ChannelPromise promise) {
    ensureBound();
    try {
        socket.joinGroup(multicastAddress);
        promise.setSuccess();
    } catch (IOException e) {
        promise.setFailure(e);
    }
    return promise;
}
 
Example 8
Source File: OioDatagramChannel.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
public ChannelFuture joinGroup(
        InetAddress multicastAddress, NetworkInterface networkInterface, InetAddress source,
        ChannelPromise promise) {
    promise.setFailure(new UnsupportedOperationException());
    return promise;
}
 
Example 9
Source File: Http2MultiplexCodec.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private void firstWriteComplete(ChannelFuture future, ChannelPromise promise) {
    Throwable cause = future.cause();
    if (cause == null) {
        // As we just finished our first write which made the stream-id valid we need to re-evaluate
        // the writability of the channel.
        writabilityChanged(Http2MultiplexCodec.this.isWritable(stream));
        promise.setSuccess();
    } else {
        promise.setFailure(wrapStreamClosedError(cause));
        // If the first write fails there is not much we can do, just close
        closeForcibly();
    }
}
 
Example 10
Source File: EmbeddedChannel.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
 * Checks for the presence of an {@link Exception}.
 */
private ChannelFuture checkException(ChannelPromise promise) {
  Throwable t = lastException;
  if (t != null) {
    lastException = null;

    if (promise.isVoid()) {
        PlatformDependent.throwException(t);
    }

    return promise.setFailure(t);
  }

  return promise.setSuccess();
}
 
Example 11
Source File: SslHandler.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public void write(final ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (!(msg instanceof ByteBuf)) {
        UnsupportedMessageTypeException exception = new UnsupportedMessageTypeException(msg, ByteBuf.class);
        ReferenceCountUtil.safeRelease(msg);
        promise.setFailure(exception);
    } else if (pendingUnencryptedWrites == null) {
        ReferenceCountUtil.safeRelease(msg);
        promise.setFailure(newPendingWritesNullException());
    } else {
        pendingUnencryptedWrites.add((ByteBuf) msg, promise);
    }
}
 
Example 12
Source File: Http2FrameCodec.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private static void notifyHeaderWritePromise(ChannelFuture future, ChannelPromise promise) {
    Throwable cause = future.cause();
    if (cause == null) {
        promise.setSuccess();
    } else {
        promise.setFailure(cause);
    }
}
 
Example 13
Source File: Http2MultiplexCodec.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public void connect(final SocketAddress remoteAddress,
                    SocketAddress localAddress, final ChannelPromise promise) {
    if (!promise.setUncancellable()) {
        return;
    }
    promise.setFailure(new UnsupportedOperationException());
}
 
Example 14
Source File: AmbrySendToHttp2Adaptor.java    From ambry with Apache License 2.0 5 votes vote down vote up
/**
 * Handles conversion of {@link Send} to HTTP/2 frames.
 */
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
  if (!ctx.channel().isOpen()) {
    logger.debug("Channel closed when write. Channel: {}", ctx.channel());
    promise.setFailure(new ChannelException("Channel has been closed when write."));
  }
  if (!(msg instanceof Send)) {
    ctx.write(msg, promise);
    return;
  }
  Send send = (Send) msg;

  Http2Headers http2Headers;
  if (forServer) {
    logger.trace("Write content to channel as server {}", ctx.channel());
    http2Headers = new DefaultHttp2Headers().status(HttpResponseStatus.OK.codeAsText());
  } else {
    logger.trace("Write content to channel as client {}", ctx.channel());
    http2Headers = new DefaultHttp2Headers().method(HttpMethod.POST.asciiName()).scheme("https").path("/");
  }
  DefaultHttp2HeadersFrame headersFrame = new DefaultHttp2HeadersFrame(http2Headers, false);
  ctx.write(headersFrame);
  // TODO: Use {@link RetainingAsyncWritableChannel} after writeTo(AsyncWritableChannel channel, Callback<Long> callback) is fully implemented.
  ByteBufChannel byteBufChannel = new ByteBufChannel();
  try {
    send.writeTo(byteBufChannel);
  } catch (IOException e) {
    promise.setFailure(e);
    return;
  }
  DefaultHttp2DataFrame dataFrame = new DefaultHttp2DataFrame(byteBufChannel.getBuf(), true);
  // Caller should call writeAndFlush().
  ctx.write(dataFrame, promise);
}
 
Example 15
Source File: OioDatagramChannel.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
public ChannelFuture joinGroup(
        InetSocketAddress multicastAddress, NetworkInterface networkInterface,
        ChannelPromise promise) {
    ensureBound();
    try {
        socket.joinGroup(multicastAddress, networkInterface);
        promise.setSuccess();
    } catch (IOException e) {
        promise.setFailure(e);
    }
    return promise;
}
 
Example 16
Source File: WrappingXnioSocketChannel.java    From netty-xnio-transport with Apache License 2.0 4 votes vote down vote up
@Override
public void connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) {
    promise.setFailure(new UnsupportedOperationException("Wrapped XNIO Channel"));
}
 
Example 17
Source File: AbstractEpollServerChannel.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@Override
public void connect(SocketAddress socketAddress, SocketAddress socketAddress2, ChannelPromise channelPromise) {
    // Connect not supported by ServerChannel implementations
    channelPromise.setFailure(new UnsupportedOperationException());
}
 
Example 18
Source File: OioDatagramChannel.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@Override
public ChannelFuture block(InetAddress multicastAddress,
        InetAddress sourceToBlock, ChannelPromise promise) {
    promise.setFailure(new UnsupportedOperationException());
    return promise;
}
 
Example 19
Source File: JZlibEncoder.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
private ChannelFuture finishEncode(ChannelHandlerContext ctx, ChannelPromise promise) {
    if (finished) {
        promise.setSuccess();
        return promise;
    }
    finished = true;

    ByteBuf footer;
    try {
        // Configure input.
        z.next_in = EmptyArrays.EMPTY_BYTES;
        z.next_in_index = 0;
        z.avail_in = 0;

        // Configure output.
        byte[] out = new byte[32]; // room for ADLER32 + ZLIB / CRC32 + GZIP header
        z.next_out = out;
        z.next_out_index = 0;
        z.avail_out = out.length;

        // Write the ADLER32 checksum (stream footer).
        int resultCode = z.deflate(JZlib.Z_FINISH);
        if (resultCode != JZlib.Z_OK && resultCode != JZlib.Z_STREAM_END) {
            promise.setFailure(ZlibUtil.deflaterException(z, "compression failure", resultCode));
            return promise;
        } else if (z.next_out_index != 0) {
            footer = Unpooled.wrappedBuffer(out, 0, z.next_out_index);
        } else {
            footer = Unpooled.EMPTY_BUFFER;
        }
    } finally {
        z.deflateEnd();

        // Deference the external references explicitly to tell the VM that
        // the allocated byte arrays are temporary so that the call stack
        // can be utilized.
        // I'm not sure if the modern VMs do this optimization though.
        z.next_in = null;
        z.next_out = null;
    }
    return ctx.writeAndFlush(footer, promise);
}
 
Example 20
Source File: FailedChannel.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public void connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) {
    promise.setFailure(new UnsupportedOperationException());
}