• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef NET_TOOLS_FLIP_SERVER_SM_CONNECTION_H_
6 #define NET_TOOLS_FLIP_SERVER_SM_CONNECTION_H_
7 
8 #include <arpa/inet.h>  // in_addr_t
9 #include <time.h>
10 
11 #include <list>
12 #include <string>
13 
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"
22 
23 namespace net {
24 
25 class FlipAcceptor;
26 class MemoryCache;
27 struct SSLState;
28 
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;
DataFrame()36   DataFrame() : data(NULL), size(0), delete_when_done(false), index(0) {}
37   virtual ~DataFrame();
38 };
39 
40 typedef std::list<DataFrame*> OutputList;
41 
42 class SMConnection : public SMConnectionInterface,
43                      public EpollCallbackInterface,
44                      public NotifierInterface {
45  public:
46   virtual ~SMConnection();
47 
48   static SMConnection* NewSMConnection(EpollServer* epoll_server,
49                                        SSLState* ssl_state,
50                                        MemoryCache* memory_cache,
51                                        FlipAcceptor* acceptor,
52                                        std::string log_prefix);
53 
54   // TODO(mbelshe): Make these private.
55   time_t last_read_time_;
56   std::string server_ip_;
57   std::string server_port_;
58 
59   virtual EpollServer* epoll_server() OVERRIDE;
output_list()60   OutputList* output_list() { return &output_list_; }
memory_cache()61   MemoryCache* memory_cache() { return memory_cache_; }
62   virtual void ReadyToSend() OVERRIDE;
63   void EnqueueDataFrame(DataFrame* df);
64 
fd()65   int fd() const { return fd_; }
initialized()66   bool initialized() const { return initialized_; }
client_ip()67   std::string client_ip() const { return client_ip_; }
68 
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);
77 
78   void CorkSocket();
79   void UncorkSocket();
80 
81   int Send(const char* data, int len, int flags);
82 
83   // EpollCallbackInterface interface.
84   virtual void OnRegistration(EpollServer* eps,
85                               int fd,
86                               int event_mask) OVERRIDE;
OnModification(int fd,int event_mask)87   virtual void OnModification(int fd, int event_mask) OVERRIDE {}
88   virtual void OnEvent(int fd, EpollEvent* event) OVERRIDE;
89   virtual void OnUnregistration(int fd, bool replaced) OVERRIDE;
90   virtual void OnShutdown(EpollServer* eps, int fd) OVERRIDE;
91 
92   // NotifierInterface interface.
Notify()93   virtual void Notify() OVERRIDE {}
94 
95   void Cleanup(const char* cleanup);
96 
97   // Flag indicating if we should force spdy on all connections.
force_spdy()98   static bool force_spdy() { return force_spdy_; }
set_force_spdy(bool value)99   static void set_force_spdy(bool value) { force_spdy_ = value; }
100 
101  private:
102   // Decide if SPDY was negotiated.
103   bool WasSpdyNegotiated(SpdyMajorVersion* version_negotiated);
104 
105   // Initialize the protocol interfaces we'll need for this connection.
106   // Returns true if successful, false otherwise.
107   bool SetupProtocolInterfaces();
108 
109   bool DoRead();
110   bool DoWrite();
111   bool DoConsumeReadData();
112   void Reset();
113 
114   void HandleEvents();
115   void HandleResponseFullyRead();
116 
117  protected:
118   friend std::ostream& operator<<(std::ostream& os, const SMConnection& c) {
119     os << &c << "\n";
120     return os;
121   }
122 
123   SMConnection(EpollServer* epoll_server,
124                SSLState* ssl_state,
125                MemoryCache* memory_cache,
126                FlipAcceptor* acceptor,
127                std::string log_prefix);
128 
129  private:
130   int fd_;
131   int events_;
132 
133   bool registered_in_epoll_server_;
134   bool initialized_;
135   bool protocol_detected_;
136   bool connection_complete_;
137 
138   SMConnectionPoolInterface* connection_pool_;
139 
140   EpollServer* epoll_server_;
141   SSLState* ssl_state_;
142   MemoryCache* memory_cache_;
143   FlipAcceptor* acceptor_;
144   std::string client_ip_;
145 
146   RingBuffer read_buffer_;
147 
148   OutputList output_list_;
149   SMInterface* sm_spdy_interface_;
150   SMInterface* sm_http_interface_;
151   SMInterface* sm_streamer_interface_;
152   SMInterface* sm_interface_;
153   std::string log_prefix_;
154 
155   size_t max_bytes_sent_per_dowrite_;
156 
157   SSL* ssl_;
158 
159   static bool force_spdy_;
160 };
161 
162 }  // namespace net
163 
164 #endif  // NET_TOOLS_FLIP_SERVER_SM_CONNECTION_H_
165