if (config == null) {
config = getDefaultConfig();
}
DatagramChannel ch = null;
boolean initialized = false;
try {
ch = DatagramChannel.open();
DatagramSessionConfig cfg;
if (config.getSessionConfig() instanceof DatagramSessionConfig) {
cfg = (DatagramSessionConfig) config.getSessionConfig();
} else {
cfg = (DatagramSessionConfig) getDefaultConfig()
.getSessionConfig();
}
ch.socket().setReuseAddress(cfg.isReuseAddress());
ch.socket().setBroadcast(cfg.isBroadcast());
ch.socket().setReceiveBufferSize(cfg.getReceiveBufferSize());
ch.socket().setSendBufferSize(cfg.getSendBufferSize());
if (ch.socket().getTrafficClass() != cfg.getTrafficClass()) {
ch.socket().setTrafficClass(cfg.getTrafficClass());
}
if (localAddress != null) {
ch.socket().bind(localAddress);
}
ch.connect(address);
ch.configureBlocking(false);
initialized = true;
} catch (IOException e) {
return DefaultConnectFuture.newFailedFuture(e);
} finally {
if (!initialized && ch != null) {
try {
ch.disconnect();
ch.close();
} catch (IOException e) {
ExceptionMonitor.getInstance().exceptionCaught(e);
}
}
}
RegistrationRequest request = new RegistrationRequest(ch, handler,
config);
synchronized (this) {
try {
startupWorker();
} catch (IOException e) {
try {
ch.disconnect();
ch.close();
} catch (IOException e2) {
ExceptionMonitor.getInstance().exceptionCaught(e2);
}
return DefaultConnectFuture.newFailedFuture(e);