import org.apache.catalina.tribes.transport.DataSender;
import org.apache.catalina.tribes.RemoteProcessException;
import java.io.EOFException;
+import java.net.*;
/**
* This class is NOT thread safe and should never be used with more than one thread at a time
if ( !key.isValid() ) throw new IOException("Key is not valid, it must have been cancelled.");
if ( key.isConnectable() ) {
if ( socketChannel.finishConnect() ) {
- //we connected, register ourselves for writing
- setConnected(true);
- connecting = false;
- setRequestCount(0);
- setConnectTime(System.currentTimeMillis());
- socketChannel.socket().setSendBufferSize(getTxBufSize());
- socketChannel.socket().setReceiveBufferSize(getRxBufSize());
- socketChannel.socket().setSoTimeout((int)getTimeout());
- socketChannel.socket().setSoLinger(false,0);
- socketChannel.socket().setTcpNoDelay(getTcpNoDelay());
- socketChannel.socket().setKeepAlive(getSoKeepAlive());
- socketChannel.socket().setReuseAddress(getSoReuseAddress());
- socketChannel.socket().setOOBInline(getOoBInline());
- socketChannel.socket().setSoLinger(getSoLingerOn(),getSoLingerTime());
- socketChannel.socket().setTrafficClass(getSoTrafficClass());
+ completeConnect();
if ( current != null ) key.interestOps(key.interestOps() | SelectionKey.OP_WRITE);
return false;
} else {
}//end if
return false;
}
+
+ private void completeConnect() throws SocketException {
+ //we connected, register ourselves for writing
+ setConnected(true);
+ connecting = false;
+ setRequestCount(0);
+ setConnectTime(System.currentTimeMillis());
+ socketChannel.socket().setSendBufferSize(getTxBufSize());
+ socketChannel.socket().setReceiveBufferSize(getRxBufSize());
+ socketChannel.socket().setSoTimeout((int)getTimeout());
+ socketChannel.socket().setSoLinger(false,0);
+ socketChannel.socket().setTcpNoDelay(getTcpNoDelay());
+ socketChannel.socket().setKeepAlive(getSoKeepAlive());
+ socketChannel.socket().setReuseAddress(getSoReuseAddress());
+ socketChannel.socket().setOOBInline(getOoBInline());
+ socketChannel.socket().setSoLinger(getSoLingerOn(),getSoLingerTime());
+ socketChannel.socket().setTrafficClass(getSoTrafficClass());
+ }
if ( socketChannel != null ) throw new IOException("Socket channel has already been established. Connection might be in progress.");
socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
- socketChannel.connect(addr);
- socketChannel.register(getSelector(),SelectionKey.OP_CONNECT,this);
+ if ( socketChannel.connect(addr) ) {
+ completeConnect();
+ socketChannel.register(getSelector(), SelectionKey.OP_WRITE, this);
+ } else {
+ socketChannel.register(getSelector(), SelectionKey.OP_CONNECT, this);
+ }
}