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