src/java.base/share/classes/java/net/AbstractPlainSocketImpl.java
branchniosocketimpl-branch
changeset 57347 16c087c9103e
parent 57268 adcdd45830a0
parent 54689 b28b7f631301
child 57382 14e01d39c01a
equal deleted inserted replaced
57344:8b621b0d921c 57347:16c087c9103e
    72     protected boolean closePending = false;
    72     protected boolean closePending = false;
    73 
    73 
    74     /* indicates connection reset state */
    74     /* indicates connection reset state */
    75     private volatile boolean connectionReset;
    75     private volatile boolean connectionReset;
    76 
    76 
       
    77     /* indicates whether impl is bound  */
       
    78     boolean isBound;
       
    79 
       
    80     /* indicates whether impl is connected  */
       
    81     volatile boolean isConnected;
       
    82 
    77    /* whether this Socket is a stream (TCP) socket or not (UDP)
    83    /* whether this Socket is a stream (TCP) socket or not (UDP)
    78     */
    84     */
    79     protected boolean stream;
    85     protected boolean stream;
    80 
    86 
    81     /**
    87     /**
   101         if (!checkedReusePort) {
   107         if (!checkedReusePort) {
   102             isReusePortAvailable = isReusePortAvailable0();
   108             isReusePortAvailable = isReusePortAvailable0();
   103             checkedReusePort = true;
   109             checkedReusePort = true;
   104         }
   110         }
   105         return isReusePortAvailable;
   111         return isReusePortAvailable;
       
   112     }
       
   113 
       
   114     AbstractPlainSocketImpl(boolean isServer) {
       
   115         super(isServer);
   106     }
   116     }
   107 
   117 
   108     /**
   118     /**
   109      * Returns a set of SocketOptions supported by this impl and by this impl's
   119      * Returns a set of SocketOptions supported by this impl and by this impl's
   110      * socket (Socket or ServerSocket)
   120      * socket (Socket or ServerSocket)
   146         } else {
   156         } else {
   147             fd = new FileDescriptor();
   157             fd = new FileDescriptor();
   148             socketCreate(true);
   158             socketCreate(true);
   149             SocketCleanable.register(fd);
   159             SocketCleanable.register(fd);
   150         }
   160         }
   151         if (socket != null)
       
   152             socket.setCreated();
       
   153         if (serverSocket != null)
       
   154             serverSocket.setCreated();
       
   155     }
   161     }
   156 
   162 
   157     /**
   163     /**
   158      * Creates a socket and connects it to the specified port on
   164      * Creates a socket and connects it to the specified port on
   159      * the specified host.
   165      * the specified host.
   178                 } catch (IOException ioe) {
   184                 } catch (IOException ioe) {
   179                     /* Do nothing. If connect threw an exception then
   185                     /* Do nothing. If connect threw an exception then
   180                        it will be passed up the call stack */
   186                        it will be passed up the call stack */
   181                 }
   187                 }
   182             }
   188             }
       
   189             isConnected = connected;
   183         }
   190         }
   184     }
   191     }
   185 
   192 
   186     /**
   193     /**
   187      * Creates a socket and connects it to the specified address on
   194      * Creates a socket and connects it to the specified address on
   193         this.port = port;
   200         this.port = port;
   194         this.address = address;
   201         this.address = address;
   195 
   202 
   196         try {
   203         try {
   197             connectToAddress(address, port, timeout);
   204             connectToAddress(address, port, timeout);
       
   205             isConnected = true;
   198             return;
   206             return;
   199         } catch (IOException e) {
   207         } catch (IOException e) {
   200             // everything failed
   208             // everything failed
   201             close();
   209             close();
   202             throw e;
   210             throw e;
   234                 } catch (IOException ioe) {
   242                 } catch (IOException ioe) {
   235                     /* Do nothing. If connect threw an exception then
   243                     /* Do nothing. If connect threw an exception then
   236                        it will be passed up the call stack */
   244                        it will be passed up the call stack */
   237                 }
   245                 }
   238             }
   246             }
       
   247             isConnected = connected;
   239         }
   248         }
   240     }
   249     }
   241 
   250 
   242     private void connectToAddress(InetAddress address, int port, int timeout) throws IOException {
   251     private void connectToAddress(InetAddress address, int port, int timeout) throws IOException {
   243         if (address.isAnyLocalAddress()) {
   252         if (address.isAnyLocalAddress()) {
   391      * throws an IOException indicating what went wrong.
   400      * throws an IOException indicating what went wrong.
   392      */
   401      */
   393 
   402 
   394     synchronized void doConnect(InetAddress address, int port, int timeout) throws IOException {
   403     synchronized void doConnect(InetAddress address, int port, int timeout) throws IOException {
   395         synchronized (fdLock) {
   404         synchronized (fdLock) {
   396             if (!closePending && (socket == null || !socket.isBound())) {
   405             if (!closePending && !isBound) {
   397                 NetHooks.beforeTcpConnect(fd, address, port);
   406                 NetHooks.beforeTcpConnect(fd, address, port);
   398             }
   407             }
   399         }
   408         }
   400         try {
   409         try {
   401             acquireFD();
   410             acquireFD();
   405                 synchronized (fdLock) {
   414                 synchronized (fdLock) {
   406                     if (closePending) {
   415                     if (closePending) {
   407                         throw new SocketException ("Socket closed");
   416                         throw new SocketException ("Socket closed");
   408                     }
   417                     }
   409                 }
   418                 }
   410                 // If we have a ref. to the Socket, then sets the flags
       
   411                 // created, bound & connected to true.
       
   412                 // This is normally done in Socket.connect() but some
       
   413                 // subclasses of Socket may call impl.connect() directly!
       
   414                 if (socket != null) {
       
   415                     socket.setBound();
       
   416                     socket.setConnected();
       
   417                 }
       
   418             } finally {
   419             } finally {
   419                 releaseFD();
   420                 releaseFD();
   420             }
   421             }
   421         } catch (IOException e) {
   422         } catch (IOException e) {
   422             close();
   423             close();
   431      */
   432      */
   432     protected synchronized void bind(InetAddress address, int lport)
   433     protected synchronized void bind(InetAddress address, int lport)
   433         throws IOException
   434         throws IOException
   434     {
   435     {
   435        synchronized (fdLock) {
   436        synchronized (fdLock) {
   436             if (!closePending && (socket == null || !socket.isBound())) {
   437             if (!closePending && !isBound) {
   437                 NetHooks.beforeTcpBind(fd, address, lport);
   438                 NetHooks.beforeTcpBind(fd, address, lport);
   438             }
   439             }
   439         }
   440         }
   440         socketBind(address, lport);
   441         socketBind(address, lport);
   441         if (socket != null)
   442         isBound = true;
   442             socket.setBound();
       
   443         if (serverSocket != null)
       
   444             serverSocket.setBound();
       
   445     }
   443     }
   446 
   444 
   447     /**
   445     /**
   448      * Listens, for a specified amount of time, for connections.
   446      * Listens, for a specified amount of time, for connections.
   449      * @param count the amount of time to listen for connections
   447      * @param count the amount of time to listen for connections
   725     protected void socketClose() throws IOException {
   723     protected void socketClose() throws IOException {
   726         SocketCleanable.unregister(fd);
   724         SocketCleanable.unregister(fd);
   727         socketClose0(false);
   725         socketClose0(false);
   728     }
   726     }
   729 
   727 
   730     abstract void socketCreate(boolean isServer) throws IOException;
   728     abstract void socketCreate(boolean stream) throws IOException;
   731     abstract void socketConnect(InetAddress address, int port, int timeout)
   729     abstract void socketConnect(InetAddress address, int port, int timeout)
   732         throws IOException;
   730         throws IOException;
   733     abstract void socketBind(InetAddress address, int port)
   731     abstract void socketBind(InetAddress address, int port)
   734         throws IOException;
   732         throws IOException;
   735     abstract void socketListen(int count)
   733     abstract void socketListen(int count)