blob: 176dd298567458a4a56ee7a94aad5e2dd8f59642 [file] [log] [blame]
[email protected]aea80602009-09-18 00:55:081// Copyright (c) 2009 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef NET_FLIP_FLIP_SESSION_H_
6#define NET_FLIP_FLIP_SESSION_H_
7
8#include <deque>
9#include <list>
10#include <map>
11#include <queue>
12#include <string>
13
14#include "base/ref_counted.h"
15#include "net/base/io_buffer.h"
16#include "net/base/load_states.h"
17#include "net/base/net_errors.h"
[email protected]18c53ae2009-10-01 18:18:5218#include "net/base/ssl_config_service.h"
[email protected]aea80602009-09-18 00:55:0819#include "net/base/upload_data_stream.h"
20#include "net/flip/flip_framer.h"
[email protected]eebc0b42009-11-04 00:17:1321#include "net/flip/flip_io_buffer.h"
[email protected]aea80602009-09-18 00:55:0822#include "net/flip/flip_protocol.h"
23#include "net/flip/flip_session_pool.h"
24#include "net/socket/client_socket.h"
25#include "net/socket/client_socket_handle.h"
26#include "testing/platform_test.h"
27
28namespace net {
29
[email protected]cd314c822009-10-29 03:13:0630class FlipStream;
[email protected]aea80602009-09-18 00:55:0831class HttpNetworkSession;
32class HttpRequestInfo;
33class HttpResponseInfo;
34
[email protected]23297922009-10-28 20:12:3635// The FlipDelegate interface is an interface so that the FlipSession
36// can interact with the provider of a given Flip stream.
[email protected]aea80602009-09-18 00:55:0837class FlipDelegate {
38 public:
39 virtual ~FlipDelegate() {}
[email protected]23297922009-10-28 20:12:3640
41 // Accessors from the delegate.
42
43 // The delegate provides access to the HttpRequestInfo for use by the flip
44 // session.
[email protected]aea80602009-09-18 00:55:0845 virtual const HttpRequestInfo* request() = 0;
[email protected]23297922009-10-28 20:12:3646
47 // The delegate provides access to an UploadDataStream for use by the
48 // flip session. If the delegate is not uploading content, this call
49 // must return NULL.
[email protected]aea80602009-09-18 00:55:0850 virtual const UploadDataStream* data() = 0;
51
[email protected]23297922009-10-28 20:12:3652 // Callbacks.
53
54 // Called by the FlipSession when UploadData has been sent. If the
55 // request has no upload data, this call will never be called. This
56 // callback may be called multiple times if large amounts of data are
57 // being uploaded. This callback will only be called prior to the
58 // OnRequestSent callback.
59 // |result| contains the number of bytes written or an error code.
60 virtual void OnUploadDataSent(int result) = 0;
61
62 // Called by the FlipSession when the Request has been entirely sent.
63 // If the request contains upload data, all upload data has been sent.
64 // |result| contains an error code if a failure has occurred or OK
65 // on success.
66 virtual void OnRequestSent(int result) = 0;
67
68 // Called by the FlipSession when a response (e.g. a SYN_REPLY) has been
69 // received for this request. This callback will never be called prior
70 // to the OnRequestSent() callback.
[email protected]aea80602009-09-18 00:55:0871 virtual void OnResponseReceived(HttpResponseInfo* response) = 0;
[email protected]23297922009-10-28 20:12:3672
73 // Called by the FlipSession when response data has been received for this
74 // request. This callback may be called multiple times as data arrives
75 // from the network, and will never be called prior to OnResponseReceived.
76 // |buffer| contains the data received. The delegate must copy any data
77 // from this buffer before returning from this callback.
78 // |bytes| is the number of bytes received or an error.
79 // A zero-length count does not indicate end-of-stream.
[email protected]aea80602009-09-18 00:55:0880 virtual void OnDataReceived(const char* buffer, int bytes) = 0;
[email protected]23297922009-10-28 20:12:3681
82 // Called by the FlipSession when the request is finished. This callback
83 // will always be called at the end of the request and signals to the
84 // delegate that the delegate can be torn down. No further callbacks to the
85 // delegate will be made after this call.
86 // |status| is an error code or OK.
[email protected]aea80602009-09-18 00:55:0887 virtual void OnClose(int status) = 0;
[email protected]aea80602009-09-18 00:55:0888};
89
[email protected]aea80602009-09-18 00:55:0890class FlipSession : public base::RefCounted<FlipSession>,
91 public flip::FlipFramerVisitorInterface {
92 public:
[email protected]aea80602009-09-18 00:55:0893 virtual ~FlipSession();
94
95 // Get the domain for this FlipSession.
[email protected]d1eda932009-11-04 01:03:1096 const std::string& domain() const { return domain_; }
[email protected]aea80602009-09-18 00:55:0897
98 // Connect the FLIP Socket.
99 // Returns net::Error::OK on success.
100 // Note that this call does not wait for the connect to complete. Callers can
101 // immediately start using the FlipSession while it connects.
102 net::Error Connect(const std::string& group_name,
103 const HostResolver::RequestInfo& host, int priority);
104
105 // Create a new stream.
106 // FlipDelegate must remain valid until the stream is either cancelled by the
107 // creator via CancelStream or the FlipDelegate OnClose or OnCancel callbacks
108 // have been made.
109 // Once the stream is created, the delegate should wait for a callback.
110 int CreateStream(FlipDelegate* delegate);
111
112 // Cancel a stream.
113 bool CancelStream(int id);
114
115 // Check if a stream is active.
[email protected]d1eda932009-11-04 01:03:10116 bool IsStreamActive(int id) const;
[email protected]aea80602009-09-18 00:55:08117
118 // The LoadState is used for informing the user of the current network
119 // status, such as "resolving host", "connecting", etc.
120 LoadState GetLoadState() const;
[email protected]d1eda932009-11-04 01:03:10121
[email protected]aea80602009-09-18 00:55:08122 protected:
[email protected]72552f02009-10-28 15:25:01123 friend class FlipNetworkTransactionTest;
[email protected]aea80602009-09-18 00:55:08124 friend class FlipSessionPool;
[email protected]650e2cae2009-10-21 23:52:07125 friend class HttpNetworkLayer; // Temporary for server.
[email protected]aea80602009-09-18 00:55:08126
127 // Provide access to the framer for testing.
128 flip::FlipFramer* GetFramer() { return &flip_framer_; }
129
130 // Create a new FlipSession.
131 // |host| is the hostname that this session connects to.
132 FlipSession(std::string host, HttpNetworkSession* session);
133
134 // Closes all open streams. Used as part of shutdown.
135 void CloseAllStreams(net::Error code);
136
[email protected]affe8fe2009-10-14 20:06:05137 // Enable or disable SSL. This is only to be used for testing.
138 static void SetSSLMode(bool enable) { use_ssl_ = enable; }
139
[email protected]aea80602009-09-18 00:55:08140 private:
141 // FlipFramerVisitorInterface
142 virtual void OnError(flip::FlipFramer*);
143 virtual void OnStreamFrameData(flip::FlipStreamId stream_id,
144 const char* data,
[email protected]aeac1e42009-10-10 00:26:01145 size_t len);
[email protected]aea80602009-09-18 00:55:08146 virtual void OnControl(const flip::FlipControlFrame* frame);
147 virtual void OnLameDuck();
148
149 // Control frame handlers.
150 void OnSyn(const flip::FlipSynStreamControlFrame* frame,
151 const flip::FlipHeaderBlock* headers);
152 void OnSynReply(const flip::FlipSynReplyControlFrame* frame,
153 const flip::FlipHeaderBlock* headers);
154 void OnFin(const flip::FlipFinStreamControlFrame* frame);
155
156 // IO Callbacks
[email protected]18c53ae2009-10-01 18:18:52157 void OnTCPConnect(int result);
158 void OnSSLConnect(int result);
[email protected]aea80602009-09-18 00:55:08159 void OnReadComplete(int result);
160 void OnWriteComplete(int result);
161
162 // Start reading from the socket.
163 void ReadSocket();
164
165 // Write current data to the socket.
166 void WriteSocketLater();
167 void WriteSocket();
168
169 // Get a new stream id.
170 int GetNewStreamId();
171
172 // Track active streams in the active stream list.
[email protected]cd314c822009-10-29 03:13:06173 FlipStream* ActivateStream(flip::FlipStreamId id, FlipDelegate* delegate);
[email protected]ba051e312009-10-07 22:12:33174 void DeactivateStream(flip::FlipStreamId id);
[email protected]aea80602009-09-18 00:55:08175
176 // Check if we have a pending pushed-stream for this url
177 // Returns the stream if found (and returns it from the pending
178 // list), returns NULL otherwise.
[email protected]cd314c822009-10-29 03:13:06179 FlipStream* GetPushStream(std::string url);
[email protected]aea80602009-09-18 00:55:08180
181 // Callbacks for the Flip session.
182 CompletionCallbackImpl<FlipSession> connect_callback_;
[email protected]18c53ae2009-10-01 18:18:52183 CompletionCallbackImpl<FlipSession> ssl_connect_callback_;
[email protected]aea80602009-09-18 00:55:08184 CompletionCallbackImpl<FlipSession> read_callback_;
185 CompletionCallbackImpl<FlipSession> write_callback_;
186
187 // The domain this session is connected to.
188 std::string domain_;
189
[email protected]18c53ae2009-10-01 18:18:52190 SSLConfig ssl_config_;
191
[email protected]aea80602009-09-18 00:55:08192 scoped_refptr<HttpNetworkSession> session_;
193
194 // The socket handle for this session.
195 ClientSocketHandle connection_;
[email protected]18c53ae2009-10-01 18:18:52196 bool connection_started_; // Is the connect process started.
197 bool connection_ready_; // Is the connection ready for use.
[email protected]aea80602009-09-18 00:55:08198
199 // The read buffer used to read data from the socket.
200 enum { kReadBufferSize = (4 * 1024) };
[email protected]230cadd2009-09-22 16:33:59201 scoped_refptr<IOBuffer> read_buffer_;
[email protected]aea80602009-09-18 00:55:08202 bool read_pending_;
203
204 int stream_hi_water_mark_; // The next stream id to use.
205
[email protected]93300672009-10-24 13:22:51206 // TODO(mbelshe): We need to track these stream lists better.
207 // I suspect it is possible to remove a stream from
208 // one list, but not the other.
[email protected]cd314c822009-10-29 03:13:06209 typedef std::map<int, FlipStream*> ActiveStreamMap;
210 typedef std::list<FlipStream*> ActiveStreamList;
[email protected]aea80602009-09-18 00:55:08211 ActiveStreamMap active_streams_;
212
213 ActiveStreamList pushed_streams_;
[email protected]18c53ae2009-10-01 18:18:52214 // List of streams declared in X-Associated-Content headers.
215 // The key is a string representing the path of the URI being pushed.
216 std::map<std::string, FlipDelegate*> pending_streams_;
[email protected]aea80602009-09-18 00:55:08217
218 // As we gather data to be sent, we put it into the output queue.
[email protected]eebc0b42009-11-04 00:17:13219 typedef std::priority_queue<FlipIOBuffer> OutputQueue;
[email protected]aea80602009-09-18 00:55:08220 OutputQueue queue_;
221
222 // TODO(mbelshe): this is ugly!!
223 // The packet we are currently sending.
[email protected]eebc0b42009-11-04 00:17:13224 FlipIOBuffer in_flight_write_;
[email protected]aea80602009-09-18 00:55:08225 bool delayed_write_pending_;
226 bool write_pending_;
227
228 // Flip Frame state.
229 flip::FlipFramer flip_framer_;
230
[email protected]affe8fe2009-10-14 20:06:05231 static bool use_ssl_;
[email protected]aea80602009-09-18 00:55:08232};
233
234} // namespace net
235
236#endif // NET_FLIP_FLIP_SESSION_H_