Search in sources :

Example 6 with AlreadyConnectedException

use of java.nio.channels.AlreadyConnectedException in project netty by netty.

the class EpollSocketChannel method doConnect.

@Override
protected boolean doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception {
    if (localAddress != null) {
        checkResolvable((InetSocketAddress) localAddress);
    }
    InetSocketAddress remoteAddr = (InetSocketAddress) remoteAddress;
    checkResolvable(remoteAddr);
    if (remote != null) {
        // later.
        throw new AlreadyConnectedException();
    }
    boolean connected = super.doConnect(remoteAddress, localAddress);
    if (connected) {
        remote = computeRemoteAddr(remoteAddr, fd().remoteAddress());
    } else {
        // Store for later usage in doFinishConnect()
        requestedRemote = remoteAddr;
    }
    // We always need to set the localAddress even if not connected yet as the bind already took place.
    //
    // See https://github.com/netty/netty/issues/3463
    local = fd().localAddress();
    return connected;
}
Also used : AlreadyConnectedException(java.nio.channels.AlreadyConnectedException) InetSocketAddress(java.net.InetSocketAddress)

Example 7 with AlreadyConnectedException

use of java.nio.channels.AlreadyConnectedException in project netty by netty.

the class SocketMultipleConnectTest method testMultipleConnect.

