We started redesigning GpuMemoryBuffer interface to handle multiple buffers [0].
[chromium-blink-merge.git] / net / tools / flip_server / spdy_interface.h
blob20bc0c9ce430d96b66955121590229b48dbc1456
1 // Copyright (c) 2012 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.
5 #ifndef NET_TOOLS_FLIP_SERVER_SPDY_INTERFACE_H_
6 #define NET_TOOLS_FLIP_SERVER_SPDY_INTERFACE_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/compiler_specific.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "net/spdy/buffered_spdy_framer.h"
15 #include "net/spdy/spdy_protocol.h"
16 #include "net/tools/balsa/balsa_headers.h"
17 #include "net/tools/balsa/balsa_visitor_interface.h"
18 #include "net/tools/flip_server/output_ordering.h"
19 #include "net/tools/flip_server/sm_connection.h"
20 #include "net/tools/flip_server/sm_interface.h"
22 namespace net {
24 class FlipAcceptor;
25 class MemoryCache;
27 class SpdySM : public BufferedSpdyFramerVisitorInterface, public SMInterface {
28 public:
29 SpdySM(SMConnection* connection,
30 SMInterface* sm_http_interface,
31 EpollServer* epoll_server,
32 MemoryCache* memory_cache,
33 FlipAcceptor* acceptor,
34 SpdyMajorVersion spdy_version);
35 ~SpdySM() override;
37 void InitSMInterface(SMInterface* sm_http_interface,
38 int32 server_idx) override {}
40 void InitSMConnection(SMConnectionPoolInterface* connection_pool,
41 SMInterface* sm_interface,
42 EpollServer* epoll_server,
43 int fd,
44 std::string server_ip,
45 std::string server_port,
46 std::string remote_ip,
47 bool use_ssl) override;
49 // Create new SPDY framer after reusing SpdySM and negotiating new version
50 void CreateFramer(SpdyMajorVersion spdy_version);
52 private:
53 void set_is_request() override {}
54 SMInterface* NewConnectionInterface();
55 // virtual for tests
56 virtual SMInterface* FindOrMakeNewSMConnectionInterface(
57 const std::string& server_ip,
58 const std::string& server_port);
59 int SpdyHandleNewStream(SpdyStreamId stream_id,
60 SpdyPriority priority,
61 const SpdyHeaderBlock& headers,
62 std::string& http_data,
63 bool* is_https_scheme);
65 // BufferedSpdyFramerVisitorInterface:
66 void OnError(SpdyFramer::SpdyError error_code) override {}
67 void OnStreamError(SpdyStreamId stream_id,
68 const std::string& description) override {}
69 // Called after all the header data for SYN_STREAM control frame is received.
70 void OnSynStream(SpdyStreamId stream_id,
71 SpdyStreamId associated_stream_id,
72 SpdyPriority priority,
73 bool fin,
74 bool unidirectional,
75 const SpdyHeaderBlock& headers) override;
77 // Called after all the header data for SYN_REPLY control frame is received.
78 void OnSynReply(SpdyStreamId stream_id,
79 bool fin,
80 const SpdyHeaderBlock& headers) override;
82 // Called after all the header data for HEADERS control frame is received.
83 void OnHeaders(SpdyStreamId stream_id,
84 bool has_priority,
85 SpdyPriority priority,
86 bool fin,
87 const SpdyHeaderBlock& headers) override;
89 // Called when data frame header is received.
90 void OnDataFrameHeader(SpdyStreamId stream_id,
91 size_t length,
92 bool fin) override {}
94 // Called when data is received.
95 // |stream_id| The stream receiving data.
96 // |data| A buffer containing the data received.
97 // |len| The length of the data buffer.
98 // When the other side has finished sending data on this stream,
99 // this method will be called with a zero-length buffer.
100 void OnStreamFrameData(SpdyStreamId stream_id,
101 const char* data,
102 size_t len,
103 bool fin) override;
105 // Called when padding is received (padding length field or padding octets).
106 // |stream_id| The stream receiving data.
107 // |len| The number of padding octets.
108 void OnStreamPadding(SpdyStreamId stream_id, size_t len) override;
110 // Called when a SETTINGS frame is received.
111 // |clear_persisted| True if the respective flag is set on the SETTINGS frame.
112 void OnSettings(bool clear_persisted) override {}
114 // Called when an individual setting within a SETTINGS frame has been parsed
115 // and validated.
116 void OnSetting(SpdySettingsIds id, uint8 flags, uint32 value) override {}
118 // Called when a PING frame has been parsed.
119 void OnPing(SpdyPingId unique_id, bool is_ack) override {}
121 // Called when a RST_STREAM frame has been parsed.
122 void OnRstStream(SpdyStreamId stream_id, SpdyRstStreamStatus status) override;
124 // Called when a GOAWAY frame has been parsed.
125 void OnGoAway(SpdyStreamId last_accepted_stream_id,
126 SpdyGoAwayStatus status) override {}
128 // Called when a WINDOW_UPDATE frame has been parsed.
129 void OnWindowUpdate(SpdyStreamId stream_id,
130 uint32 delta_window_size) override {}
132 // Called when a PUSH_PROMISE frame has been parsed.
133 void OnPushPromise(SpdyStreamId stream_id,
134 SpdyStreamId promised_stream_id,
135 const SpdyHeaderBlock& headers) override {}
137 bool OnUnknownFrame(SpdyStreamId stream_id, int frame_type) override;
139 public:
140 size_t ProcessReadInput(const char* data, size_t len) override;
141 size_t ProcessWriteInput(const char* data, size_t len) override;
142 bool MessageFullyRead() const override;
143 void SetStreamID(uint32 stream_id) override {}
144 bool Error() const override;
145 const char* ErrorAsString() const override;
146 void Reset() override {}
147 void ResetForNewInterface(int32 server_idx) override;
148 void ResetForNewConnection() override;
149 // SMInterface's Cleanup is currently only called by SMConnection after a
150 // protocol message as been fully read. Spdy's SMInterface does not need
151 // to do any cleanup at this time.
152 // TODO(klindsay) This method is probably not being used properly and
153 // some logic review and method renaming is probably in order.
154 void Cleanup() override {}
155 // Send a settings frame
156 int PostAcceptHook() override;
157 void NewStream(uint32 stream_id,
158 uint32 priority,
159 const std::string& filename) override;
160 void AddToOutputOrder(const MemCacheIter& mci);
161 void SendEOF(uint32 stream_id) override;
162 void SendErrorNotFound(uint32 stream_id) override;
163 size_t SendSynStream(uint32 stream_id, const BalsaHeaders& headers) override;
164 size_t SendSynReply(uint32 stream_id, const BalsaHeaders& headers) override;
165 void SendDataFrame(uint32 stream_id,
166 const char* data,
167 int64 len,
168 uint32 flags,
169 bool compress) override;
170 BufferedSpdyFramer* spdy_framer() { return buffered_spdy_framer_.get(); }
172 const OutputOrdering& output_ordering() const {
173 return client_output_ordering_;
176 static std::string forward_ip_header() { return forward_ip_header_; }
177 static void set_forward_ip_header(const std::string& value) {
178 forward_ip_header_ = value;
180 SpdyMajorVersion spdy_version() const {
181 DCHECK(buffered_spdy_framer_);
182 return buffered_spdy_framer_->protocol_version();
185 private:
186 void SendEOFImpl(uint32 stream_id);
187 void SendErrorNotFoundImpl(uint32 stream_id);
188 void KillStream(uint32 stream_id);
189 void CopyHeaders(SpdyHeaderBlock& dest, const BalsaHeaders& headers);
190 size_t SendSynStreamImpl(uint32 stream_id, const BalsaHeaders& headers);
191 size_t SendSynReplyImpl(uint32 stream_id, const BalsaHeaders& headers);
192 void SendDataFrameImpl(uint32 stream_id,
193 const char* data,
194 int64 len,
195 SpdyDataFlags flags,
196 bool compress);
197 void EnqueueDataFrame(DataFrame* df);
198 void GetOutput() override;
200 private:
201 scoped_ptr<BufferedSpdyFramer> buffered_spdy_framer_;
202 bool valid_spdy_session_; // True if we have seen valid data on this session.
203 // Use this to fail fast when junk is sent to our
204 // port.
206 SMConnection* connection_;
207 OutputList* client_output_list_;
208 OutputOrdering client_output_ordering_;
209 uint32 next_outgoing_stream_id_;
210 EpollServer* epoll_server_;
211 FlipAcceptor* acceptor_;
212 MemoryCache* memory_cache_;
213 std::vector<SMInterface*> server_interface_list;
214 std::vector<int32> unused_server_interface_list;
215 typedef std::map<uint32, SMInterface*> StreamToSmif;
216 StreamToSmif stream_to_smif_;
217 bool close_on_error_;
219 static std::string forward_ip_header_;
222 } // namespace net
224 #endif // NET_TOOLS_FLIP_SERVER_SPDY_INTERFACE_H_