public Future<Boolean> connect(InetSocketAddressWrapper addressWrapper) { if (addressWrapper == null) { throw new NullPointerException("Null Address"); } // Remove addr from removed set this.removedAddrSet.remove(addressWrapper.getInetSocketAddress()); SocketChannel socketChannel = null; ConnectFuture future = new ConnectFuture(addressWrapper); try { socketChannel = SocketChannel.open(); this.configureSocketChannel(socketChannel); if (!socketChannel.connect(addressWrapper.getInetSocketAddress())) { this.selectorManager.registerChannel(socketChannel, SelectionKey.OP_CONNECT, future); } else { this.addSession(this.createSession(socketChannel, addressWrapper)); future.setResult(true); } } catch (Exception e) { if (socketChannel != null) { try { socketChannel.close(); } catch (IOException e1) { //propagate original exception } } future.failure(e); } return future; }
key.interestOps(key.interestOps() & ~SelectionKey.OP_CONNECT); ConnectFuture future = (ConnectFuture) key.attachment(); if (future == null || future.isCancelled()) { this.cancelKey(key); return; this.cancelKey(key); future .failure(new IOException("Connect to " + SystemUtils.getRawAddress( future.getInetSocketAddressWrapper().getInetSocketAddress()) + ":" + future.getInetSocketAddressWrapper().getInetSocketAddress().getPort() + " fail")); } else { key.attach(null); this.addSession(this.createSession((SocketChannel) key.channel(), future.getInetSocketAddressWrapper())); future.setResult(Boolean.TRUE); future.failure(e); this.cancelKey(key); throw new IOException("Connect to " + SystemUtils.getRawAddress(future.getInetSocketAddressWrapper().getInetSocketAddress()) + ":" + future.getInetSocketAddressWrapper().getInetSocketAddress().getPort() + " fail," + e.getMessage());
key.interestOps(key.interestOps() & ~SelectionKey.OP_CONNECT); ConnectFuture future = (ConnectFuture) key.attachment(); if (future == null || future.isCancelled()) { this.cancelKey(key); return; this.cancelKey(key); future .failure(new IOException("Connect to " + SystemUtils.getRawAddress( future.getInetSocketAddressWrapper().getInetSocketAddress()) + ":" + future.getInetSocketAddressWrapper().getInetSocketAddress().getPort() + " fail")); } else { key.attach(null); this.addSession(this.createSession((SocketChannel) key.channel(), future.getInetSocketAddressWrapper())); future.setResult(Boolean.TRUE); future.failure(e); this.cancelKey(key); throw new IOException("Connect to " + SystemUtils.getRawAddress(future.getInetSocketAddressWrapper().getInetSocketAddress()) + ":" + future.getInetSocketAddressWrapper().getInetSocketAddress().getPort() + " fail," + e.getMessage());
public Future<Boolean> connect(InetSocketAddressWrapper addressWrapper) { if (addressWrapper == null) { throw new NullPointerException("Null Address"); } // Remove addr from removed set this.removedAddrSet.remove(addressWrapper.getInetSocketAddress()); SocketChannel socketChannel = null; ConnectFuture future = new ConnectFuture(addressWrapper); try { socketChannel = SocketChannel.open(); this.configureSocketChannel(socketChannel); if (!socketChannel.connect(addressWrapper.getInetSocketAddress())) { this.selectorManager.registerChannel(socketChannel, SelectionKey.OP_CONNECT, future); } else { this.addSession(this.createSession(socketChannel, addressWrapper)); future.setResult(true); } } catch (Exception e) { if (socketChannel != null) { try { socketChannel.close(); } catch (IOException e1) { //propagate original exception } } future.failure(e); } return future; }