1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.squareup.okhttp.internal.spdy;
18 
19 import java.io.Closeable;
20 import java.io.IOException;
21 import java.util.List;
22 import okio.Buffer;
23 
24 /** Writes transport frames for SPDY/3 or HTTP/2. */
25 public interface FrameWriter extends Closeable {
26   /** HTTP/2 only. */
connectionPreface()27   void connectionPreface() throws IOException;
28   /** Informs the peer that we've applied its latest settings. */
ackSettings(Settings peerSettings)29   void ackSettings(Settings peerSettings) throws IOException;
30 
31   /**
32    * HTTP/2 only. Send a push promise header block.
33    * <p>
34    * A push promise contains all the headers that pertain to a server-initiated
35    * request, and a {@code promisedStreamId} to which response frames will be
36    * delivered. Push promise frames are sent as a part of the response to
37    * {@code streamId}.  The {@code promisedStreamId} has a priority of one
38    * greater than {@code streamId}.
39    *
40    * @param streamId client-initiated stream ID.  Must be an odd number.
41    * @param promisedStreamId server-initiated stream ID.  Must be an even
42    * number.
43    * @param requestHeaders minimally includes {@code :method}, {@code :scheme},
44    * {@code :authority}, and (@code :path}.
45    */
pushPromise(int streamId, int promisedStreamId, List<Header> requestHeaders)46   void pushPromise(int streamId, int promisedStreamId, List<Header> requestHeaders)
47       throws IOException;
48 
49   /** SPDY/3 only. */
flush()50   void flush() throws IOException;
synStream(boolean outFinished, boolean inFinished, int streamId, int associatedStreamId, List<Header> headerBlock)51   void synStream(boolean outFinished, boolean inFinished, int streamId, int associatedStreamId,
52       List<Header> headerBlock) throws IOException;
synReply(boolean outFinished, int streamId, List<Header> headerBlock)53   void synReply(boolean outFinished, int streamId, List<Header> headerBlock)
54       throws IOException;
headers(int streamId, List<Header> headerBlock)55   void headers(int streamId, List<Header> headerBlock) throws IOException;
rstStream(int streamId, ErrorCode errorCode)56   void rstStream(int streamId, ErrorCode errorCode) throws IOException;
57 
58   /** The maximum size of bytes that may be sent in a single call to {@link #data}. */
maxDataLength()59   int maxDataLength();
60 
61   /**
62    * {@code source.length} may be longer than the max length of the variant's data frame.
63    * Implementations must send multiple frames as necessary.
64    *
65    * @param source the buffer to draw bytes from. May be null if byteCount is 0.
66    * @param byteCount must be between 0 and the minimum of {code source.length}
67    * and {@link #maxDataLength}.
68    */
data(boolean outFinished, int streamId, Buffer source, int byteCount)69   void data(boolean outFinished, int streamId, Buffer source, int byteCount) throws IOException;
70 
71   /** Write okhttp's settings to the peer. */
settings(Settings okHttpSettings)72   void settings(Settings okHttpSettings) throws IOException;
73 
74   /**
75    *  Send a connection-level ping to the peer.  {@code ack} indicates this is
76    *  a reply.  Payload parameters are different between SPDY/3 and HTTP/2.
77    *  <p>
78    *  In SPDY/3, only the first {@code payload1} parameter is sent.  If the
79    *  sender is a client, it is an unsigned odd number.  Likewise, a server
80    *  will send an even number.
81    *  <p>
82    *  In HTTP/2, both {@code payload1} and {@code payload2} parameters are
83    *  sent.  The data is opaque binary, and there are no rules on the content.
84    */
ping(boolean ack, int payload1, int payload2)85   void ping(boolean ack, int payload1, int payload2) throws IOException;
86 
87   /**
88    * Tell the peer to stop creating streams and that we last processed
89    * {@code lastGoodStreamId}, or zero if no streams were processed.
90    *
91    * @param lastGoodStreamId the last stream ID processed, or zero if no
92    * streams were processed.
93    * @param errorCode reason for closing the connection.
94    * @param debugData only valid for HTTP/2; opaque debug data to send.
95    */
goAway(int lastGoodStreamId, ErrorCode errorCode, byte[] debugData)96   void goAway(int lastGoodStreamId, ErrorCode errorCode, byte[] debugData) throws IOException;
97 
98   /**
99    * Inform peer that an additional {@code windowSizeIncrement} bytes can be
100    * sent on {@code streamId}, or the connection if {@code streamId} is zero.
101    */
windowUpdate(int streamId, long windowSizeIncrement)102   void windowUpdate(int streamId, long windowSizeIncrement) throws IOException;
103 }
104