< prev index next >

src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java

Print this page

        

*** 57,161 **** /** * An implementation of SocketChannels */ ! class SocketChannelImpl extends SocketChannel implements SelChImpl { // Used to make native read and write calls ! private static NativeDispatcher nd; // Our file descriptor object ! private final FileDescriptor fd; ! private final int fdVal; // Lock held by current reading or connecting thread ! private final ReentrantLock readLock = new ReentrantLock(); // Lock held by current writing or connecting thread ! private final ReentrantLock writeLock = new ReentrantLock(); // Lock held by any thread that modifies the state fields declared below // DO NOT invoke a blocking I/O operation while holding this lock! ! private final Object stateLock = new Object(); // Input/Output closed ! private volatile boolean isInputClosed; ! private volatile boolean isOutputClosed; // -- The following fields are protected by stateLock // set true when exclusive binding is on and SO_REUSEADDR is emulated ! private boolean isReuseAddress; // State, increases monotonically ! private static final int ST_UNCONNECTED = 0; ! private static final int ST_CONNECTIONPENDING = 1; ! private static final int ST_CONNECTED = 2; ! private static final int ST_CLOSING = 3; ! private static final int ST_KILLPENDING = 4; ! private static final int ST_KILLED = 5; ! private volatile int state; // need stateLock to change // IDs of native threads doing reads and writes, for signalling ! private long readerThread; ! private long writerThread; // Binding ! private InetSocketAddress localAddress; ! private InetSocketAddress remoteAddress; // Socket adaptor, created on demand ! private Socket socket; // -- End of fields protected by stateLock // Constructor for normal connecting sockets // ! SocketChannelImpl(SelectorProvider sp) throws IOException { super(sp); ! this.fd = Net.socket(true); this.fdVal = IOUtil.fdVal(fd); } ! SocketChannelImpl(SelectorProvider sp, FileDescriptor fd, boolean bound) throws IOException { super(sp); this.fd = fd; this.fdVal = IOUtil.fdVal(fd); if (bound) { synchronized (stateLock) { ! this.localAddress = Net.localAddress(fd); } } } // Constructor for sockets obtained from server sockets // ! SocketChannelImpl(SelectorProvider sp, FileDescriptor fd, InetSocketAddress isa) throws IOException { super(sp); this.fd = fd; this.fdVal = IOUtil.fdVal(fd); synchronized (stateLock) { ! this.localAddress = Net.localAddress(fd); this.remoteAddress = isa; this.state = ST_CONNECTED; } } /** * Checks that the channel is open. * * @throws ClosedChannelException if channel is closed (or closing) */ ! private void ensureOpen() throws ClosedChannelException { if (!isOpen()) throw new ClosedChannelException(); } /** --- 57,170 ---- /** * An implementation of SocketChannels */ ! public class SocketChannelImpl extends SocketChannel implements SelChImpl { // Used to make native read and write calls ! protected static NativeDispatcher nd; // Our file descriptor object ! protected FileDescriptor fd; ! protected final int fdVal; // Lock held by current reading or connecting thread ! protected final ReentrantLock readLock = new ReentrantLock(); // Lock held by current writing or connecting thread ! protected final ReentrantLock writeLock = new ReentrantLock(); // Lock held by any thread that modifies the state fields declared below // DO NOT invoke a blocking I/O operation while holding this lock! ! protected final Object stateLock = new Object(); // Input/Output closed ! protected volatile boolean isInputClosed; ! protected volatile boolean isOutputClosed; // -- The following fields are protected by stateLock // set true when exclusive binding is on and SO_REUSEADDR is emulated ! protected boolean isReuseAddress; // State, increases monotonically ! protected static final int ST_UNCONNECTED = 0; ! protected static final int ST_CONNECTIONPENDING = 1; ! protected static final int ST_CONNECTED = 2; ! protected static final int ST_CLOSING = 3; ! protected static final int ST_KILLPENDING = 4; ! protected static final int ST_KILLED = 5; ! protected volatile int state; // need stateLock to change // IDs of native threads doing reads and writes, for signalling ! protected long readerThread; ! protected long writerThread; // Binding ! protected InetSocketAddress localAddress; ! protected InetSocketAddress remoteAddress; // Socket adaptor, created on demand ! protected Socket socket; // -- End of fields protected by stateLock // Constructor for normal connecting sockets // ! protected SocketChannelImpl(SelectorProvider sp) throws IOException { super(sp); ! this.fd = createFD(); this.fdVal = IOUtil.fdVal(fd); } ! protected SocketChannelImpl(SelectorProvider sp, FileDescriptor fd, boolean bound) throws IOException { super(sp); this.fd = fd; this.fdVal = IOUtil.fdVal(fd); if (bound) { synchronized (stateLock) { ! this.localAddress = createLocalAddress(fd); } } } // Constructor for sockets obtained from server sockets // ! protected SocketChannelImpl(SelectorProvider sp, FileDescriptor fd, InetSocketAddress isa) throws IOException { super(sp); this.fd = fd; this.fdVal = IOUtil.fdVal(fd); synchronized (stateLock) { ! this.localAddress = createLocalAddress(fd); this.remoteAddress = isa; this.state = ST_CONNECTED; } } + protected FileDescriptor createFD() throws IOException { + return Net.socket(true); + } + + protected InetSocketAddress createLocalAddress(FileDescriptor fd) + throws IOException { + return Net.localAddress(fd); + } + /** * Checks that the channel is open. * * @throws ClosedChannelException if channel is closed (or closing) */ ! protected void ensureOpen() throws ClosedChannelException { if (!isOpen()) throw new ClosedChannelException(); } /**
*** 284,304 **** return Collections.unmodifiableSet(set); } } @Override ! public final Set<SocketOption<?>> supportedOptions() { return DefaultOptionsHolder.defaultOptions; } /** * Marks the beginning of a read operation that might block. * * @throws ClosedChannelException if the channel is closed * @throws NotYetConnectedException if the channel is not yet connected */ ! private void beginRead(boolean blocking) throws ClosedChannelException { if (blocking) { // set hook for Thread.interrupt begin(); synchronized (stateLock) { --- 293,313 ---- return Collections.unmodifiableSet(set); } } @Override ! public Set<SocketOption<?>> supportedOptions() { return DefaultOptionsHolder.defaultOptions; } /** * Marks the beginning of a read operation that might block. * * @throws ClosedChannelException if the channel is closed * @throws NotYetConnectedException if the channel is not yet connected */ ! protected void beginRead(boolean blocking) throws ClosedChannelException { if (blocking) { // set hook for Thread.interrupt begin(); synchronized (stateLock) {
*** 315,325 **** * Marks the end of a read operation that may have blocked. * * @throws AsynchronousCloseException if the channel was closed due to this * thread being interrupted on a blocking read operation. */ ! private void endRead(boolean blocking, boolean completed) throws AsynchronousCloseException { if (blocking) { synchronized (stateLock) { readerThread = 0; --- 324,334 ---- * Marks the end of a read operation that may have blocked. * * @throws AsynchronousCloseException if the channel was closed due to this * thread being interrupted on a blocking read operation. */ ! protected void endRead(boolean blocking, boolean completed) throws AsynchronousCloseException { if (blocking) { synchronized (stateLock) { readerThread = 0;
*** 405,415 **** * Marks the beginning of a write operation that might block. * * @throws ClosedChannelException if the channel is closed or output shutdown * @throws NotYetConnectedException if the channel is not yet connected */ ! private void beginWrite(boolean blocking) throws ClosedChannelException { if (blocking) { // set hook for Thread.interrupt begin(); synchronized (stateLock) { --- 414,424 ---- * Marks the beginning of a write operation that might block. * * @throws ClosedChannelException if the channel is closed or output shutdown * @throws NotYetConnectedException if the channel is not yet connected */ ! protected void beginWrite(boolean blocking) throws ClosedChannelException { if (blocking) { // set hook for Thread.interrupt begin(); synchronized (stateLock) {
*** 428,438 **** * Marks the end of a write operation that may have blocked. * * @throws AsynchronousCloseException if the channel was closed due to this * thread being interrupted on a blocking write operation. */ ! private void endWrite(boolean blocking, boolean completed) throws AsynchronousCloseException { if (blocking) { synchronized (stateLock) { writerThread = 0; --- 437,447 ---- * Marks the end of a write operation that may have blocked. * * @throws AsynchronousCloseException if the channel was closed due to this * thread being interrupted on a blocking write operation. */ ! protected void endWrite(boolean blocking, boolean completed) throws AsynchronousCloseException { if (blocking) { synchronized (stateLock) { writerThread = 0;
*** 505,515 **** } /** * Writes a byte of out of band data. */ ! int sendOutOfBandData(byte b) throws IOException { writeLock.lock(); try { boolean blocking = isBlocking(); int n = 0; try { --- 514,524 ---- } /** * Writes a byte of out of band data. */ ! protected int sendOutOfBandData(byte b) throws IOException { writeLock.lock(); try { boolean blocking = isBlocking(); int n = 0; try {
*** 551,570 **** } /** * Returns the local address, or null if not bound */ ! InetSocketAddress localAddress() { synchronized (stateLock) { return localAddress; } } /** * Returns the remote address, or null if not connected */ ! InetSocketAddress remoteAddress() { synchronized (stateLock) { return remoteAddress; } } --- 560,579 ---- } /** * Returns the local address, or null if not bound */ ! protected InetSocketAddress localAddress() { synchronized (stateLock) { return localAddress; } } /** * Returns the remote address, or null if not connected */ ! protected InetSocketAddress remoteAddress() { synchronized (stateLock) { return remoteAddress; } }
*** 616,626 **** * @throws ClosedChannelException if the channel is closed * @throws AlreadyConnectedException if already connected * @throws ConnectionPendingException is a connection is pending * @throws IOException if the pre-connect hook fails */ ! private void beginConnect(boolean blocking, InetSocketAddress isa) throws IOException { if (blocking) { // set hook for Thread.interrupt begin(); --- 625,635 ---- * @throws ClosedChannelException if the channel is closed * @throws AlreadyConnectedException if already connected * @throws ConnectionPendingException is a connection is pending * @throws IOException if the pre-connect hook fails */ ! protected void beginConnect(boolean blocking, InetSocketAddress isa) throws IOException { if (blocking) { // set hook for Thread.interrupt begin();
*** 651,661 **** * * @throws AsynchronousCloseException if the channel was closed due to this * thread being interrupted on a blocking connect operation. * @throws IOException if completed and unable to obtain the local address */ ! private void endConnect(boolean blocking, boolean completed) throws IOException { endRead(blocking, completed); if (completed) { --- 660,670 ---- * * @throws AsynchronousCloseException if the channel was closed due to this * thread being interrupted on a blocking connect operation. * @throws IOException if completed and unable to obtain the local address */ ! protected void endConnect(boolean blocking, boolean completed) throws IOException { endRead(blocking, completed); if (completed) {
*** 713,723 **** * Marks the beginning of a finishConnect operation that might block. * * @throws ClosedChannelException if the channel is closed * @throws NoConnectionPendingException if no connection is pending */ ! private void beginFinishConnect(boolean blocking) throws ClosedChannelException { if (blocking) { // set hook for Thread.interrupt begin(); } synchronized (stateLock) { --- 722,732 ---- * Marks the beginning of a finishConnect operation that might block. * * @throws ClosedChannelException if the channel is closed * @throws NoConnectionPendingException if no connection is pending */ ! protected void beginFinishConnect(boolean blocking) throws ClosedChannelException { if (blocking) { // set hook for Thread.interrupt begin(); } synchronized (stateLock) {
*** 736,746 **** * * @throws AsynchronousCloseException if the channel was closed due to this * thread being interrupted on a blocking connect operation. * @throws IOException if completed and unable to obtain the local address */ ! private void endFinishConnect(boolean blocking, boolean completed) throws IOException { endRead(blocking, completed); if (completed) { --- 745,755 ---- * * @throws AsynchronousCloseException if the channel was closed due to this * thread being interrupted on a blocking connect operation. * @throws IOException if completed and unable to obtain the local address */ ! protected void endFinishConnect(boolean blocking, boolean completed) throws IOException { endRead(blocking, completed); if (completed) {
*** 928,942 **** } return this; } } ! boolean isInputOpen() { return !isInputClosed; } ! boolean isOutputOpen() { return !isOutputClosed; } /** * Poll this channel's socket for reading up to the given timeout. --- 937,951 ---- } return this; } } ! protected boolean isInputOpen() { return !isInputClosed; } ! protected boolean isOutputOpen() { return !isOutputClosed; } /** * Poll this channel's socket for reading up to the given timeout.
< prev index next >