io.netty.channel.ChannelException Java Examples

The following examples show how to use io.netty.channel.ChannelException. 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: NioSctpChannel.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new instance
 *
 * @param parent        the {@link Channel} which is the parent of this {@link NioSctpChannel}
 *                      or {@code null}.
 * @param sctpChannel   the underlying {@link SctpChannel}
 */
public NioSctpChannel(Channel parent, SctpChannel sctpChannel) {
    super(parent, sctpChannel, SelectionKey.OP_READ);
    try {
        sctpChannel.configureBlocking(false);
        config = new NioSctpChannelConfig(this, sctpChannel);
        notificationHandler = new SctpNotificationHandler(this);
    } catch (IOException e) {
        try {
            sctpChannel.close();
        } catch (IOException e2) {
            if (logger.isWarnEnabled()) {
                logger.warn(
                        "Failed to close a partially initialized sctp channel.", e2);
            }
        }

        throw new ChannelException("Failed to enter non-blocking mode.", e);
    }
}
 
Example #2
Source File: NioUdtByteConnectorChannel.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
public NioUdtByteConnectorChannel(final Channel parent, final SocketChannelUDT channelUDT) {
    super(parent, channelUDT);
    try {
        channelUDT.configureBlocking(false);
        switch (channelUDT.socketUDT().status()) {
        case INIT:
        case OPENED:
            config = new DefaultUdtChannelConfig(this, channelUDT, true);
            break;
        default:
            config = new DefaultUdtChannelConfig(this, channelUDT, false);
            break;
        }
    } catch (final Exception e) {
        try {
            channelUDT.close();
        } catch (final Exception e2) {
            if (logger.isWarnEnabled()) {
                logger.warn("Failed to close channel.", e2);
            }
        }
        throw new ChannelException("Failed to configure channel.", e);
    }
}
 
Example #3
Source File: DefaultDatagramChannelConfig.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
public DatagramChannelConfig setBroadcast(boolean broadcast) {
    try {
        // See: https://github.com/netty/netty/issues/576
        if (broadcast &&
            !javaSocket.getLocalAddress().isAnyLocalAddress() &&
            !PlatformDependent.isWindows() && !PlatformDependent.maybeSuperUser()) {
            // Warn a user about the fact that a non-root user can't receive a
            // broadcast packet on *nix if the socket is bound on non-wildcard address.
            logger.warn(
                    "A non-root user can't receive a broadcast packet if the socket " +
                    "is not bound to a wildcard address; setting the SO_BROADCAST flag " +
                    "anyway as requested on the socket which is bound to " +
                    javaSocket.getLocalSocketAddress() + '.');
        }

        javaSocket.setBroadcast(broadcast);
    } catch (SocketException e) {
        throw new ChannelException(e);
    }
    return this;
}
 
Example #4
Source File: OioDatagramChannel.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new instance from the given {@link MulticastSocket}.
 *
 * @param socket    the {@link MulticastSocket} which is used by this instance
 */
public OioDatagramChannel(MulticastSocket socket) {
    super(null);

    boolean success = false;
    try {
        socket.setSoTimeout(SO_TIMEOUT);
        socket.setBroadcast(false);
        success = true;
    } catch (SocketException e) {
        throw new ChannelException(
                "Failed to configure the datagram socket timeout.", e);
    } finally {
        if (!success) {
            socket.close();
        }
    }

    this.socket = socket;
    config = new DefaultOioDatagramChannelConfig(this, socket);
}
 
Example #5
Source File: KQueueSocketChannelConfig.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
public void setSndLowAt(int sndLowAt)  {
    try {
        channel.socket.setSndLowAt(sndLowAt);
    } catch (IOException e) {
        throw new ChannelException(e);
    }
}
 
Example #6
Source File: DefaultDatagramChannelConfig.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public int getSendBufferSize() {
    try {
        return javaSocket.getSendBufferSize();
    } catch (SocketException e) {
        throw new ChannelException(e);
    }
}
 
Example #7
Source File: EpollSocketChannelConfig.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
 * Returns {@code true} if <a href="http://man7.org/linux/man-pages/man7/ip.7.html">IP_TRANSPARENT</a> is enabled,
 * {@code false} otherwise.
 */
public boolean isIpTransparent() {
    try {
        return channel.socket.isIpTransparent();
    } catch (IOException e) {
        throw new ChannelException(e);
    }
}
 
