src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/internal/RawChannelImpl.java
branchhttp-client-branch
changeset 56079 d23b02f37fce
parent 55973 4d9b002587db
equal deleted inserted replaced
56078:6c11b48a0695 56079:d23b02f37fce
       
     1 /*
       
     2  * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package jdk.incubator.http.internal;
       
    27 
       
    28 import jdk.incubator.http.internal.common.Utils;
       
    29 import jdk.incubator.http.internal.websocket.RawChannel;
       
    30 
       
    31 import java.io.IOException;
       
    32 import java.nio.ByteBuffer;
       
    33 import java.nio.channels.SelectableChannel;
       
    34 import java.nio.channels.SocketChannel;
       
    35 import java.util.function.Supplier;
       
    36 
       
    37 /*
       
    38  * Each RawChannel corresponds to a TCP connection (SocketChannel) but is
       
    39  * connected to a Selector and an ExecutorService for invoking the send and
       
    40  * receive callbacks. Also includes SSL processing.
       
    41  */
       
    42 final class RawChannelImpl implements RawChannel {
       
    43 
       
    44     private final HttpClientImpl client;
       
    45     private final HttpConnection.DetachedConnectionChannel detachedChannel;
       
    46     private final Object         initialLock = new Object();
       
    47     private Supplier<ByteBuffer> initial;
       
    48 
       
    49     RawChannelImpl(HttpClientImpl client,
       
    50                    HttpConnection connection,
       
    51                    Supplier<ByteBuffer> initial)
       
    52             throws IOException
       
    53     {
       
    54         this.client = client;
       
    55         this.detachedChannel = connection.detachChannel();
       
    56         this.initial = initial;
       
    57 
       
    58         SocketChannel chan = connection.channel();
       
    59         client.cancelRegistration(chan);
       
    60         // Constructing a RawChannel is supposed to have a "hand over"
       
    61         // semantics, in other words if construction fails, the channel won't be
       
    62         // needed by anyone, in which case someone still needs to close it
       
    63         try {
       
    64             chan.configureBlocking(false);
       
    65         } catch (IOException e) {
       
    66             try {
       
    67                 chan.close();
       
    68             } catch (IOException e1) {
       
    69                 e.addSuppressed(e1);
       
    70             } finally {
       
    71                 detachedChannel.close();
       
    72             }
       
    73             throw e;
       
    74         }
       
    75     }
       
    76 
       
    77     private class NonBlockingRawAsyncEvent extends AsyncEvent {
       
    78 
       
    79         private final RawEvent re;
       
    80 
       
    81         NonBlockingRawAsyncEvent(RawEvent re) {
       
    82             // !BLOCKING & !REPEATING
       
    83             this.re = re;
       
    84         }
       
    85 
       
    86         @Override
       
    87         public SelectableChannel channel() {
       
    88             return detachedChannel.channel();
       
    89         }
       
    90 
       
    91         @Override
       
    92         public int interestOps() {
       
    93             return re.interestOps();
       
    94         }
       
    95 
       
    96         @Override
       
    97         public void handle() {
       
    98             re.handle();
       
    99         }
       
   100 
       
   101         @Override
       
   102         public void abort(IOException ioe) { }
       
   103     }
       
   104 
       
   105     @Override
       
   106     public void registerEvent(RawEvent event) throws IOException {
       
   107         client.registerEvent(new NonBlockingRawAsyncEvent(event));
       
   108     }
       
   109 
       
   110     @Override
       
   111     public ByteBuffer read() throws IOException {
       
   112         assert !detachedChannel.channel().isBlocking();
       
   113         // connection.read() will no longer be available.
       
   114         return detachedChannel.read();
       
   115     }
       
   116 
       
   117     @Override
       
   118     public ByteBuffer initialByteBuffer() {
       
   119         synchronized (initialLock) {
       
   120             if (initial == null) {
       
   121                 throw new IllegalStateException();
       
   122             }
       
   123             ByteBuffer ref = initial.get();
       
   124             ref = ref.hasRemaining() ? Utils.copy(ref)
       
   125                     : Utils.EMPTY_BYTEBUFFER;
       
   126             initial = null;
       
   127             return ref;
       
   128         }
       
   129     }
       
   130 
       
   131     @Override
       
   132     public long write(ByteBuffer[] src, int offset, int len) throws IOException {
       
   133         // this makes the whitebox driver test fail.
       
   134         return detachedChannel.write(src, offset, len);
       
   135     }
       
   136 
       
   137     @Override
       
   138     public void shutdownInput() throws IOException {
       
   139         detachedChannel.shutdownInput();
       
   140     }
       
   141 
       
   142     @Override
       
   143     public void shutdownOutput() throws IOException {
       
   144         detachedChannel.shutdownOutput();
       
   145     }
       
   146 
       
   147     @Override
       
   148     public void close() throws IOException {
       
   149         detachedChannel.close();
       
   150     }
       
   151 
       
   152     @Override
       
   153     public String toString() {
       
   154         return super.toString()+"("+ detachedChannel.toString() + ")";
       
   155     }
       
   156 
       
   157 
       
   158 }