Home | History | Annotate | Download | only in flip_server
      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/tools/flip_server/create_listener.h"
     16 #include "net/tools/flip_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"
     21 
     22 namespace net {
     23 
     24 class FlipAcceptor;
     25 class MemoryCache;
     26 struct SSLState;
     27 
     28 // A frame of data to be sent.
     29 class DataFrame {
     30  public:
     31   const char* data;
     32   size_t size;
     33   bool delete_when_done;
     34   size_t index;
     35   DataFrame() : data(NULL), size(0), delete_when_done(false), index(0) {}
     36   virtual ~DataFrame();
     37 };
     38 
     39 typedef std::list<DataFrame*> OutputList;
     40 
     41 class SMConnection : public SMConnectionInterface,
     42                      public EpollCallbackInterface,
     43                      public NotifierInterface {
     44  public:
     45   virtual ~SMConnection();
     46 
     47   static SMConnection* NewSMConnection(EpollServer* epoll_server,
     48                                        SSLState *ssl_state,
     49                                        MemoryCache* memory_cache,
     50                                        FlipAcceptor *acceptor,
     51                                        std::string log_prefix);
     52 
     53   // TODO(mbelshe): Make these private.
     54   time_t last_read_time_;
     55   std::string server_ip_;
     56   std::string server_port_;
     57 
     58   virtual EpollServer* epoll_server() OVERRIDE;
     59   OutputList* output_list() { return &output_list_; }
     60   MemoryCache* memory_cache() { return memory_cache_; }
     61   virtual void ReadyToSend() OVERRIDE;
     62   void EnqueueDataFrame(DataFrame* df);
     63 
     64   int fd() const { return fd_; }
     65   bool initialized() const { return initialized_; }
     66   std::string client_ip() const { return client_ip_; }
     67 
     68   void InitSMConnection(SMConnectionPoolInterface* connection_pool,
     69                         SMInterface* sm_interface,
     70                         EpollServer* epoll_server,
     71                         int fd,
     72                         std::string server_ip,
     73                         std::string server_port,
     74                         std::string remote_ip,
     75                         bool use_ssl);
     76 
     77   void CorkSocket();
     78   void UncorkSocket();
     79 
     80   int Send(const char* data, int len, int flags);
     81 
     82   // EpollCallbackInterface interface.
     83   virtual void OnRegistration(EpollServer* eps,
     84                               int fd,
     85                               int event_mask) OVERRIDE;
     86   virtual void OnModification(int fd, int event_mask) OVERRIDE {}
     87   virtual void OnEvent(int fd, EpollEvent* event) OVERRIDE;
     88   virtual void OnUnregistration(int fd, bool replaced) OVERRIDE;
     89   virtual void OnShutdown(EpollServer* eps, int fd) OVERRIDE;
     90 
     91   // NotifierInterface interface.
     92   virtual 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();
    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  private:
    123   SMConnection(EpollServer* epoll_server,
    124                SSLState* ssl_state,
    125                MemoryCache* memory_cache,
    126                FlipAcceptor* acceptor,
    127                std::string log_prefix);
    128   int fd_;
    129   int events_;
    130 
    131   bool registered_in_epoll_server_;
    132   bool initialized_;
    133   bool protocol_detected_;
    134   bool connection_complete_;
    135 
    136   SMConnectionPoolInterface* connection_pool_;
    137 
    138   EpollServer *epoll_server_;
    139   SSLState *ssl_state_;
    140   MemoryCache* memory_cache_;
    141   FlipAcceptor *acceptor_;
    142   std::string client_ip_;
    143 
    144   RingBuffer read_buffer_;
    145 
    146   OutputList output_list_;
    147   SMInterface* sm_spdy_interface_;
    148   SMInterface* sm_http_interface_;
    149   SMInterface* sm_streamer_interface_;
    150   SMInterface* sm_interface_;
    151   std::string log_prefix_;
    152 
    153   size_t max_bytes_sent_per_dowrite_;
    154 
    155   SSL* ssl_;
    156 
    157   static bool force_spdy_;
    158 };
    159 
    160 }  // namespace net
    161 
    162 #endif  // NET_TOOLS_FLIP_SERVER_SM_CONNECTION_H_
    163