Java Code Examples for io.netty.util.internal.SocketUtils#connect()

The following examples show how to use io.netty.util.internal.SocketUtils#connect() . 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: NioUdtMessageConnectorChannel.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean doConnect(final SocketAddress remoteAddress,
        final SocketAddress localAddress) throws Exception {
    doBind(localAddress != null? localAddress : new InetSocketAddress(0));
    boolean success = false;
    try {
        final boolean connected = SocketUtils.connect(javaChannel(), remoteAddress);
        if (!connected) {
            selectionKey().interestOps(
                    selectionKey().interestOps() | OP_CONNECT);
        }
        success = true;
        return connected;
    } finally {
        if (!success) {
            doClose();
        }
    }
}
 
Example 2
Source File: NioUdtByteConnectorChannel.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean doConnect(final SocketAddress remoteAddress,
                            final SocketAddress localAddress) throws Exception {
    doBind(localAddress != null? localAddress : new InetSocketAddress(0));
    boolean success = false;
    try {
        final boolean connected = SocketUtils.connect(javaChannel(), remoteAddress);
        if (!connected) {
            selectionKey().interestOps(
                    selectionKey().interestOps() | OP_CONNECT);
        }
        success = true;
        return connected;
    } finally {
        if (!success) {
            doClose();
        }
    }
}
 
Example 3
Source File: OioSocketChannel.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
protected void doConnect(SocketAddress remoteAddress,
        SocketAddress localAddress) throws Exception {
    if (localAddress != null) {
        SocketUtils.bind(socket, localAddress);
    }

    boolean success = false;
    try {
        SocketUtils.connect(socket, remoteAddress, config().getConnectTimeoutMillis());
        activate(socket.getInputStream(), socket.getOutputStream());
        success = true;
    } catch (SocketTimeoutException e) {
        ConnectTimeoutException cause = new ConnectTimeoutException("connection timed out: " + remoteAddress);
        cause.setStackTrace(e.getStackTrace());
        throw cause;
    } finally {
        if (!success) {
            doClose();
        }
    }
}
 
Example 4
Source File: NioSocketChannel.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
    protected boolean doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception {
        if (localAddress != null) {
//            调用java底层的socketChannel的bind方法
            doBind0(localAddress);
        }

        boolean success = false;
        try {
//            调用java底层的socketChannel的connect方法
            boolean connected = SocketUtils.connect(javaChannel(), remoteAddress);
            if (!connected) {
//                如果没有连接成功注册OP_CONNECT事件
                selectionKey().interestOps(SelectionKey.OP_CONNECT);
            }
            success = true;
            return connected;
        } finally {
            if (!success) {
//                如果连接失败调用java底层channel close
                doClose();
            }
        }
    }
 
Example 5
Source File: SocketShutdownOutputByPeerTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
public void testShutdownOutput(ServerBootstrap sb) throws Throwable {
    TestHandler h = new TestHandler();
    Socket s = new Socket();
    Channel sc = null;
    try {
        sc = sb.childHandler(h).childOption(ChannelOption.ALLOW_HALF_CLOSURE, true).bind().sync().channel();

        SocketUtils.connect(s, sc.localAddress(), 10000);
        s.getOutputStream().write(1);

        assertEquals(1, (int) h.queue.take());

        assertTrue(h.ch.isOpen());
        assertTrue(h.ch.isActive());
        assertFalse(h.ch.isInputShutdown());
        assertFalse(h.ch.isOutputShutdown());

        s.shutdownOutput();

        h.halfClosure.await();

        assertTrue(h.ch.isOpen());
        assertTrue(h.ch.isActive());
        assertTrue(h.ch.isInputShutdown());
        assertFalse(h.ch.isOutputShutdown());
        assertEquals(1, h.closure.getCount());
        Thread.sleep(100);
        assertEquals(1, h.halfClosureCount.intValue());
    } finally {
        if (sc != null) {
            sc.close();
        }
        s.close();
    }
}
 
Example 6
Source File: SocketShutdownOutputByPeerTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
public void testShutdownOutputWithoutOption(ServerBootstrap sb) throws Throwable {
    TestHandler h = new TestHandler();
    Socket s = new Socket();
    Channel sc = null;
    try {
        sc = sb.childHandler(h).bind().sync().channel();

        SocketUtils.connect(s, sc.localAddress(), 10000);
        s.getOutputStream().write(1);

        assertEquals(1, (int) h.queue.take());

        assertTrue(h.ch.isOpen());
        assertTrue(h.ch.isActive());
        assertFalse(h.ch.isInputShutdown());
        assertFalse(h.ch.isOutputShutdown());

        s.shutdownOutput();

        h.closure.await();

        assertFalse(h.ch.isOpen());
        assertFalse(h.ch.isActive());
        assertTrue(h.ch.isInputShutdown());
        assertTrue(h.ch.isOutputShutdown());

        assertEquals(1, h.halfClosure.getCount());
        Thread.sleep(100);
        assertEquals(0, h.halfClosureCount.intValue());
    } finally {
        if (sc != null) {
            sc.close();
        }
        s.close();
    }
}