Example #8
Source File: KQueueChannelConfigTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testOptionSetThrowsChannelException() throws Exception {
    KQueueSocketChannel channel = new KQueueSocketChannel();
    channel.config().setKeepAlive(true);
    channel.fd().close();
    try {
        channel.config().setKeepAlive(true);
        fail();
    } catch (ChannelException e) {
        // expected
    }
}
 
Example #9
Source File: KQueueServerSocketChannelConfig.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
public KQueueServerSocketChannelConfig setReusePort(boolean reusePort) {
    try {
        channel.socket.setReusePort(reusePort);
        return this;
    } catch (IOException e) {
        throw new ChannelException(e);
    }
}
 
Example #10
Source File: DefaultSctpServerChannelConfig.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public SctpServerChannelConfig setSendBufferSize(int sendBufferSize) {
    try {
        javaChannel.setOption(SctpStandardSocketOptions.SO_SNDBUF, sendBufferSize);
    } catch (IOException e) {
        throw new ChannelException(e);
    }
    return this;
}
 
Example #11
Source File: EpollServerSocketChannelConfig.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
 * If {@code true} is used <a href="http://man7.org/linux/man-pages/man7/ip.7.html">IP_TRANSPARENT</a> is enabled,
 * {@code false} for disable it. Default is disabled.
 */
public EpollServerSocketChannelConfig setIpTransparent(boolean transparent) {
    try {
        channel.socket.setIpTransparent(transparent);
        return this;
    } catch (IOException e) {
        throw new ChannelException(e);
    }
}
 
Example #12
Source File: OioEventLoopTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testTooManyServerChannels() throws Exception {
    EventLoopGroup g = new OioEventLoopGroup(1);
    ServerBootstrap b = new ServerBootstrap();
    b.channel(OioServerSocketChannel.class);
    b.group(g);
    b.childHandler(new ChannelInboundHandlerAdapter());
    ChannelFuture f1 = b.bind(0);
    f1.sync();

    ChannelFuture f2 = b.bind(0);
    f2.await();

    assertThat(f2.cause(), is(instanceOf(ChannelException.class)));
    assertThat(f2.cause().getMessage().toLowerCase(), containsString("too many channels"));

    final CountDownLatch notified = new CountDownLatch(1);
    f2.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            notified.countDown();
        }
    });

    notified.await();
    g.shutdownGracefully();
}
 
Example #13
Source File: OioEventLoopTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testTooManyClientChannels() throws Exception {
    EventLoopGroup g = new OioEventLoopGroup(1);
    ServerBootstrap sb = new ServerBootstrap();
    sb.channel(OioServerSocketChannel.class);
    sb.group(g);
    sb.childHandler(new ChannelInboundHandlerAdapter());
    ChannelFuture f1 = sb.bind(0);
    f1.sync();

    Bootstrap cb = new Bootstrap();
    cb.channel(OioSocketChannel.class);
    cb.group(g);
    cb.handler(new ChannelInboundHandlerAdapter());
    ChannelFuture f2 = cb.connect(NetUtil.LOCALHOST, ((InetSocketAddress) f1.channel().localAddress()).getPort());
    f2.await();

    assertThat(f2.cause(), is(instanceOf(ChannelException.class)));
    assertThat(f2.cause().getMessage().toLowerCase(), containsString("too many channels"));

    final CountDownLatch notified = new CountDownLatch(1);
    f2.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            notified.countDown();
        }
    });

    notified.await();
    g.shutdownGracefully();
}
 
Example #14
Source File: EpollDatagramChannelConfig.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public EpollDatagramChannelConfig setReuseAddress(boolean reuseAddress) {
    try {
        datagramChannel.socket.setReuseAddress(reuseAddress);
        return this;
    } catch (IOException e) {
        throw new ChannelException(e);
    }
}
 
Example #15
Source File: EpollSocketChannelConfigTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetOptionWhenClosed() {
    ch.close().syncUninterruptibly();
    try {
        ch.config().setSoLinger(0);
        fail();
    } catch (ChannelException e) {
        assertTrue(e.getCause() instanceof ClosedChannelException);
    }
}
 
Example #16
Source File: KQueueDatagramChannelConfig.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public KQueueDatagramChannelConfig setTrafficClass(int trafficClass) {
    try {
        datagramChannel.socket.setTrafficClass(trafficClass);
        return this;
    } catch (IOException e) {
        throw new ChannelException(e);
    }
}
 