public void testMultipleConnect(ServerBootstrap sb, Bootstrap cb) throws Exception {
    Channel sc = null;
    Channel cc = null;
    try {
        sb.childHandler(new ChannelInboundHandlerAdapter());
        sc = sb.bind(0).syncUninterruptibly().channel();
        cb.handler(new ChannelInboundHandlerAdapter());
        cc = cb.register().syncUninterruptibly().channel();
        cc.connect(sc.localAddress()).syncUninterruptibly();
        ChannelFuture connectFuture2 = cc.connect(sc.localAddress()).await();
        assertTrue(connectFuture2.cause() instanceof AlreadyConnectedException);
    } finally {
        if (cc != null) {
            cc.close();
        }
        if (sc != null) {
            sc.close();
        }
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) AlreadyConnectedException(java.nio.channels.AlreadyConnectedException) Channel(io.netty.channel.Channel) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 8 with AlreadyConnectedException

use of java.nio.channels.AlreadyConnectedException in project robovm by robovm.

the class SocketChannelTest method testCFII_Data_ConnectWithServer_nonBlocking.

/*
     * Test method for 'SocketChannelImpl.connect(SocketAddress)'
     */
public void testCFII_Data_ConnectWithServer_nonBlocking() throws Exception {
    ensureServerOpen();
    java.nio.ByteBuffer writeBuf = java.nio.ByteBuffer.allocate(CAPACITY_NORMAL);
    java.nio.ByteBuffer[] writeBufArr = new java.nio.ByteBuffer[1];
    writeBufArr[0] = java.nio.ByteBuffer.allocate(CAPACITY_NORMAL);
    assertFalse(this.channel1.isRegistered());
    assertTrue(this.channel1.isBlocking());
    this.channel1.configureBlocking(false);
    this.channel1.connect(localAddr1);
    assertFalse(this.channel1.isBlocking());
    boolean connected = channel1.isConnected();
    if (!connected) {
        assertTrue(this.channel1.isConnectionPending());
        assertTrue(this.channel1.isOpen());
    }
    if (tryFinish()) {
        assertEquals(CAPACITY_NORMAL, this.channel1.write(writeBuf));
        assertEquals(CAPACITY_NORMAL, this.channel1.write(writeBufArr, 0, 1));
        this.channel1.configureBlocking(false);
        try {
            this.channel1.connect(localAddr1);
            fail("Should throw AlreadyConnectedException");
        } catch (AlreadyConnectedException e) {
        // correct
        }
    }
    assertFalse(this.channel1.isRegistered());
    tryFinish();
}
Also used : AlreadyConnectedException(java.nio.channels.AlreadyConnectedException) ByteBuffer(java.nio.ByteBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 9 with AlreadyConnectedException

use of java.nio.channels.AlreadyConnectedException in project robovm by robovm.

the class SocketChannelTest method testCFII_Data_ConnectWithServer.

// -------------------------------------------------------------------
// Original tests. Test method for CFII with real data.
// -------------------------------------------------------------------
/**
     *
     * 'SocketChannelImpl.connect(SocketAddress)'
     */
public void testCFII_Data_ConnectWithServer() throws Exception {
    ensureServerOpen();
    java.nio.ByteBuffer writeBuf = java.nio.ByteBuffer.allocate(CAPACITY_NORMAL);
    java.nio.ByteBuffer[] writeBufArr = new java.nio.ByteBuffer[1];
    writeBufArr[0] = java.nio.ByteBuffer.allocate(CAPACITY_NORMAL);
    assertFalse(this.channel1.isRegistered());
    assertTrue(this.channel1.isBlocking());
    this.channel1.connect(localAddr1);
    assertTrue(this.channel1.isBlocking());
    assertTrue(this.channel1.isConnected());
    assertFalse(this.channel1.isConnectionPending());
    assertTrue(this.channel1.isOpen());
    assertEquals(CAPACITY_NORMAL, this.channel1.write(writeBuf));
    assertEquals(CAPACITY_NORMAL, this.channel1.write(writeBufArr, 0, 1));
    this.channel1.configureBlocking(false);
    try {
        this.channel1.connect(localAddr1);
        fail("Should throw AlreadyConnectedException");
    } catch (AlreadyConnectedException e) {
    // correct
    }
    assertFalse(this.channel1.isRegistered());
    tryFinish();
}
Also used : AlreadyConnectedException(java.nio.channels.AlreadyConnectedException) ByteBuffer(java.nio.ByteBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 10 with AlreadyConnectedException

use of java.nio.channels.AlreadyConnectedException in project jdk8u_jdk by JetBrains.

the class Bind method testBind.

void testBind() {
    SctpChannel channel = null;
    try {
        channel = SctpChannel.open();
        /* TEST 1: empty set if channel is not bound */
        check(channel.getAllLocalAddresses().isEmpty(), "getAllLocalAddresses returned non empty set for unbound channel");
        /* TEST 2: null to bind the channel to an automatically assigned
             *         socket address */
        channel.bind(null);
        /* TEST 3: non empty set if the channel is bound */
        check(!channel.getAllLocalAddresses().isEmpty(), "getAllLocalAddresses returned empty set for bound channel");
        debug("getAllLocalAddresses on channel bound to the wildcard:\n" + channel.getAllLocalAddresses());
        /* TEST 4: AlreadyBoundException if this channel is already bound */
        try {
            channel.bind(null);
        } catch (AlreadyBoundException unused) {
            pass();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
        /* TEST 5: UnsupportedAddressTypeException */
        try {
            channel.close();
            /* open a new unbound channel for test */
            channel = SctpChannel.open();
            channel.bind(new UnsupportedSocketAddress());
            fail("UnsupportedSocketAddress expected");
        } catch (UnsupportedAddressTypeException unused) {
            pass();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
        /* TEST 6: AlreadyConnectedException */
        try {
            channel.close();
            /* open a new unbound channel for test */
            channel = SctpChannel.open();
            connectChannel(channel);
            channel.bind(null);
            fail("AlreadyConnectedException expected");
        } catch (AlreadyConnectedException unused) {
            pass();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
        /* TEST 7: ClosedChannelException - If this channel is closed */
        try {
            channel.close();
            /* open a new unbound channel for test */
            channel = SctpChannel.open();
            channel.close();
            channel.bind(null);
            fail("ClosedChannelException expected");
        } catch (ClosedChannelException unused) {
            pass();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
        /* TEST 8: ClosedChannelException if channel is closed */
        try {
            channel.getAllLocalAddresses();
            fail("should have thrown ClosedChannelException");
        } catch (ClosedChannelException cce) {
            pass();
        } catch (Exception ioe) {
            unexpected(ioe);
        }
    } catch (IOException ioe) {
        unexpected(ioe);
    } finally {
        try {
            channel.close();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
    }
}
Also used : SctpChannel(com.sun.nio.sctp.SctpChannel) UnsupportedAddressTypeException(java.nio.channels.UnsupportedAddressTypeException) ClosedChannelException(java.nio.channels.ClosedChannelException) AlreadyBoundException(java.nio.channels.AlreadyBoundException) AlreadyConnectedException(java.nio.channels.AlreadyConnectedException) ClosedChannelException(java.nio.channels.ClosedChannelException) AlreadyConnectedException(java.nio.channels.AlreadyConnectedException) UnsupportedAddressTypeException(java.nio.channels.UnsupportedAddressTypeException) AlreadyBoundException(java.nio.channels.AlreadyBoundException) IllegalUnbindException(com.sun.nio.sctp.IllegalUnbindException)

Aggregations

AlreadyConnectedException (java.nio.channels.AlreadyConnectedException)10 ByteBuffer (java.nio.ByteBuffer)4 NoConnectionPendingException (java.nio.channels.NoConnectionPendingException)3 SctpChannel (com.sun.nio.sctp.SctpChannel)2 InetSocketAddress (java.net.InetSocketAddress)2 UnsupportedAddressTypeException (java.nio.channels.UnsupportedAddressTypeException)2 IllegalUnbindException (com.sun.nio.sctp.IllegalUnbindException)1 SctpServerChannel (com.sun.nio.sctp.SctpServerChannel)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)1 IOException (java.io.IOException)1 ServerSocket (java.net.ServerSocket)1 SocketAddress (java.net.SocketAddress)1 AlreadyBoundException (java.nio.channels.AlreadyBoundException)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 ConnectionPendingException (java.nio.channels.ConnectionPendingException)1 UnresolvedAddressException (java.nio.channels.UnresolvedAddressException)1