| 50 | } |
| 51 | |
| 52 | int Acceptor::StartAccept(int listened_fd, int idle_timeout_sec, |
| 53 | const std::shared_ptr<SocketSSLContext>& ssl_ctx, |
| 54 | bool force_ssl) { |
| 55 | if (listened_fd < 0) { |
| 56 | LOG(FATAL) << "Invalid listened_fd=" << listened_fd; |
| 57 | return -1; |
| 58 | } |
| 59 | |
| 60 | if (!ssl_ctx && force_ssl) { |
| 61 | LOG(ERROR) << "Fail to force SSL for all connections " |
| 62 | " because ssl_ctx is NULL"; |
| 63 | return -1; |
| 64 | } |
| 65 | |
| 66 | BAIDU_SCOPED_LOCK(_map_mutex); |
| 67 | if (_status == UNINITIALIZED) { |
| 68 | if (Initialize() != 0) { |
| 69 | LOG(FATAL) << "Fail to initialize Acceptor"; |
| 70 | return -1; |
| 71 | } |
| 72 | _status = READY; |
| 73 | } |
| 74 | if (_status != READY) { |
| 75 | LOG(FATAL) << "Acceptor hasn't stopped yet: status=" << status(); |
| 76 | return -1; |
| 77 | } |
| 78 | if (idle_timeout_sec > 0) { |
| 79 | bthread_attr_t tmp = BTHREAD_ATTR_NORMAL; |
| 80 | tmp.tag = _bthread_tag; |
| 81 | bthread_attr_set_name(&tmp, "CloseIdleConnections"); |
| 82 | if (bthread_start_background(&_close_idle_tid, &tmp, CloseIdleConnections, this) != 0) { |
| 83 | LOG(FATAL) << "Fail to start bthread"; |
| 84 | return -1; |
| 85 | } |
| 86 | } |
| 87 | _idle_timeout_sec = idle_timeout_sec; |
| 88 | _force_ssl = force_ssl; |
| 89 | _ssl_ctx = ssl_ctx; |
| 90 | |
| 91 | // Creation of _acception_id is inside lock so that OnNewConnections |
| 92 | // (which may run immediately) should see sane fields set below. |
| 93 | SocketOptions options; |
| 94 | options.fd = listened_fd; |
| 95 | options.user = this; |
| 96 | options.bthread_tag = _bthread_tag; |
| 97 | options.on_edge_triggered_events = OnNewConnections; |
| 98 | if (Socket::Create(options, &_acception_id) != 0) { |
| 99 | // Close-idle-socket thread will be stopped inside destructor |
| 100 | LOG(FATAL) << "Fail to create _acception_id"; |
| 101 | return -1; |
| 102 | } |
| 103 | |
| 104 | _listened_fd = listened_fd; |
| 105 | _status = RUNNING; |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | void* Acceptor::CloseIdleConnections(void* arg) { |
no test coverage detected