Example #17
Source File: DefaultSctpServerChannelConfig.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public SctpStandardSocketOptions.InitMaxStreams getInitMaxStreams() {
    try {
        return javaChannel.getOption(SctpStandardSocketOptions.SCTP_INIT_MAXSTREAMS);
    } catch (IOException e) {
        throw new ChannelException(e);
    }
}
 
Example #18
Source File: DefaultDatagramChannelConfig.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public DatagramChannelConfig setReceiveBufferSize(int receiveBufferSize) {
    try {
        javaSocket.setReceiveBufferSize(receiveBufferSize);
    } catch (SocketException e) {
        throw new ChannelException(e);
    }
    return this;
}
 
Example #19
Source File: KQueueServerChannelConfig.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
public KQueueServerChannelConfig setReuseAddress(boolean reuseAddress) {
    try {
        channel.socket.setReuseAddress(reuseAddress);
        return this;
    } catch (IOException e) {
        throw new ChannelException(e);
    }
}
 
Example #20
Source File: DefaultDatagramChannelConfig.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isReuseAddress() {
    try {
        return javaSocket.getReuseAddress();
    } catch (SocketException e) {
        throw new ChannelException(e);
    }
}
 
Example #21
Source File: EpollSocketChannelConfig.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public int getTrafficClass() {
    try {
        return channel.socket.getTrafficClass();
    } catch (IOException e) {
        throw new ChannelException(e);
    }
}
 
Example #22
Source File: DefaultDatagramChannelConfig.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public NetworkInterface getNetworkInterface() {
    if (javaSocket instanceof MulticastSocket) {
        try {
            return ((MulticastSocket) javaSocket).getNetworkInterface();
        } catch (SocketException e) {
            throw new ChannelException(e);
        }
    } else {
        throw new UnsupportedOperationException();
    }
}
 
Example #23
Source File: DefaultDatagramChannelConfig.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public DatagramChannelConfig setLoopbackModeDisabled(boolean loopbackModeDisabled) {
    if (javaSocket instanceof MulticastSocket) {
        try {
            ((MulticastSocket) javaSocket).setLoopbackMode(loopbackModeDisabled);
        } catch (SocketException e) {
            throw new ChannelException(e);
        }
    } else {
        throw new UnsupportedOperationException();
    }
    return this;
}
 
Example #24
Source File: DefaultDatagramChannelConfig.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isLoopbackModeDisabled() {
    if (javaSocket instanceof MulticastSocket) {
        try {
            return ((MulticastSocket) javaSocket).getLoopbackMode();
        } catch (SocketException e) {
            throw new ChannelException(e);
        }
    } else {
        throw new UnsupportedOperationException();
    }
}
 
Example #25
Source File: EpollDatagramChannelConfig.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public EpollDatagramChannelConfig setReceiveBufferSize(int receiveBufferSize) {
    try {
        datagramChannel.socket.setReceiveBufferSize(receiveBufferSize);
        return this;
    } catch (IOException e) {
        throw new ChannelException(e);
    }
}
 
Example #26
Source File: EpollSocketChannelConfig.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
 * Get the {@code TCP_KEEPINTVL} option on the socket. See {@code man 7 tcp} for more details.
 */
public int getTcpKeepIntvl() {
    try {
        return channel.socket.getTcpKeepIntvl();
    } catch (IOException e) {
        throw new ChannelException(e);
    }
}
 
Example #27
Source File: DefaultDatagramChannelConfig.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isBroadcast() {
    try {
        return javaSocket.getBroadcast();
    } catch (SocketException e) {
        throw new ChannelException(e);
    }
}
 
Example #28
Source File: EpollSocketChannelConfig.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public EpollSocketChannelConfig setSendBufferSize(int sendBufferSize) {
    try {
        channel.socket.setSendBufferSize(sendBufferSize);
        calculateMaxBytesPerGatheringWrite();
        return this;
    } catch (IOException e) {
        throw new ChannelException(e);
    }
}
 
Example #29
Source File: KQueueSocketChannelConfigTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetOptionWhenClosed() {
    ch.close().syncUninterruptibly();
    try {
        ch.config().setSoLinger(0);
        fail();
    } catch (ChannelException e) {
        assertTrue(e.getCause() instanceof ClosedChannelException);
    }
}
 
Example #30
Source File: DefaultServerSocketChannelConfig.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public ServerSocketChannelConfig setReuseAddress(boolean reuseAddress) {
    try {
        javaSocket.setReuseAddress(reuseAddress);
    } catch (SocketException e) {
        throw new ChannelException(e);
    }
    return this;
}