Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / net / tools / flip_server / sm_connection.h
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 class SpdySM;
29
30 // A frame of data to be sent.
31 class DataFrame {
32  public:
33   const char* data;
34   size_t size;
35   bool delete_when_done;
36   size_t index;
37   DataFrame() : data(NULL), size(0), delete_when_done(false), index(0) {}
38   virtual ~DataFrame();
39 };
40
41 typedef std::list<DataFrame*> OutputList;
42
43 class SMConnection : public SMConnectionInterface,
44                      public EpollCallbackInterface,
45                      public NotifierInterface {
46  public:
47   ~SMConnection() override;
48
49   static SMConnection* NewSMConnection(EpollServer* epoll_server,
50                                        SSLState* ssl_state,
51                                        MemoryCache* memory_cache,
52                                        FlipAcceptor* acceptor,
53                                        std::string log_prefix);
54
55   // TODO(mbelshe): Make these private.
56   time_t last_read_time_;
57   std::string server_ip_;
58   std::string server_port_;
59
60   EpollServer* epoll_server() override;
61   OutputList* output_list() { return &output_list_; }
62   MemoryCache* memory_cache() { return memory_cache_; }
63   void ReadyToSend() override;
64   void EnqueueDataFrame(DataFrame* df);
65
66   int fd() const { return fd_; }
67   bool initialized() const { return initialized_; }
68   std::string client_ip() const { return client_ip_; }
69
70   virtual void InitSMConnection(SMConnectionPoolInterface* connection_pool,
71                                 SMInterface* sm_interface,
72                                 EpollServer* epoll_server,
73                                 int fd,
74                                 std::string server_ip,
75                                 std::string server_port,
76                                 std::string remote_ip,
77                                 bool use_ssl);
78
79   void CorkSocket();
80   void UncorkSocket();
81
82   int Send(const char* data, int len, int flags);
83
84   // EpollCallbackInterface interface.
85   void OnRegistration(EpollServer* eps, int fd, int event_mask) override;
86   void OnModification(int fd, int event_mask) override {}
87   void OnEvent(int fd, EpollEvent* event) override;
88   void OnUnregistration(int fd, bool replaced) override;
89   void OnShutdown(EpollServer* eps, int fd) override;
90
91   // NotifierInterface interface.
92   void Notify() override {}
93
94   void Cleanup(const char* cleanup);
95
96   // Flag indicating if we should force spdy on all connections.
97   static bool force_spdy() { return force_spdy_; }
98   static void set_force_spdy(bool value) { force_spdy_ = value; }
99
100  private:
101   // Decide if SPDY was negotiated.
102   bool WasSpdyNegotiated(SpdyMajorVersion* version_negotiated);
103
104   // Initialize the protocol interfaces we'll need for this connection.
105   // Returns true if successful, false otherwise.
106   bool SetupProtocolInterfaces();
107
108   bool DoRead();
109   bool DoWrite();
110   bool DoConsumeReadData();
111   void Reset();
112
113   void HandleEvents();
114   void HandleResponseFullyRead();
115
116  protected:
117   friend std::ostream& operator<<(std::ostream& os, const SMConnection& c) {
118     os << &c << "\n";
119     return os;
120   }
121
122   SMConnection(EpollServer* epoll_server,
123                SSLState* ssl_state,
124                MemoryCache* memory_cache,
125                FlipAcceptor* acceptor,
126                std::string log_prefix);
127
128  private:
129   int fd_;
130   int events_;
131
132   bool registered_in_epoll_server_;
133   bool initialized_;
134   bool protocol_detected_;
135   bool connection_complete_;
136
137   SMConnectionPoolInterface* connection_pool_;
138
139   EpollServer* epoll_server_;
140   SSLState* ssl_state_;
141   MemoryCache* memory_cache_;
142   FlipAcceptor* acceptor_;
143   std::string client_ip_;
144
145   RingBuffer read_buffer_;
146
147   OutputList output_list_;
148   SpdySM* sm_spdy_interface_;
149   SMInterface* sm_http_interface_;
150   SMInterface* sm_streamer_interface_;
151   SMInterface* sm_interface_;
152   std::string log_prefix_;
153
154   size_t max_bytes_sent_per_dowrite_;
155
156   SSL* ssl_;
157
158   static bool force_spdy_;
159 };
160
161 }  // namespace net
162
163 #endif  // NET_TOOLS_FLIP_SERVER_SM_CONNECTION_H_