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
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/create_listener.h"
18 #include "net/tools/flip_server/mem_cache.h"
19 #include "net/tools/flip_server/ring_buffer.h"
20 #include "net/tools/flip_server/sm_interface.h"
21 #include "openssl/ssl.h"
30 // A frame of data to be sent.
35 bool delete_when_done
;
37 DataFrame() : data(NULL
), size(0), delete_when_done(false), index(0) {}
41 typedef std::list
<DataFrame
*> OutputList
;
43 class SMConnection
: public SMConnectionInterface
,
44 public EpollCallbackInterface
,
45 public NotifierInterface
{
47 virtual ~SMConnection();
49 static SMConnection
* NewSMConnection(EpollServer
* epoll_server
,
51 MemoryCache
* memory_cache
,
52 FlipAcceptor
* acceptor
,
53 std::string log_prefix
);
55 // TODO(mbelshe): Make these private.
56 time_t last_read_time_
;
57 std::string server_ip_
;
58 std::string server_port_
;
60 virtual EpollServer
* epoll_server() override
;
61 OutputList
* output_list() { return &output_list_
; }
62 MemoryCache
* memory_cache() { return memory_cache_
; }
63 virtual void ReadyToSend() override
;
64 void EnqueueDataFrame(DataFrame
* df
);
66 int fd() const { return fd_
; }
67 bool initialized() const { return initialized_
; }
68 std::string
client_ip() const { return client_ip_
; }
70 virtual void InitSMConnection(SMConnectionPoolInterface
* connection_pool
,
71 SMInterface
* sm_interface
,
72 EpollServer
* epoll_server
,
74 std::string server_ip
,
75 std::string server_port
,
76 std::string remote_ip
,
82 int Send(const char* data
, int len
, int flags
);
84 // EpollCallbackInterface interface.
85 virtual void OnRegistration(EpollServer
* eps
,
87 int event_mask
) override
;
88 virtual void OnModification(int fd
, int event_mask
) override
{}
89 virtual void OnEvent(int fd
, EpollEvent
* event
) override
;
90 virtual void OnUnregistration(int fd
, bool replaced
) override
;
91 virtual void OnShutdown(EpollServer
* eps
, int fd
) override
;
93 // NotifierInterface interface.
94 virtual void Notify() override
{}
96 void Cleanup(const char* cleanup
);
98 // Flag indicating if we should force spdy on all connections.
99 static bool force_spdy() { return force_spdy_
; }
100 static void set_force_spdy(bool value
) { force_spdy_
= value
; }
103 // Decide if SPDY was negotiated.
104 bool WasSpdyNegotiated(SpdyMajorVersion
* version_negotiated
);
106 // Initialize the protocol interfaces we'll need for this connection.
107 // Returns true if successful, false otherwise.
108 bool SetupProtocolInterfaces();
112 bool DoConsumeReadData();
116 void HandleResponseFullyRead();
119 friend std::ostream
& operator<<(std::ostream
& os
, const SMConnection
& c
) {
124 SMConnection(EpollServer
* epoll_server
,
126 MemoryCache
* memory_cache
,
127 FlipAcceptor
* acceptor
,
128 std::string log_prefix
);
134 bool registered_in_epoll_server_
;
136 bool protocol_detected_
;
137 bool connection_complete_
;
139 SMConnectionPoolInterface
* connection_pool_
;
141 EpollServer
* epoll_server_
;
142 SSLState
* ssl_state_
;
143 MemoryCache
* memory_cache_
;
144 FlipAcceptor
* acceptor_
;
145 std::string client_ip_
;
147 RingBuffer read_buffer_
;
149 OutputList output_list_
;
150 SpdySM
* sm_spdy_interface_
;
151 SMInterface
* sm_http_interface_
;
152 SMInterface
* sm_streamer_interface_
;
153 SMInterface
* sm_interface_
;
154 std::string log_prefix_
;
156 size_t max_bytes_sent_per_dowrite_
;
160 static bool force_spdy_
;
165 #endif // NET_TOOLS_FLIP_SERVER_SM_CONNECTION_H_