OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_HTTP_BIDIRECTIONAL_STREAM_H_ | 5 #ifndef NET_HTTP_BIDIRECTIONAL_STREAM_H_ |
6 #define NET_HTTP_BIDIRECTIONAL_STREAM_H_ | 6 #define NET_HTTP_BIDIRECTIONAL_STREAM_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
(...skipping 13 matching lines...) Expand all Loading... |
24 class HttpNetworkSession; | 24 class HttpNetworkSession; |
25 class HttpStream; | 25 class HttpStream; |
26 class HttpStreamRequest; | 26 class HttpStreamRequest; |
27 class IOBuffer; | 27 class IOBuffer; |
28 class ProxyInfo; | 28 class ProxyInfo; |
29 class SpdyHeaderBlock; | 29 class SpdyHeaderBlock; |
30 struct BidirectionalStreamRequestInfo; | 30 struct BidirectionalStreamRequestInfo; |
31 struct SSLConfig; | 31 struct SSLConfig; |
32 | 32 |
33 // A class to do HTTP/2 bidirectional streaming. Note that at most one each of | 33 // A class to do HTTP/2 bidirectional streaming. Note that at most one each of |
34 // ReadData or SendData should be in flight until the operation completes. | 34 // ReadData or SendData/SendvData should be in flight until the operation |
35 // The BidirectionalStream must be torn down before the HttpNetworkSession. | 35 // completes. The BidirectionalStream must be torn down before the |
| 36 // HttpNetworkSession. |
36 class NET_EXPORT BidirectionalStream | 37 class NET_EXPORT BidirectionalStream |
37 : public NON_EXPORTED_BASE(BidirectionalStreamImpl::Delegate), | 38 : public NON_EXPORTED_BASE(BidirectionalStreamImpl::Delegate), |
38 public NON_EXPORTED_BASE(HttpStreamRequest::Delegate) { | 39 public NON_EXPORTED_BASE(HttpStreamRequest::Delegate) { |
39 public: | 40 public: |
40 // Delegate interface to get notified of success of failure. Callbacks will be | 41 // Delegate interface to get notified of success of failure. Callbacks will be |
41 // invoked asynchronously. | 42 // invoked asynchronously. |
42 class NET_EXPORT Delegate { | 43 class NET_EXPORT Delegate { |
43 public: | 44 public: |
44 Delegate(); | 45 Delegate(); |
45 | 46 |
46 // Called when headers have been sent. This is called at most once for | 47 // Called when the stream is ready for writing and reading. This is called |
47 // the lifetime of a stream. | 48 // at most once for the lifetime of a stream. |
48 // The delegate may call BidirectionalStream::ReadData to start reading, | 49 // The delegate may call BidirectionalStream::ReadData to start reading, |
49 // or call BidirectionalStream::SendData to send data. | 50 // or call BidirectionalStream::SendData to send data. |
50 // The delegate should not call BidirectionalStream::Cancel | 51 // The delegate should not call BidirectionalStream::Cancel |
51 // during this callback. | 52 // during this callback. |
52 virtual void OnHeadersSent() = 0; | 53 virtual void OnStreamReady() = 0; |
53 | 54 |
54 // Called when headers are received. This is called at most once for the | 55 // Called when headers are received. This is called at most once for the |
55 // lifetime of a stream. | 56 // lifetime of a stream. |
56 // The delegate may call BidirectionalStream::ReadData to start reading, | 57 // The delegate may call BidirectionalStream::ReadData to start reading, |
57 // call BidirectionalStream::SendData to send data, | 58 // call BidirectionalStream::SendData to send data, |
58 // or call BidirectionalStream::Cancel to cancel the stream. | 59 // or call BidirectionalStream::Cancel to cancel the stream. |
59 virtual void OnHeadersReceived(const SpdyHeaderBlock& response_headers) = 0; | 60 virtual void OnHeadersReceived(const SpdyHeaderBlock& response_headers) = 0; |
60 | 61 |
61 // Called when a pending read is completed asynchronously. | 62 // Called when a pending read is completed asynchronously. |
62 // |bytes_read| specifies how much data is read. | 63 // |bytes_read| specifies how much data is read. |
(...skipping 26 matching lines...) Expand all Loading... |
89 DISALLOW_COPY_AND_ASSIGN(Delegate); | 90 DISALLOW_COPY_AND_ASSIGN(Delegate); |
90 }; | 91 }; |
91 | 92 |
92 // Constructs a BidirectionalStream. |request_info| contains information about | 93 // Constructs a BidirectionalStream. |request_info| contains information about |
93 // the request, and must be non-NULL. |session| is the http network session | 94 // the request, and must be non-NULL. |session| is the http network session |
94 // with which this request will be made. |delegate| must be non-NULL. | 95 // with which this request will be made. |delegate| must be non-NULL. |
95 // |session| and |delegate| must outlive |this|. | 96 // |session| and |delegate| must outlive |this|. |
96 BidirectionalStream( | 97 BidirectionalStream( |
97 std::unique_ptr<BidirectionalStreamRequestInfo> request_info, | 98 std::unique_ptr<BidirectionalStreamRequestInfo> request_info, |
98 HttpNetworkSession* session, | 99 HttpNetworkSession* session, |
| 100 bool disable_auto_flush, |
99 Delegate* delegate); | 101 Delegate* delegate); |
100 | 102 |
101 // Constructor that accepts a Timer, which can be used in tests to control | 103 // Constructor that accepts a Timer, which can be used in tests to control |
102 // the buffering of received data. | 104 // the buffering of received data. |
103 BidirectionalStream( | 105 BidirectionalStream( |
104 std::unique_ptr<BidirectionalStreamRequestInfo> request_info, | 106 std::unique_ptr<BidirectionalStreamRequestInfo> request_info, |
105 HttpNetworkSession* session, | 107 HttpNetworkSession* session, |
| 108 bool disable_auto_flush, |
106 Delegate* delegate, | 109 Delegate* delegate, |
107 std::unique_ptr<base::Timer> timer); | 110 std::unique_ptr<base::Timer> timer); |
108 | 111 |
109 // Cancels |stream_request_| or |stream_impl_| if applicable. | 112 // Cancels |stream_request_| or |stream_impl_| if applicable. |
110 // |this| should not be destroyed during Delegate::OnHeadersSent or | 113 // |this| should not be destroyed during Delegate::OnHeadersSent or |
111 // Delegate::OnDataSent. | 114 // Delegate::OnDataSent. |
112 ~BidirectionalStream() override; | 115 ~BidirectionalStream() override; |
113 | 116 |
114 // Reads at most |buf_len| bytes into |buf|. Returns the number of bytes read, | 117 // Reads at most |buf_len| bytes into |buf|. Returns the number of bytes read, |
115 // or ERR_IO_PENDING if the read is to be completed asynchronously, or an | 118 // or ERR_IO_PENDING if the read is to be completed asynchronously, or an |
116 // error code if any error occurred. If returns 0, there is no more data to | 119 // error code if any error occurred. If returns 0, there is no more data to |
117 // read. This should not be called before Delegate::OnHeadersReceived is | 120 // read. This should not be called before Delegate::OnHeadersReceived is |
118 // invoked, and should not be called again unless it returns with number | 121 // invoked, and should not be called again unless it returns with number |
119 // greater than 0 or until Delegate::OnDataRead is invoked. | 122 // greater than 0 or until Delegate::OnDataRead is invoked. |
120 int ReadData(IOBuffer* buf, int buf_len); | 123 int ReadData(IOBuffer* buf, int buf_len); |
121 | 124 |
122 // Sends data. This should not be called before Delegate::OnHeadersSent is | 125 // Sends data. This should not be called before Delegate::OnHeadersSent is |
123 // invoked, and should not be called again until Delegate::OnDataSent is | 126 // invoked, and should not be called again until Delegate::OnDataSent is |
124 // invoked. If |end_stream| is true, the DATA frame will have an END_STREAM | 127 // invoked. If |end_stream| is true, the DATA frame will have an END_STREAM |
125 // flag. | 128 // flag. |
126 void SendData(IOBuffer* data, int length, bool end_stream); | 129 void SendData(IOBuffer* data, int length, bool end_stream); |
127 | 130 |
| 131 // Same as SendData except this takes in a vector of IOBuffers. |
| 132 void SendvData(const std::vector<IOBuffer*>& buffers, |
| 133 const std::vector<int>& lengths, |
| 134 bool end_stream); |
| 135 |
128 // If |stream_request_| is non-NULL, cancel it. If |stream_impl_| is | 136 // If |stream_request_| is non-NULL, cancel it. If |stream_impl_| is |
129 // established, cancel it. No delegate method will be called after Cancel(). | 137 // established, cancel it. No delegate method will be called after Cancel(). |
130 // Any pending operations may or may not succeed. | 138 // Any pending operations may or may not succeed. |
131 void Cancel(); | 139 void Cancel(); |
132 | 140 |
133 // Returns the protocol used by this stream. If stream has not been | 141 // Returns the protocol used by this stream. If stream has not been |
134 // established, return kProtoUnknown. | 142 // established, return kProtoUnknown. |
135 NextProto GetProtocol() const; | 143 NextProto GetProtocol() const; |
136 | 144 |
137 // Total number of bytes received over the network of SPDY data, headers, and | 145 // Total number of bytes received over the network of SPDY data, headers, and |
138 // push_promise frames associated with this stream, including the size of | 146 // push_promise frames associated with this stream, including the size of |
139 // frame headers, after SSL decryption and not including proxy overhead. | 147 // frame headers, after SSL decryption and not including proxy overhead. |
140 // If stream has not been established, return 0. | 148 // If stream has not been established, return 0. |
141 int64_t GetTotalReceivedBytes() const; | 149 int64_t GetTotalReceivedBytes() const; |
142 | 150 |
143 // Total number of bytes sent over the network of SPDY frames associated with | 151 // Total number of bytes sent over the network of SPDY frames associated with |
144 // this stream, including the size of frame headers, before SSL encryption and | 152 // this stream, including the size of frame headers, before SSL encryption and |
145 // not including proxy overhead. Note that some SPDY frames such as pings are | 153 // not including proxy overhead. Note that some SPDY frames such as pings are |
146 // not associated with any stream, and are not included in this value. | 154 // not associated with any stream, and are not included in this value. |
147 int64_t GetTotalSentBytes() const; | 155 int64_t GetTotalSentBytes() const; |
148 | 156 |
149 // TODO(xunjieli): Implement a method to do flow control and a method to ping | 157 // TODO(xunjieli): Implement a method to do flow control and a method to ping |
150 // remote end point. | 158 // remote end point. |
151 | 159 |
152 private: | 160 private: |
153 // BidirectionalStreamImpl::Delegate implementation: | 161 // BidirectionalStreamImpl::Delegate implementation: |
154 void OnHeadersSent() override; | 162 void OnStreamReady() override; |
155 void OnHeadersReceived(const SpdyHeaderBlock& response_headers) override; | 163 void OnHeadersReceived(const SpdyHeaderBlock& response_headers) override; |
156 void OnDataRead(int bytes_read) override; | 164 void OnDataRead(int bytes_read) override; |
157 void OnDataSent() override; | 165 void OnDataSent() override; |
158 void OnTrailersReceived(const SpdyHeaderBlock& trailers) override; | 166 void OnTrailersReceived(const SpdyHeaderBlock& trailers) override; |
159 void OnFailed(int error) override; | 167 void OnFailed(int error) override; |
160 | 168 |
161 // HttpStreamRequest::Delegate implementation: | 169 // HttpStreamRequest::Delegate implementation: |
162 void OnStreamReady(const SSLConfig& used_ssl_config, | 170 void OnStreamReady(const SSLConfig& used_ssl_config, |
163 const ProxyInfo& used_proxy_info, | 171 const ProxyInfo& used_proxy_info, |
164 HttpStream* stream) override; | 172 HttpStream* stream) override; |
(...skipping 22 matching lines...) Expand all Loading... |
187 const ProxyInfo& used_proxy_info, | 195 const ProxyInfo& used_proxy_info, |
188 HttpStream* stream) override; | 196 HttpStream* stream) override; |
189 void OnQuicBroken() override; | 197 void OnQuicBroken() override; |
190 | 198 |
191 // BidirectionalStreamRequestInfo used when requesting the stream. | 199 // BidirectionalStreamRequestInfo used when requesting the stream. |
192 std::unique_ptr<BidirectionalStreamRequestInfo> request_info_; | 200 std::unique_ptr<BidirectionalStreamRequestInfo> request_info_; |
193 const BoundNetLog net_log_; | 201 const BoundNetLog net_log_; |
194 | 202 |
195 HttpNetworkSession* session_; | 203 HttpNetworkSession* session_; |
196 | 204 |
| 205 bool disable_auto_flush_; |
197 Delegate* const delegate_; | 206 Delegate* const delegate_; |
198 | 207 |
199 // Timer used to buffer data received in short time-spans and send a single | 208 // Timer used to buffer data received in short time-spans and send a single |
200 // read completion notification. | 209 // read completion notification. |
201 std::unique_ptr<base::Timer> timer_; | 210 std::unique_ptr<base::Timer> timer_; |
202 // HttpStreamRequest used to request a BidirectionalStreamImpl. This is NULL | 211 // HttpStreamRequest used to request a BidirectionalStreamImpl. This is NULL |
203 // if the request has been canceled or completed. | 212 // if the request has been canceled or completed. |
204 std::unique_ptr<HttpStreamRequest> stream_request_; | 213 std::unique_ptr<HttpStreamRequest> stream_request_; |
205 // The underlying BidirectioanlStreamImpl used for this stream. It is | 214 // The underlying BidirectioanlStreamImpl used for this stream. It is |
206 // non-NULL, if the |stream_request_| successfully finishes. | 215 // non-NULL, if the |stream_request_| successfully finishes. |
207 std::unique_ptr<BidirectionalStreamImpl> stream_impl_; | 216 std::unique_ptr<BidirectionalStreamImpl> stream_impl_; |
208 | 217 |
209 // Buffer used for reading. | 218 // Buffer used for reading. |
210 scoped_refptr<IOBuffer> read_buffer_; | 219 scoped_refptr<IOBuffer> read_buffer_; |
211 // Buffer used for writing. | 220 // List of buffers used for writing. |
212 scoped_refptr<IOBuffer> write_buffer_; | 221 std::vector<scoped_refptr<IOBuffer>> write_buffer_list_; |
213 // Length of |write_buffer_|. | 222 // List of buffer length. |
214 size_t write_buffer_len_; | 223 std::vector<int> write_buffer_len_list_; |
215 | 224 |
216 DISALLOW_COPY_AND_ASSIGN(BidirectionalStream); | 225 DISALLOW_COPY_AND_ASSIGN(BidirectionalStream); |
217 }; | 226 }; |
218 | 227 |
219 } // namespace net | 228 } // namespace net |
220 | 229 |
221 #endif // NET_HTTP_BIDIRECTIONAL_STREAM_H_ | 230 #endif // NET_HTTP_BIDIRECTIONAL_STREAM_H_ |
OLD | NEW |