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;
}
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();
}
}
}
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();
}
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();
}
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);
}
}
}
Aggregations