simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 1 | package com.google.net.stubby.newtransport.okhttp; |
| 2 | |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 3 | import com.google.common.annotations.VisibleForTesting; |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 4 | import com.google.common.base.Preconditions; |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 5 | import com.google.common.io.ByteStreams; |
nathanmittler | 43d2fcc | 2014-08-29 21:58:54 -0700 | [diff] [blame] | 6 | import com.google.common.util.concurrent.ListenableFuture; |
lryan | e4bd1c7 | 2014-09-08 14:03:35 -0700 | [diff] [blame^] | 7 | import com.google.net.stubby.Metadata; |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 8 | import com.google.net.stubby.MethodDescriptor; |
| 9 | import com.google.net.stubby.Status; |
zhangkun | 048649e | 2014-08-28 15:52:03 -0700 | [diff] [blame] | 10 | import com.google.net.stubby.newtransport.AbstractClientStream; |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 11 | import com.google.net.stubby.newtransport.AbstractClientTransport; |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 12 | import com.google.net.stubby.newtransport.ClientStream; |
| 13 | import com.google.net.stubby.newtransport.ClientTransport; |
| 14 | import com.google.net.stubby.newtransport.InputStreamDeframer; |
| 15 | import com.google.net.stubby.newtransport.StreamListener; |
simonma | 1966d5b | 2014-08-08 14:51:01 -0700 | [diff] [blame] | 16 | import com.google.net.stubby.newtransport.StreamState; |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 17 | import com.google.net.stubby.transport.Transport; |
| 18 | import com.google.net.stubby.transport.Transport.Code; |
| 19 | |
| 20 | import com.squareup.okhttp.internal.spdy.ErrorCode; |
| 21 | import com.squareup.okhttp.internal.spdy.FrameReader; |
| 22 | import com.squareup.okhttp.internal.spdy.Header; |
| 23 | import com.squareup.okhttp.internal.spdy.HeadersMode; |
brettmorgan | 56f5ec3 | 2014-07-18 16:54:50 -0700 | [diff] [blame] | 24 | import com.squareup.okhttp.internal.spdy.Http20Draft12; |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 25 | import com.squareup.okhttp.internal.spdy.Settings; |
| 26 | import com.squareup.okhttp.internal.spdy.Variant; |
| 27 | |
zhangkun | 048649e | 2014-08-28 15:52:03 -0700 | [diff] [blame] | 28 | import okio.Buffer; |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 29 | import okio.BufferedSink; |
| 30 | import okio.BufferedSource; |
zhangkun | 048649e | 2014-08-28 15:52:03 -0700 | [diff] [blame] | 31 | import okio.ByteString; |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 32 | import okio.Okio; |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 33 | |
| 34 | import java.io.IOException; |
| 35 | import java.net.Socket; |
| 36 | import java.nio.ByteBuffer; |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 37 | import java.util.ArrayList; |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 38 | import java.util.Collections; |
| 39 | import java.util.HashMap; |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 40 | import java.util.Iterator; |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 41 | import java.util.List; |
| 42 | import java.util.Map; |
simonma | f2c4c85 | 2014-08-06 10:00:56 -0700 | [diff] [blame] | 43 | import java.util.concurrent.Executor; |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 44 | |
| 45 | import javax.annotation.concurrent.GuardedBy; |
| 46 | |
| 47 | /** |
| 48 | * A okhttp-based {@link ClientTransport} implementation. |
| 49 | */ |
| 50 | public class OkHttpClientTransport extends AbstractClientTransport { |
| 51 | /** The default initial window size in HTTP/2 is 64 KiB for the stream and connection. */ |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 52 | @VisibleForTesting |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 53 | static final int DEFAULT_INITIAL_WINDOW_SIZE = 64 * 1024; |
| 54 | |
simonma | 8f3e9ee | 2014-09-05 11:02:52 -0700 | [diff] [blame] | 55 | private static final Map<ErrorCode, Status> ERROR_CODE_TO_STATUS; |
| 56 | static { |
| 57 | Map<ErrorCode, Status> errorToStatus = new HashMap<ErrorCode, Status>(); |
| 58 | errorToStatus.put(ErrorCode.NO_ERROR, Status.OK); |
| 59 | errorToStatus.put(ErrorCode.PROTOCOL_ERROR, |
| 60 | new Status(Transport.Code.INTERNAL, "Protocol error")); |
| 61 | errorToStatus.put(ErrorCode.INVALID_STREAM, |
| 62 | new Status(Transport.Code.INTERNAL, "Invalid stream")); |
| 63 | errorToStatus.put(ErrorCode.UNSUPPORTED_VERSION, |
| 64 | new Status(Transport.Code.INTERNAL, "Unsupported version")); |
| 65 | errorToStatus.put(ErrorCode.STREAM_IN_USE, |
| 66 | new Status(Transport.Code.INTERNAL, "Stream in use")); |
| 67 | errorToStatus.put(ErrorCode.STREAM_ALREADY_CLOSED, |
| 68 | new Status(Transport.Code.INTERNAL, "Stream already closed")); |
| 69 | errorToStatus.put(ErrorCode.INTERNAL_ERROR, |
| 70 | new Status(Transport.Code.INTERNAL, "Internal error")); |
| 71 | errorToStatus.put(ErrorCode.FLOW_CONTROL_ERROR, |
| 72 | new Status(Transport.Code.INTERNAL, "Flow control error")); |
| 73 | errorToStatus.put(ErrorCode.STREAM_CLOSED, |
| 74 | new Status(Transport.Code.INTERNAL, "Stream closed")); |
| 75 | errorToStatus.put(ErrorCode.FRAME_TOO_LARGE, |
| 76 | new Status(Transport.Code.INTERNAL, "Frame too large")); |
| 77 | errorToStatus.put(ErrorCode.REFUSED_STREAM, |
| 78 | new Status(Transport.Code.INTERNAL, "Refused stream")); |
| 79 | errorToStatus.put(ErrorCode.CANCEL, new Status(Transport.Code.CANCELLED, "Cancelled")); |
| 80 | errorToStatus.put(ErrorCode.COMPRESSION_ERROR, |
| 81 | new Status(Transport.Code.INTERNAL, "Compression error")); |
| 82 | errorToStatus.put(ErrorCode.INVALID_CREDENTIALS, |
| 83 | new Status(Transport.Code.PERMISSION_DENIED, "Invalid credentials")); |
| 84 | ERROR_CODE_TO_STATUS = Collections.unmodifiableMap(errorToStatus); |
| 85 | } |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 86 | |
| 87 | private final String host; |
| 88 | private final int port; |
| 89 | private FrameReader frameReader; |
| 90 | private AsyncFrameWriter frameWriter; |
lryan | e4bd1c7 | 2014-09-08 14:03:35 -0700 | [diff] [blame^] | 91 | private final Object lock = new Object(); |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 92 | @GuardedBy("lock") |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 93 | private int nextStreamId; |
| 94 | private final Map<Integer, OkHttpClientStream> streams = |
| 95 | Collections.synchronizedMap(new HashMap<Integer, OkHttpClientStream>()); |
simonma | f2c4c85 | 2014-08-06 10:00:56 -0700 | [diff] [blame] | 96 | private final Executor executor; |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 97 | private int unacknowledgedBytesRead; |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 98 | private ClientFrameHandler clientFrameHandler; |
| 99 | // The status used to finish all active streams when the transport is closed. |
| 100 | @GuardedBy("lock") |
| 101 | private boolean goAway; |
| 102 | @GuardedBy("lock") |
| 103 | private Status goAwayStatus; |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 104 | |
simonma | f2c4c85 | 2014-08-06 10:00:56 -0700 | [diff] [blame] | 105 | OkHttpClientTransport(String host, int port, Executor executor) { |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 106 | this.host = Preconditions.checkNotNull(host); |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 107 | this.port = port; |
simonma | f2c4c85 | 2014-08-06 10:00:56 -0700 | [diff] [blame] | 108 | this.executor = Preconditions.checkNotNull(executor); |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 109 | // Client initiated streams are odd, server initiated ones are even. Server should not need to |
| 110 | // use it. We start clients at 3 to avoid conflicting with HTTP negotiation. |
| 111 | nextStreamId = 3; |
| 112 | } |
| 113 | |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 114 | /** |
| 115 | * Create a transport connected to a fake peer for test. |
| 116 | */ |
| 117 | @VisibleForTesting |
simonma | f2c4c85 | 2014-08-06 10:00:56 -0700 | [diff] [blame] | 118 | OkHttpClientTransport(Executor executor, FrameReader frameReader, AsyncFrameWriter frameWriter, |
| 119 | int nextStreamId) { |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 120 | host = null; |
| 121 | port = -1; |
simonma | f2c4c85 | 2014-08-06 10:00:56 -0700 | [diff] [blame] | 122 | this.executor = Preconditions.checkNotNull(executor); |
| 123 | this.frameReader = Preconditions.checkNotNull(frameReader); |
| 124 | this.frameWriter = Preconditions.checkNotNull(frameWriter); |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 125 | this.nextStreamId = nextStreamId; |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 126 | } |
| 127 | |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 128 | @Override |
lryan | e4bd1c7 | 2014-09-08 14:03:35 -0700 | [diff] [blame^] | 129 | protected ClientStream newStreamInternal(MethodDescriptor<?, ?> method, |
| 130 | Metadata.Headers headers, |
| 131 | StreamListener listener) { |
| 132 | return new OkHttpClientStream(method, headers.serialize(), listener); |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override |
| 136 | protected void doStart() { |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 137 | // We set host to null for test. |
| 138 | if (host != null) { |
| 139 | BufferedSource source; |
| 140 | BufferedSink sink; |
| 141 | try { |
| 142 | Socket socket = new Socket(host, port); |
| 143 | source = Okio.buffer(Okio.source(socket)); |
| 144 | sink = Okio.buffer(Okio.sink(socket)); |
| 145 | } catch (IOException e) { |
| 146 | throw new RuntimeException(e); |
| 147 | } |
| 148 | Variant variant = new Http20Draft12(); |
| 149 | frameReader = variant.newReader(source, true); |
| 150 | frameWriter = new AsyncFrameWriter(variant.newWriter(sink, true), this, executor); |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 151 | } |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 152 | |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 153 | notifyStarted(); |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 154 | clientFrameHandler = new ClientFrameHandler(); |
| 155 | executor.execute(clientFrameHandler); |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | @Override |
| 159 | protected void doStop() { |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 160 | boolean normalClose; |
| 161 | synchronized (lock) { |
| 162 | normalClose = !goAway; |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 163 | } |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 164 | if (normalClose) { |
| 165 | abort(new Status(Code.INTERNAL, "Transport stopped")); |
| 166 | // Send GOAWAY with lastGoodStreamId of 0, since we don't expect any server-initiated streams. |
| 167 | // The GOAWAY is part of graceful shutdown. |
simonma | b645b38 | 2014-08-26 10:58:45 -0700 | [diff] [blame] | 168 | frameWriter.goAway(0, ErrorCode.NO_ERROR, new byte[0]); |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 169 | } |
| 170 | stopIfNecessary(); |
| 171 | } |
| 172 | |
| 173 | @VisibleForTesting |
| 174 | ClientFrameHandler getHandler() { |
| 175 | return clientFrameHandler; |
| 176 | } |
| 177 | |
| 178 | @VisibleForTesting |
| 179 | Map<Integer, OkHttpClientStream> getStreams() { |
| 180 | return streams; |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /** |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 184 | * Finish all active streams with given status, then close the transport. |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 185 | */ |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 186 | void abort(Status status) { |
| 187 | onGoAway(-1, status); |
| 188 | } |
| 189 | |
| 190 | private void onGoAway(int lastKnownStreamId, Status status) { |
| 191 | ArrayList<OkHttpClientStream> goAwayStreams = new ArrayList<OkHttpClientStream>(); |
| 192 | synchronized (lock) { |
| 193 | goAway = true; |
| 194 | goAwayStatus = status; |
| 195 | Iterator<Map.Entry<Integer, OkHttpClientStream>> it = streams.entrySet().iterator(); |
| 196 | while (it.hasNext()) { |
| 197 | Map.Entry<Integer, OkHttpClientStream> entry = it.next(); |
| 198 | if (entry.getKey() > lastKnownStreamId) { |
| 199 | goAwayStreams.add(entry.getValue()); |
| 200 | it.remove(); |
| 201 | } |
| 202 | } |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 203 | } |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 204 | |
| 205 | // Starting stop, go into STOPPING state so that Channel know this Transport should not be used |
| 206 | // further, will become STOPPED once all streams are complete. |
| 207 | stopAsync(); |
| 208 | |
| 209 | for (OkHttpClientStream stream : goAwayStreams) { |
lryan | e4bd1c7 | 2014-09-08 14:03:35 -0700 | [diff] [blame^] | 210 | stream.setStatus(status, new Metadata.Trailers()); |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
| 214 | /** |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 215 | * Called when a stream is closed. |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 216 | * |
| 217 | * <p> Return false if the stream has already finished. |
| 218 | */ |
| 219 | private boolean finishStream(int streamId, Status status) { |
| 220 | OkHttpClientStream stream; |
| 221 | stream = streams.remove(streamId); |
| 222 | if (stream != null) { |
| 223 | // This is mainly for failed streams, for successfully finished streams, it's a no-op. |
lryan | e4bd1c7 | 2014-09-08 14:03:35 -0700 | [diff] [blame^] | 224 | stream.setStatus(status, new Metadata.Trailers()); |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 225 | return true; |
| 226 | } |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | /** |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 231 | * When the transport is in goAway states, we should stop it once all active streams finish. |
| 232 | */ |
| 233 | private void stopIfNecessary() { |
| 234 | boolean shouldStop; |
| 235 | synchronized (lock) { |
| 236 | shouldStop = (goAway && streams.size() == 0); |
| 237 | } |
| 238 | if (shouldStop) { |
| 239 | frameWriter.close(); |
| 240 | try { |
| 241 | frameReader.close(); |
| 242 | } catch (IOException e) { |
| 243 | throw new RuntimeException(e); |
| 244 | } |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 245 | notifyStopped(); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Returns a Grpc status corresponding to the given ErrorCode. |
| 251 | */ |
| 252 | @VisibleForTesting |
| 253 | static Status toGrpcStatus(ErrorCode code) { |
| 254 | return ERROR_CODE_TO_STATUS.get(code); |
| 255 | } |
| 256 | |
| 257 | /** |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 258 | * Runnable which reads frames and dispatches them to in flight calls |
| 259 | */ |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 260 | @VisibleForTesting |
| 261 | class ClientFrameHandler implements FrameReader.Handler, Runnable { |
| 262 | ClientFrameHandler() {} |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 263 | |
| 264 | @Override |
| 265 | public void run() { |
| 266 | String threadName = Thread.currentThread().getName(); |
| 267 | Thread.currentThread().setName("OkHttpClientTransport"); |
| 268 | try { |
| 269 | // Read until the underlying socket closes. |
| 270 | while (frameReader.nextFrame(this)) { |
| 271 | } |
| 272 | } catch (IOException ioe) { |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 273 | abort(Status.fromThrowable(ioe)); |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 274 | } finally { |
| 275 | // Restore the original thread name. |
| 276 | Thread.currentThread().setName(threadName); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Handle a HTTP2 DATA frame |
| 282 | */ |
| 283 | @Override |
| 284 | public void data(boolean inFinished, int streamId, BufferedSource in, int length) |
| 285 | throws IOException { |
| 286 | final OkHttpClientStream stream; |
| 287 | stream = streams.get(streamId); |
| 288 | if (stream == null) { |
| 289 | frameWriter.rstStream(streamId, ErrorCode.INVALID_STREAM); |
| 290 | return; |
| 291 | } |
| 292 | InputStreamDeframer deframer = stream.getDeframer(); |
| 293 | |
| 294 | // Wait until the frame is complete. |
| 295 | in.require(length); |
| 296 | |
| 297 | deframer.deliverFrame(ByteStreams.limit(in.inputStream(), length), inFinished); |
| 298 | unacknowledgedBytesRead += length; |
| 299 | stream.unacknowledgedBytesRead += length; |
| 300 | if (unacknowledgedBytesRead >= DEFAULT_INITIAL_WINDOW_SIZE / 2) { |
| 301 | frameWriter.windowUpdate(0, unacknowledgedBytesRead); |
| 302 | unacknowledgedBytesRead = 0; |
| 303 | } |
| 304 | if (stream.unacknowledgedBytesRead >= DEFAULT_INITIAL_WINDOW_SIZE / 2) { |
| 305 | frameWriter.windowUpdate(streamId, stream.unacknowledgedBytesRead); |
| 306 | stream.unacknowledgedBytesRead = 0; |
| 307 | } |
| 308 | if (inFinished) { |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 309 | if (finishStream(streamId, Status.OK)) { |
| 310 | stopIfNecessary(); |
| 311 | } |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Handle HTTP2 HEADER and CONTINUATION frames |
| 317 | */ |
| 318 | @Override |
| 319 | public void headers(boolean outFinished, |
| 320 | boolean inFinished, |
| 321 | int streamId, |
| 322 | int associatedStreamId, |
brettmorgan | 56f5ec3 | 2014-07-18 16:54:50 -0700 | [diff] [blame] | 323 | List<Header> headerBlock, |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 324 | HeadersMode headersMode) { |
| 325 | // TODO(user): handle received headers. |
| 326 | } |
| 327 | |
| 328 | @Override |
| 329 | public void rstStream(int streamId, ErrorCode errorCode) { |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 330 | if (finishStream(streamId, toGrpcStatus(errorCode))) { |
| 331 | stopIfNecessary(); |
| 332 | } |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | @Override |
| 336 | public void settings(boolean clearPrevious, Settings settings) { |
| 337 | // not impl |
| 338 | frameWriter.ackSettings(); |
| 339 | } |
| 340 | |
| 341 | @Override |
| 342 | public void ping(boolean ack, int payload1, int payload2) { |
| 343 | if (!ack) { |
| 344 | frameWriter.ping(true, payload1, payload2); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | @Override |
| 349 | public void ackSettings() { |
| 350 | // Do nothing currently. |
| 351 | } |
| 352 | |
| 353 | @Override |
| 354 | public void goAway(int lastGoodStreamId, ErrorCode errorCode, ByteString debugData) { |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 355 | onGoAway(lastGoodStreamId, new Status(Code.UNAVAILABLE, "Go away")); |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | @Override |
| 359 | public void pushPromise(int streamId, int promisedStreamId, List<Header> requestHeaders) |
| 360 | throws IOException { |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 361 | // We don't accept server initiated stream. |
| 362 | frameWriter.rstStream(streamId, ErrorCode.PROTOCOL_ERROR); |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | @Override |
| 366 | public void windowUpdate(int arg0, long arg1) { |
| 367 | // TODO(user): flow control. |
| 368 | } |
| 369 | |
| 370 | @Override |
brettmorgan | 56f5ec3 | 2014-07-18 16:54:50 -0700 | [diff] [blame] | 371 | public void priority(int streamId, int streamDependency, int weight, boolean exclusive) { |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 372 | // Ignore priority change. |
| 373 | // TODO(user): log |
| 374 | } |
brettmorgan | 56f5ec3 | 2014-07-18 16:54:50 -0700 | [diff] [blame] | 375 | |
| 376 | @Override |
| 377 | public void alternateService(int streamId, String origin, ByteString protocol, String host, |
| 378 | int port, long maxAge) { |
| 379 | // TODO(user): Deal with alternateService propagation |
| 380 | } |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 381 | } |
| 382 | |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 383 | @GuardedBy("lock") |
| 384 | private void assignStreamId(OkHttpClientStream stream) { |
| 385 | Preconditions.checkState(stream.streamId == 0, "StreamId already assigned"); |
| 386 | stream.streamId = nextStreamId; |
| 387 | streams.put(stream.streamId, stream); |
| 388 | if (nextStreamId >= Integer.MAX_VALUE - 2) { |
| 389 | onGoAway(Integer.MAX_VALUE, new Status(Code.INTERNAL, "Stream id exhaust")); |
| 390 | } else { |
| 391 | nextStreamId += 2; |
| 392 | } |
| 393 | } |
| 394 | |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 395 | /** |
| 396 | * Client stream for the okhttp transport. |
| 397 | */ |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 398 | @VisibleForTesting |
zhangkun | 048649e | 2014-08-28 15:52:03 -0700 | [diff] [blame] | 399 | class OkHttpClientStream extends AbstractClientStream { |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 400 | int streamId; |
| 401 | final InputStreamDeframer deframer; |
| 402 | int unacknowledgedBytesRead; |
| 403 | |
lryan | e4bd1c7 | 2014-09-08 14:03:35 -0700 | [diff] [blame^] | 404 | OkHttpClientStream(MethodDescriptor<?, ?> method, byte[][] headers, StreamListener listener) { |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 405 | super(listener); |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 406 | deframer = new InputStreamDeframer(inboundMessageHandler()); |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 407 | synchronized (lock) { |
| 408 | if (goAway) { |
lryan | e4bd1c7 | 2014-09-08 14:03:35 -0700 | [diff] [blame^] | 409 | setStatus(goAwayStatus, new Metadata.Trailers()); |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 410 | return; |
| 411 | } |
| 412 | assignStreamId(this); |
| 413 | } |
| 414 | frameWriter.synStream(false, false, streamId, 0, |
lryan | e4bd1c7 | 2014-09-08 14:03:35 -0700 | [diff] [blame^] | 415 | Headers.createRequestHeaders(method.getName(), headers)); |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 416 | } |
| 417 | |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 418 | InputStreamDeframer getDeframer() { |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 419 | return deframer; |
| 420 | } |
| 421 | |
| 422 | @Override |
| 423 | protected void sendFrame(ByteBuffer frame, boolean endOfStream) { |
| 424 | Preconditions.checkState(streamId != 0, "streamId should be set"); |
simonma | 18eb63b | 2014-09-04 13:16:28 -0700 | [diff] [blame] | 425 | Buffer buffer = new Buffer(); |
| 426 | // Read the data into a buffer. |
| 427 | // TODO(user): swap to NIO buffers or zero-copy if/when okhttp/okio supports it |
| 428 | buffer.write(frame.array(), frame.arrayOffset(), frame.remaining()); |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 429 | // Write the data to the remote endpoint. |
| 430 | frameWriter.data(endOfStream, streamId, buffer); |
| 431 | frameWriter.flush(); |
| 432 | } |
| 433 | |
| 434 | @Override |
nathanmittler | 43d2fcc | 2014-08-29 21:58:54 -0700 | [diff] [blame] | 435 | protected void disableWindowUpdate(ListenableFuture<Void> processingFuture) { |
| 436 | // TODO(user): implement inbound flow control. |
| 437 | } |
| 438 | |
| 439 | @Override |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 440 | public void cancel() { |
simonma | 1966d5b | 2014-08-08 14:51:01 -0700 | [diff] [blame] | 441 | if (streamId == 0) { |
| 442 | // This should only happens when the stream was failed in constructor. |
| 443 | Preconditions.checkState(state() == StreamState.CLOSED, "A unclosed stream has no id"); |
| 444 | } |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 445 | outboundPhase = Phase.STATUS; |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 446 | if (finishStream(streamId, toGrpcStatus(ErrorCode.CANCEL))) { |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 447 | frameWriter.rstStream(streamId, ErrorCode.CANCEL); |
simonma | 7bf17dc | 2014-07-29 09:52:20 -0700 | [diff] [blame] | 448 | stopIfNecessary(); |
simonma | 77d9706 | 2014-07-18 10:22:35 -0700 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | } |
| 452 | } |