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