Home | History | Annotate | Download | only in flip_server
      1 // Copyright (c) 2009 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_ACCEPTOR_THREAD_H_
      6 #define NET_TOOLS_FLIP_SERVER_ACCEPTOR_THREAD_H_
      7 
      8 #include <list>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/threading/simple_thread.h"
     13 #include "net/tools/flip_server/epoll_server.h"
     14 #include "net/tools/flip_server/sm_interface.h"
     15 #include "openssl/ssl.h"
     16 
     17 struct sockaddr_in;
     18 
     19 namespace net {
     20 
     21 class FlipAcceptor;
     22 class MemoryCache;
     23 class SMConnection;
     24 struct SSLState;
     25 
     26 // TODO(mbelshe):  Get rid of this class; we don't need a lock just to set
     27 //    a bool cross threads - especially one which only is set once...
     28 class Notification {
     29  public:
     30    explicit Notification(bool value) : value_(value) {}
     31 
     32    void Notify() {
     33      base::AutoLock al(lock_);
     34      value_ = true;
     35    }
     36    bool HasBeenNotified() {
     37      base::AutoLock al(lock_);
     38      return value_;
     39    }
     40    bool value_;
     41    base::Lock lock_;
     42 };
     43 
     44 class SMAcceptorThread : public base::SimpleThread,
     45                          public EpollCallbackInterface,
     46                          public SMConnectionPoolInterface {
     47  public:
     48   SMAcceptorThread(FlipAcceptor *acceptor, MemoryCache* memory_cache);
     49   ~SMAcceptorThread();
     50 
     51   // EpollCallbackInteface interface
     52   virtual void OnRegistration(EpollServer* eps, int fd, int event_mask) {}
     53   virtual void OnModification(int fd, int event_mask) {}
     54   virtual void OnEvent(int fd, EpollEvent* event);
     55   virtual void OnUnregistration(int fd, bool replaced) {}
     56   virtual void OnShutdown(EpollServer* eps, int fd) {}
     57 
     58   // SMConnectionPool interface
     59   virtual void SMConnectionDone(SMConnection* sc);
     60 
     61   // TODO(mbelshe): figure out if we can move these to private functions.
     62   SMConnection* NewConnection();
     63   SMConnection* FindOrMakeNewSMConnection();
     64   void InitWorker();
     65   void HandleConnection(int server_fd, struct sockaddr_in *remote_addr);
     66   void AcceptFromListenFD();
     67 
     68   // Notify the Accept thread that it is time to terminate.
     69   void Quit() { quitting_.Notify(); }
     70 
     71   // Iterates through a list of active connections expiring any that have been
     72   // idle longer than the configured timeout.
     73   void HandleConnectionIdleTimeout();
     74 
     75   virtual void Run();
     76 
     77  private:
     78   EpollServer epoll_server_;
     79   FlipAcceptor* acceptor_;
     80   SSLState* ssl_state_;
     81   bool use_ssl_;
     82   int idle_socket_timeout_s_;
     83 
     84   std::vector<SMConnection*> unused_server_connections_;
     85   std::vector<SMConnection*> tmp_unused_server_connections_;
     86   std::vector<SMConnection*> allocated_server_connections_;
     87   std::list<SMConnection*> active_server_connections_;
     88   Notification quitting_;
     89   MemoryCache* memory_cache_;
     90 };
     91 
     92 }  // namespace net
     93 
     94 #endif  // NET_TOOLS_FLIP_SERVER_ACCEPTOR_THREAD_H_
     95 
     96