Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / tools / flip_server / sm_connection.h
blobaf94d6d1ed0d4e8b286562725958922d393b26ca
1 // Copyright (c) 2011 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_SM_CONNECTION_H_
6 #define NET_TOOLS_FLIP_SERVER_SM_CONNECTION_H_
8 #include <arpa/inet.h> // in_addr_t
9 #include <time.h>
11 #include <list>
12 #include <string>
14 #include "base/compiler_specific.h"
15 #include "net/spdy/spdy_protocol.h"
16 #include "net/tools/epoll_server/epoll_server.h"
17 #include "net/tools/flip_server/mem_cache.h"
18 #include "net/tools/flip_server/ring_buffer.h"
19 #include "net/tools/flip_server/sm_interface.h"
20 #include "openssl/ssl.h"
22 namespace net {
24 class FlipAcceptor;
25 class MemoryCache;
26 struct SSLState;
27 class SpdySM;
29 // A frame of data to be sent.
30 class DataFrame {
31 public:
32 const char* data;
33 size_t size;
34 bool delete_when_done;
35 size_t index;
36 DataFrame() : data(NULL), size(0), delete_when_done(false), index(0) {}
37 virtual ~DataFrame();
40 typedef std::list<DataFrame*> OutputList;
42 class SMConnection : public SMConnectionInterface,
43 public EpollCallbackInterface,
44 public NotifierInterface {
45 public:
46 ~SMConnection() override;
48 static SMConnection* NewSMConnection(EpollServer* epoll_server,
49 SSLState* ssl_state,
50 MemoryCache* memory_cache,
51 FlipAcceptor* acceptor,
52 std::string log_prefix);
54 // TODO(mbelshe): Make these private.
55 time_t last_read_time_;
56 std::string server_ip_;
57 std::string server_port_;
59 EpollServer* epoll_server() override;
60 OutputList* output_list() { return &output_list_; }
61 MemoryCache* memory_cache() { return memory_cache_; }
62 void ReadyToSend() override;
63 void EnqueueDataFrame(DataFrame* df);
65 int fd() const { return fd_; }
66 bool initialized() const { return initialized_; }
67 std::string client_ip() const { return client_ip_; }
69 virtual void InitSMConnection(SMConnectionPoolInterface* connection_pool,
70 SMInterface* sm_interface,
71 EpollServer* epoll_server,
72 int fd,
73 std::string server_ip,
74 std::string server_port,
75 std::string remote_ip,
76 bool use_ssl);
78 void CorkSocket();
79 void UncorkSocket();
81 int Send(const char* data, int len, int flags);
83 // EpollCallbackInterface interface.
84 void OnRegistration(EpollServer* eps, int fd, int event_mask) override;
85 void OnModification(int fd, int event_mask) override {}
86 void OnEvent(int fd, EpollEvent* event) override;
87 void OnUnregistration(int fd, bool replaced) override;
88 void OnShutdown(EpollServer* eps, int fd) override;
90 // NotifierInterface interface.
91 void Notify() override {}
93 void Cleanup(const char* cleanup);
95 // Flag indicating if we should force spdy on all connections.
96 static bool force_spdy() { return force_spdy_; }
97 static void set_force_spdy(bool value) { force_spdy_ = value; }
99 private:
100 // Decide if SPDY was negotiated.
101 bool WasSpdyNegotiated(SpdyMajorVersion* version_negotiated);
103 // Initialize the protocol interfaces we'll need for this connection.
104 // Returns true if successful, false otherwise.
105 bool SetupProtocolInterfaces();
107 bool DoRead();
108 bool DoWrite();
109 bool DoConsumeReadData();
110 void Reset();
112 void HandleEvents();
113 void HandleResponseFullyRead();
115 protected:
116 friend std::ostream& operator<<(std::ostream& os, const SMConnection& c) {
117 os << &c << "\n";
118 return os;
121 SMConnection(EpollServer* epoll_server,
122 SSLState* ssl_state,
123 MemoryCache* memory_cache,
124 FlipAcceptor* acceptor,
125 std::string log_prefix);
127 private:
128 int fd_;
129 int events_;
131 bool registered_in_epoll_server_;
132 bool initialized_;
133 bool protocol_detected_;
134 bool connection_complete_;
136 SMConnectionPoolInterface* connection_pool_;
138 EpollServer* epoll_server_;
139 SSLState* ssl_state_;
140 MemoryCache* memory_cache_;
141 FlipAcceptor* acceptor_;
142 std::string client_ip_;
144 RingBuffer read_buffer_;
146 OutputList output_list_;
147 SpdySM* sm_spdy_interface_;
148 SMInterface* sm_http_interface_;
149 SMInterface* sm_streamer_interface_;
150 SMInterface* sm_interface_;
151 std::string log_prefix_;
153 size_t max_bytes_sent_per_dowrite_;
155 SSL* ssl_;
157 static bool force_spdy_;
160 } // namespace net
162 #endif // NET_TOOLS_FLIP_SERVER_SM_CONNECTION_H_