AbstractPlainSocketImpl.java revision 1040
/*
* Copyright 1995-2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/**
* Default Socket Implementation. This implementation does
* not implement any security checks.
* Note this class should <b>NOT</b> be public.
*
* @author Steven B. Byrne
*/
abstract class AbstractPlainSocketImpl extends SocketImpl
{
/* instance variable for SO_TIMEOUT */
int timeout; // timeout in millisec
// traffic class
private int trafficClass;
private boolean shut_rd = false;
private boolean shut_wr = false;
/* number of threads using the FileDescriptor */
protected int fdUseCount = 0;
/* lock when increment/decrementing fdUseCount */
/* indicates a close is pending on the file descriptor */
protected boolean closePending = false;
/* indicates connection reset state */
private int CONNECTION_NOT_RESET = 0;
private int CONNECTION_RESET_PENDING = 1;
private int CONNECTION_RESET = 2;
private int resetState;
/**
* Load net library into runtime.
*/
static {
}
/**
* Creates a socket with a boolean that specifies whether this
* is a stream socket (true) or an unconnected UDP socket (false).
*/
fd = new FileDescriptor();
socket.setCreated();
if (serverSocket != null)
}
/**
* Creates a socket and connects it to the specified port on
* the specified host.
* @param host the specified host
* @param port the specified port
*/
throws UnknownHostException, IOException
{
try {
try {
return;
} catch (IOException e) {
pending = e;
}
} catch (UnknownHostException e) {
pending = e;
}
// everything failed
close();
throw pending;
}
/**
* Creates a socket and connects it to the specified address on
* the specified port.
* @param address the address
* @param port the specified port
*/
try {
return;
} catch (IOException e) {
// everything failed
close();
throw e;
}
}
/**
* Creates a socket and connects it to the specified address on
* the specified port.
* @param address the address
* @param timeout the timeout value in milliseconds, or zero for no timeout.
* @throws IOException if connection fails
* @throws IllegalArgumentException if address is null or is a
* SocketAddress subclass not supported by this socket
* @since 1.4
*/
throw new IllegalArgumentException("unsupported address type");
if (addr.isUnresolved())
try {
return;
} catch (IOException e) {
// everything failed
close();
throw e;
}
}
if (address.isAnyLocalAddress()) {
} else {
}
}
if (isClosedOrPending()) {
throw new SocketException("Socket Closed");
}
boolean on = true;
switch (opt) {
/* check type safety b4 going native. These should never
* fail, since only java.Socket* has access to
* PlainSocketImpl.setOption().
*/
case SO_LINGER:
throw new SocketException("Bad parameter for option");
/* true only if disabling - enabling should be Integer */
on = false;
}
break;
case SO_TIMEOUT:
throw new SocketException("Bad parameter for SO_TIMEOUT");
if (tmp < 0)
throw new IllegalArgumentException("timeout < 0");
break;
case IP_TOS:
throw new SocketException("bad argument for IP_TOS");
}
break;
case SO_BINDADDR:
throw new SocketException("Cannot re-bind socket");
case TCP_NODELAY:
throw new SocketException("bad parameter for TCP_NODELAY");
break;
case SO_SNDBUF:
case SO_RCVBUF:
throw new SocketException("bad parameter for SO_SNDBUF " +
"or SO_RCVBUF");
}
break;
case SO_KEEPALIVE:
throw new SocketException("bad parameter for SO_KEEPALIVE");
break;
case SO_OOBINLINE:
throw new SocketException("bad parameter for SO_OOBINLINE");
break;
case SO_REUSEADDR:
throw new SocketException("bad parameter for SO_REUSEADDR");
break;
default:
}
}
if (isClosedOrPending()) {
throw new SocketException("Socket Closed");
}
if (opt == SO_TIMEOUT) {
}
int ret = 0;
/*
* The native socketGetOption() knows about 3 options.
* The 32 bit value it returns will be interpreted according
* to what we're asking. A return of -1 means it understands
* the option but its turned off. It will raise a SocketException
* if "opt" isn't one it understands.
*/
switch (opt) {
case TCP_NODELAY:
case SO_OOBINLINE:
case SO_LINGER:
case SO_REUSEADDR:
case SO_BINDADDR:
case SO_SNDBUF:
case SO_RCVBUF:
case IP_TOS:
return new Integer(trafficClass);
} else {
}
case SO_KEEPALIVE:
// should never get here
default:
return null;
}
}
/**
* The workhorse of the connection operation. Tries several times to
* establish a connection to the given <host, port>. If unsuccessful,
* throws an IOException indicating what went wrong.
*/
synchronized (fdLock) {
}
}
try {
try {
synchronized (fdLock) {
if (closePending) {
throw new SocketException ("Socket closed");
}
}
// If we have a ref. to the Socket, then sets the flags
// created, bound & connected to true.
// This is normally done in Socket.connect() but some
// subclasses of Socket may call impl.connect() directly!
}
} finally {
releaseFD();
}
} catch (IOException e) {
close();
throw e;
}
}
/**
* Binds the socket to the specified address of the specified local port.
* @param address the address
* @param port the port
*/
throws IOException
{
synchronized (fdLock) {
}
}
if (serverSocket != null)
}
/**
* Listens, for a specified amount of time, for connections.
* @param count the amount of time to listen for connections
*/
}
/**
* Accepts connections.
* @param s the connection
*/
try {
socketAccept(s);
} finally {
releaseFD();
}
}
/**
* Gets an InputStream for this socket.
*/
if (isClosedOrPending()) {
throw new IOException("Socket Closed");
}
if (shut_rd) {
throw new IOException("Socket input is shutdown");
}
if (socketInputStream == null) {
socketInputStream = new SocketInputStream(this);
}
return socketInputStream;
}
}
/**
* Gets an OutputStream for this socket.
*/
if (isClosedOrPending()) {
throw new IOException("Socket Closed");
}
if (shut_wr) {
throw new IOException("Socket output is shutdown");
}
return new SocketOutputStream(this);
}
}
}
}
void setLocalPort(int localport) {
}
/**
* Returns the number of bytes that can be read without blocking.
*/
protected synchronized int available() throws IOException {
if (isClosedOrPending()) {
throw new IOException("Stream closed.");
}
/*
* If connection has been reset then return 0 to indicate
* there are no buffered bytes.
*/
if (isConnectionReset()) {
return 0;
}
/*
* If no bytes available and we were previously notified
* of a connection reset then we move to the reset state.
*
* If are notified of a connection reset then check
* again if there are bytes buffered on the socket.
*/
int n = 0;
try {
n = socketAvailable();
if (n == 0 && isConnectionResetPending()) {
}
} catch (ConnectionResetException exc1) {
try {
n = socketAvailable();
if (n == 0) {
}
} catch (ConnectionResetException exc2) {
}
}
return n;
}
/**
* Closes the socket.
*/
protected void close() throws IOException {
synchronized(fdLock) {
if (fdUseCount == 0) {
if (closePending) {
return;
}
closePending = true;
/*
* We close the FileDescriptor in two-steps - first the
* "pre-close" which closes the socket but doesn't
* release the underlying file descriptor. This operation
* may be lengthy due to untransmitted data and a long
* linger interval. Once the pre-close is done we do the
* actual socket to release the fd.
*/
try {
} finally {
socketClose();
}
return;
} else {
/*
* If a thread has acquired the fd and a close
* isn't pending then use a deferred close.
* Also decrement fdUseCount to signal the last
* thread that releases the fd to close it.
*/
if (!closePending) {
closePending = true;
fdUseCount--;
}
}
}
}
}
void reset() throws IOException {
socketClose();
}
super.reset();
}
/**
* Shutdown read-half of the socket connection;
*/
protected void shutdownInput() throws IOException {
if (socketInputStream != null) {
socketInputStream.setEOF(true);
}
shut_rd = true;
}
}
/**
* Shutdown write-half of the socket connection;
*/
protected void shutdownOutput() throws IOException {
shut_wr = true;
}
}
protected boolean supportsUrgentData () {
return true;
}
throw new IOException("Socket Closed");
}
}
/**
* Cleans up if the user forgets to close it.
*/
protected void finalize() throws IOException {
close();
}
/*
* "Acquires" and returns the FileDescriptor for this impl
*
* A corresponding releaseFD is required to "release" the
* FileDescriptor.
*/
synchronized (fdLock) {
fdUseCount++;
return fd;
}
}
/*
* "Release" the FileDescriptor for this impl.
*
* If the use count goes to -1 then the socket is closed.
*/
void releaseFD() {
synchronized (fdLock) {
fdUseCount--;
if (fdUseCount == -1) {
try {
socketClose();
} catch (IOException e) {
} finally {
}
}
}
}
}
public boolean isConnectionReset() {
synchronized (resetLock) {
return (resetState == CONNECTION_RESET);
}
}
public boolean isConnectionResetPending() {
synchronized (resetLock) {
return (resetState == CONNECTION_RESET_PENDING);
}
}
public void setConnectionReset() {
synchronized (resetLock) {
}
}
public void setConnectionResetPending() {
synchronized (resetLock) {
if (resetState == CONNECTION_NOT_RESET) {
}
}
}
/*
* Return true if already closed or close is pending
*/
public boolean isClosedOrPending() {
/*
* Lock on fdLock to ensure that we wait if a
* close is in progress.
*/
synchronized (fdLock) {
return true;
} else {
return false;
}
}
}
/*
* Return the current value of SO_TIMEOUT
*/
public int getTimeout() {
return timeout;
}
/*
* "Pre-close" a socket by dup'ing the file descriptor - this enables
* the socket to be closed without releasing the file descriptor.
*/
private void socketPreClose() throws IOException {
socketClose0(true);
}
/*
* Close the socket (and release the file descriptor).
*/
protected void socketClose() throws IOException {
socketClose0(false);
}
throws IOException;
throws IOException;
abstract void socketListen(int count)
throws IOException;
abstract void socketAccept(SocketImpl s)
throws IOException;
abstract int socketAvailable()
throws IOException;
abstract void socketClose0(boolean useDeferredClose)
throws IOException;
abstract void socketShutdown(int howto)
throws IOException;
throws SocketException;
abstract void socketSendUrgentData(int data)
throws IOException;
public final static int SHUT_RD = 0;
public final static int SHUT_WR = 1;
}
class InetAddressContainer {
}