jdk/src/share/classes/sun/nio/ch/SocketAdaptor.java
changeset 14025 fbebe005a3ee
parent 11106 802bb70d8fa2
child 14342 8435a30053c1
equal deleted inserted replaced
14024:694c379c2958 14025:fbebe005a3ee
    95                 if (timeout == 0) {
    95                 if (timeout == 0) {
    96                     sc.connect(remote);
    96                     sc.connect(remote);
    97                     return;
    97                     return;
    98                 }
    98                 }
    99 
    99 
   100                 // Implement timeout with a selector
       
   101                 SelectionKey sk = null;
       
   102                 Selector sel = null;
       
   103                 sc.configureBlocking(false);
   100                 sc.configureBlocking(false);
   104                 try {
   101                 try {
   105                     if (sc.connect(remote))
   102                     if (sc.connect(remote))
   106                         return;
   103                         return;
   107                     sel = Util.getTemporarySelector(sc);
       
   108                     sk = sc.register(sel, SelectionKey.OP_CONNECT);
       
   109                     long to = timeout;
   104                     long to = timeout;
   110                     for (;;) {
   105                     for (;;) {
   111                         if (!sc.isOpen())
   106                         if (!sc.isOpen())
   112                             throw new ClosedChannelException();
   107                             throw new ClosedChannelException();
   113                         long st = System.currentTimeMillis();
   108                         long st = System.currentTimeMillis();
   114                         int ns = sel.select(to);
   109 
   115                         if (ns > 0 &&
   110                         int result = sc.poll(PollArrayWrapper.POLLCONN, to);
   116                             sk.isConnectable() && sc.finishConnect())
   111                         if (result > 0 && sc.finishConnect())
   117                             break;
   112                             break;
   118                         sel.selectedKeys().remove(sk);
       
   119                         to -= System.currentTimeMillis() - st;
   113                         to -= System.currentTimeMillis() - st;
   120                         if (to <= 0) {
   114                         if (to <= 0) {
   121                             try {
   115                             try {
   122                                 sc.close();
   116                                 sc.close();
   123                             } catch (IOException x) { }
   117                             } catch (IOException x) { }
   124                             throw new SocketTimeoutException();
   118                             throw new SocketTimeoutException();
   125                         }
   119                         }
   126                     }
   120                     }
   127                 } finally {
   121                 } finally {
   128                     if (sk != null)
       
   129                         sk.cancel();
       
   130                     if (sc.isOpen())
   122                     if (sc.isOpen())
   131                         sc.configureBlocking(true);
   123                         sc.configureBlocking(true);
   132                     if (sel != null)
       
   133                         Util.releaseTemporarySelector(sel);
       
   134                 }
   124                 }
   135 
   125 
   136             } catch (Exception x) {
   126             } catch (Exception x) {
   137                 Net.translateException(x, true);
   127                 Net.translateException(x, true);
   138             }
   128             }
   197             synchronized (sc.blockingLock()) {
   187             synchronized (sc.blockingLock()) {
   198                 if (!sc.isBlocking())
   188                 if (!sc.isBlocking())
   199                     throw new IllegalBlockingModeException();
   189                     throw new IllegalBlockingModeException();
   200                 if (timeout == 0)
   190                 if (timeout == 0)
   201                     return sc.read(bb);
   191                     return sc.read(bb);
   202 
       
   203                 // Implement timeout with a selector
       
   204                 SelectionKey sk = null;
       
   205                 Selector sel = null;
       
   206                 sc.configureBlocking(false);
   192                 sc.configureBlocking(false);
       
   193 
   207                 try {
   194                 try {
   208                     int n;
   195                     int n;
   209                     if ((n = sc.read(bb)) != 0)
   196                     if ((n = sc.read(bb)) != 0)
   210                         return n;
   197                         return n;
   211                     sel = Util.getTemporarySelector(sc);
       
   212                     sk = sc.register(sel, SelectionKey.OP_READ);
       
   213                     long to = timeout;
   198                     long to = timeout;
   214                     for (;;) {
   199                     for (;;) {
   215                         if (!sc.isOpen())
   200                         if (!sc.isOpen())
   216                             throw new ClosedChannelException();
   201                             throw new ClosedChannelException();
   217                         long st = System.currentTimeMillis();
   202                         long st = System.currentTimeMillis();
   218                         int ns = sel.select(to);
   203                         int result = sc.poll(PollArrayWrapper.POLLIN, to);
   219                         if (ns > 0 && sk.isReadable()) {
   204                         if (result > 0) {
   220                             if ((n = sc.read(bb)) != 0)
   205                             if ((n = sc.read(bb)) != 0)
   221                                 return n;
   206                                 return n;
   222                         }
   207                         }
   223                         sel.selectedKeys().remove(sk);
       
   224                         to -= System.currentTimeMillis() - st;
   208                         to -= System.currentTimeMillis() - st;
   225                         if (to <= 0)
   209                         if (to <= 0)
   226                             throw new SocketTimeoutException();
   210                             throw new SocketTimeoutException();
   227                     }
   211                     }
   228                 } finally {
   212                 } finally {
   229                     if (sk != null)
       
   230                         sk.cancel();
       
   231                     if (sc.isOpen())
   213                     if (sc.isOpen())
   232                         sc.configureBlocking(true);
   214                         sc.configureBlocking(true);
   233                     if (sel != null)
       
   234                         Util.releaseTemporarySelector(sel);
       
   235                 }
   215                 }
   236 
   216 
   237             }
   217             }
   238         }
   218         }
   239     }
   219     }