SocketChannel socketChannel;
try {
socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
} catch (IOException ex) {
throw new IOReactorException("Failure opening socket", ex);
}
try {
validateAddress(request.getLocalAddress());
validateAddress(request.getRemoteAddress());
if (request.getLocalAddress() != null) {
Socket sock = socketChannel.socket();
sock.setReuseAddress(HttpConnectionParams.getSoReuseaddr(this.params));
sock.bind(request.getLocalAddress());
}
boolean connected = socketChannel.connect(request.getRemoteAddress());
if (connected) {
prepareSocket(socketChannel.socket());
ChannelEntry entry = new ChannelEntry(socketChannel, request);
addChannel(entry);
return;
}
} catch (IOException ex) {
request.failed(ex);
return;
}
SessionRequestHandle requestHandle = new SessionRequestHandle(request);
SelectionKey key;
try {
key = socketChannel.register(this.selector, SelectionKey.OP_CONNECT, requestHandle);
request.setKey(key);
} catch (CancelledKeyException ex) {
// Ignore cancelled keys
} catch (IOException ex) {
throw new IOReactorException("Failure registering channel " +
"with the selector", ex);
}
}
}