summaryrefslogtreecommitdiffstats
path: root/net/tools/flip_server/acceptor_thread.h
blob: f967fac94fa343ea711ddbcaf25fe96ca6129e02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_TOOLS_FLIP_SERVER_ACCEPTOR_THREAD_H_
#define NET_TOOLS_FLIP_SERVER_ACCEPTOR_THREAD_H_

#include <list>
#include <string>
#include <vector>

#include "base/compiler_specific.h"
#include "base/threading/simple_thread.h"
#include "net/tools/flip_server/epoll_server.h"
#include "net/tools/flip_server/sm_interface.h"
#include "openssl/ssl.h"

struct sockaddr_in;

namespace net {

class FlipAcceptor;
class MemoryCache;
class SMConnection;
struct SSLState;

// TODO(mbelshe):  Get rid of this class; we don't need a lock just to set
//    a bool cross threads - especially one which only is set once...
class Notification {
 public:
   explicit Notification(bool value) : value_(value) {}

   void Notify() {
     base::AutoLock al(lock_);
     value_ = true;
   }
   bool HasBeenNotified() {
     base::AutoLock al(lock_);
     return value_;
   }
   bool value_;
   base::Lock lock_;
};

class SMAcceptorThread : public base::SimpleThread,
                         public EpollCallbackInterface,
                         public SMConnectionPoolInterface {
 public:
  SMAcceptorThread(FlipAcceptor *acceptor, MemoryCache* memory_cache);
  virtual ~SMAcceptorThread();

  // EpollCallbackInteface interface
  virtual void OnRegistration(EpollServer* eps,
                              int fd,
                              int event_mask) OVERRIDE {}
  virtual void OnModification(int fd, int event_mask) OVERRIDE {}
  virtual void OnEvent(int fd, EpollEvent* event) OVERRIDE;
  virtual void OnUnregistration(int fd, bool replaced) OVERRIDE {}
  virtual void OnShutdown(EpollServer* eps, int fd) OVERRIDE {}

  // SMConnectionPool interface
  virtual void SMConnectionDone(SMConnection* sc) OVERRIDE;

  // TODO(mbelshe): figure out if we can move these to private functions.
  SMConnection* NewConnection();
  SMConnection* FindOrMakeNewSMConnection();
  void InitWorker();
  void HandleConnection(int server_fd, struct sockaddr_in *remote_addr);
  void AcceptFromListenFD();

  // Notify the Accept thread that it is time to terminate.
  void Quit() { quitting_.Notify(); }

  // Iterates through a list of active connections expiring any that have been
  // idle longer than the configured timeout.
  void HandleConnectionIdleTimeout();

  virtual void Run() OVERRIDE;

 private:
  EpollServer epoll_server_;
  FlipAcceptor* acceptor_;
  SSLState* ssl_state_;
  bool use_ssl_;
  int idle_socket_timeout_s_;

  std::vector<SMConnection*> unused_server_connections_;
  std::vector<SMConnection*> tmp_unused_server_connections_;
  std::vector<SMConnection*> allocated_server_connections_;
  std::list<SMConnection*> active_server_connections_;
  Notification quitting_;
  MemoryCache* memory_cache_;
};

}  // namespace net

#endif  // NET_TOOLS_FLIP_SERVER_ACCEPTOR_THREAD_H_