2012-06-05 18:26:04 +02:00
|
|
|
/*
|
2014-03-30 12:09:21 +02:00
|
|
|
* nghttp2 - HTTP/2 C Library
|
2012-06-05 18:26:04 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "shrpx_worker.h"
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/socket.h>
|
2013-09-24 16:17:53 +02:00
|
|
|
|
2013-09-23 17:14:55 +02:00
|
|
|
#include <memory>
|
2012-06-05 18:26:04 +02:00
|
|
|
|
|
|
|
#include <event.h>
|
|
|
|
#include <event2/bufferevent.h>
|
|
|
|
|
|
|
|
#include "shrpx_ssl.h"
|
|
|
|
#include "shrpx_thread_event_receiver.h"
|
|
|
|
#include "shrpx_log.h"
|
2013-11-04 09:53:57 +01:00
|
|
|
#include "shrpx_http2_session.h"
|
2014-07-05 11:22:40 +02:00
|
|
|
#include "shrpx_worker_config.h"
|
2014-08-19 16:36:04 +02:00
|
|
|
#include "shrpx_connect_blocker.h"
|
2013-09-23 17:14:55 +02:00
|
|
|
#include "util.h"
|
2014-09-21 14:50:12 +02:00
|
|
|
#include "libevent_util.h"
|
2013-09-23 17:14:55 +02:00
|
|
|
|
|
|
|
using namespace nghttp2;
|
2012-06-05 18:26:04 +02:00
|
|
|
|
|
|
|
namespace shrpx {
|
|
|
|
|
2014-08-12 15:22:02 +02:00
|
|
|
Worker::Worker(const WorkerInfo *info)
|
|
|
|
: sv_ssl_ctx_(info->sv_ssl_ctx),
|
|
|
|
cl_ssl_ctx_(info->cl_ssl_ctx),
|
|
|
|
fd_(info->sv[1])
|
2012-06-05 18:26:04 +02:00
|
|
|
{}
|
2012-10-01 14:51:24 +02:00
|
|
|
|
2012-06-05 18:26:04 +02:00
|
|
|
Worker::~Worker()
|
|
|
|
{
|
|
|
|
shutdown(fd_, SHUT_WR);
|
|
|
|
close(fd_);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void readcb(bufferevent *bev, void *arg)
|
|
|
|
{
|
2014-01-19 11:46:58 +01:00
|
|
|
auto receiver = static_cast<ThreadEventReceiver*>(arg);
|
2012-06-05 18:26:04 +02:00
|
|
|
receiver->on_read(bev);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void eventcb(bufferevent *bev, short events, void *arg)
|
|
|
|
{
|
|
|
|
if(events & BEV_EVENT_EOF) {
|
|
|
|
LOG(ERROR) << "Connection to main thread lost: eof";
|
|
|
|
}
|
|
|
|
if(events & BEV_EVENT_ERROR) {
|
|
|
|
LOG(ERROR) << "Connection to main thread lost: network error";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void Worker::run()
|
|
|
|
{
|
2014-07-05 11:22:40 +02:00
|
|
|
(void)reopen_log_files();
|
|
|
|
|
2013-09-24 16:17:53 +02:00
|
|
|
auto evbase = std::unique_ptr<event_base, decltype(&event_base_free)>
|
|
|
|
(event_base_new(), event_base_free);
|
|
|
|
if(!evbase) {
|
|
|
|
LOG(ERROR) << "event_base_new() failed";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto bev = std::unique_ptr<bufferevent, decltype(&bufferevent_free)>
|
|
|
|
(bufferevent_socket_new(evbase.get(), fd_, BEV_OPT_DEFER_CALLBACKS),
|
|
|
|
bufferevent_free);
|
|
|
|
if(!bev) {
|
|
|
|
LOG(ERROR) << "bufferevent_socket_new() failed";
|
|
|
|
return;
|
|
|
|
}
|
2013-11-04 09:53:57 +01:00
|
|
|
std::unique_ptr<Http2Session> http2session;
|
2014-08-19 16:36:04 +02:00
|
|
|
std::unique_ptr<ConnectBlocker> http1_connect_blocker;
|
2013-11-04 10:15:56 +01:00
|
|
|
if(get_config()->downstream_proto == PROTO_HTTP2) {
|
2013-11-04 09:53:57 +01:00
|
|
|
http2session = util::make_unique<Http2Session>(evbase.get(), cl_ssl_ctx_);
|
|
|
|
if(http2session->init_notification() == -1) {
|
2012-11-20 17:29:39 +01:00
|
|
|
DIE();
|
|
|
|
}
|
2014-08-19 16:36:04 +02:00
|
|
|
} else {
|
|
|
|
http1_connect_blocker = util::make_unique<ConnectBlocker>();
|
|
|
|
if(http1_connect_blocker->init(evbase.get()) == -1) {
|
|
|
|
DIE();
|
|
|
|
}
|
2012-11-20 17:29:39 +01:00
|
|
|
}
|
2014-08-19 16:36:04 +02:00
|
|
|
|
|
|
|
auto receiver = util::make_unique<ThreadEventReceiver>
|
|
|
|
(evbase.get(),
|
|
|
|
sv_ssl_ctx_,
|
|
|
|
http2session.get(),
|
|
|
|
http1_connect_blocker.get());
|
|
|
|
|
2014-09-18 16:56:01 +02:00
|
|
|
util::bev_enable_unless(bev.get(), EV_READ);
|
2013-09-24 16:17:53 +02:00
|
|
|
bufferevent_setcb(bev.get(), readcb, nullptr, eventcb, receiver.get());
|
2012-06-05 18:26:04 +02:00
|
|
|
|
2013-09-24 16:17:53 +02:00
|
|
|
event_base_loop(evbase.get(), 0);
|
2012-06-05 18:26:04 +02:00
|
|
|
}
|
|
|
|
|
2014-08-12 15:22:02 +02:00
|
|
|
void start_threaded_worker(WorkerInfo *info)
|
2012-06-05 18:26:04 +02:00
|
|
|
{
|
2012-07-14 16:24:03 +02:00
|
|
|
Worker worker(info);
|
2012-06-05 18:26:04 +02:00
|
|
|
worker.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace shrpx
|