Commit 446f8f13 authored by Tatsuhiro Tsujikawa's avatar Tatsuhiro Tsujikawa

src: Move libevent related helper functions to libevent_util

parent 96bb9c20
...@@ -58,6 +58,7 @@ extern "C" { ...@@ -58,6 +58,7 @@ extern "C" {
#include "app_helper.h" #include "app_helper.h"
#include "http2.h" #include "http2.h"
#include "util.h" #include "util.h"
#include "libevent_util.h"
#include "ssl.h" #include "ssl.h"
#ifndef O_BINARY #ifndef O_BINARY
......
...@@ -56,8 +56,10 @@ if ENABLE_APP ...@@ -56,8 +56,10 @@ if ENABLE_APP
bin_PROGRAMS += nghttp nghttpd nghttpx bin_PROGRAMS += nghttp nghttpd nghttpx
HELPER_OBJECTS = util.cc http2.cc timegm.c app_helper.cc nghttp2_gzip.c HELPER_OBJECTS = util.cc libevent_util.cc \
HELPER_HFILES = util.h http2.h timegm.h app_helper.h nghttp2_config.h \ http2.cc timegm.c app_helper.cc nghttp2_gzip.c
HELPER_HFILES = util.h libevent_util.h \
http2.h timegm.h app_helper.h nghttp2_config.h \
nghttp2_gzip.h nghttp2_gzip.h
HTML_PARSER_OBJECTS = HTML_PARSER_OBJECTS =
...@@ -78,7 +80,8 @@ if ENABLE_H2LOAD ...@@ -78,7 +80,8 @@ if ENABLE_H2LOAD
bin_PROGRAMS += h2load bin_PROGRAMS += h2load
h2load_SOURCES = util.cc util.h http2.cc http2.h h2load.cc h2load.h \ h2load_SOURCES = util.cc util.h libevent_util.cc libevent_util.h \
http2.cc http2.h h2load.cc h2load.h \
timegm.c timegm.h \ timegm.c timegm.h \
ssl.cc ssl.h \ ssl.cc ssl.h \
h2load_session.h \ h2load_session.h \
...@@ -92,6 +95,7 @@ endif # ENABLE_H2LOAD ...@@ -92,6 +95,7 @@ endif # ENABLE_H2LOAD
NGHTTPX_SRCS = \ NGHTTPX_SRCS = \
util.cc util.h http2.cc http2.h timegm.c timegm.h base64.h \ util.cc util.h http2.cc http2.h timegm.c timegm.h base64.h \
libevent_util.cc libevent_util.h \
app_helper.cc app_helper.h \ app_helper.cc app_helper.h \
ssl.cc ssl.h \ ssl.cc ssl.h \
shrpx_config.cc shrpx_config.h \ shrpx_config.cc shrpx_config.h \
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include "h2load.h" #include "h2load.h"
#include "util.h" #include "util.h"
#include "libevent_util.h"
using namespace nghttp2; using namespace nghttp2;
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include <spdylay/spdylay.h> #include <spdylay/spdylay.h>
#include "util.h" #include "util.h"
#include "libevent_util.h"
namespace h2load { namespace h2load {
......
/*
* nghttp2 - HTTP/2 C Library
*
* Copyright (c) 2014 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 "libevent_util.h"
#include <cstring>
namespace nghttp2 {
namespace util {
EvbufferBuffer::EvbufferBuffer()
: evbuffer_(nullptr),
buf_(nullptr),
bufmax_(0),
buflen_(0)
{}
EvbufferBuffer::EvbufferBuffer(evbuffer *evbuffer, uint8_t *buf, size_t bufmax)
: evbuffer_(evbuffer),
buf_(buf),
bufmax_(bufmax),
buflen_(0)
{}
void EvbufferBuffer::reset(evbuffer *evbuffer, uint8_t *buf, size_t bufmax)
{
evbuffer_ = evbuffer;
buf_ = buf;
bufmax_ = bufmax;
buflen_ = 0;
}
int EvbufferBuffer::flush()
{
int rv;
if(buflen_ > 0) {
rv = evbuffer_add(evbuffer_, buf_, buflen_);
if(rv == -1) {
return -1;
}
buflen_ = 0;
}
return 0;
}
int EvbufferBuffer::add(const uint8_t *data, size_t datalen)
{
int rv;
if(buflen_ + datalen > bufmax_) {
if(buflen_ > 0) {
rv = evbuffer_add(evbuffer_, buf_, buflen_);
if(rv == -1) {
return -1;
}
buflen_ = 0;
}
if(datalen > bufmax_) {
rv = evbuffer_add(evbuffer_, data, datalen);
if(rv == -1) {
return -1;
}
return 0;
}
}
memcpy(buf_ + buflen_, data, datalen);
buflen_ += datalen;
return 0;
}
size_t EvbufferBuffer::get_buflen() const
{
return buflen_;
}
void bev_enable_unless(bufferevent *bev, int events)
{
if((bufferevent_get_enabled(bev) & events) == events) {
return;
}
bufferevent_enable(bev, events);
}
void bev_disable_unless(bufferevent *bev, int events)
{
if((bufferevent_get_enabled(bev) & events) == 0) {
return;
}
bufferevent_disable(bev, events);
}
} // namespace util
} // namespace nghttp2
/*
* nghttp2 - HTTP/2 C Library
*
* Copyright (c) 2014 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.
*/
#ifndef LIBEVENT_UTIL_H
#define LIBEVENT_UTIL_H
#include "nghttp2_config.h"
#include <event2/buffer.h>
#include <event2/bufferevent.h>
namespace nghttp2 {
namespace util {
class EvbufferBuffer {
public:
EvbufferBuffer();
EvbufferBuffer(evbuffer *evbuffer, uint8_t *buf, size_t bufmax);
void reset(evbuffer *evbuffer, uint8_t *buf, size_t bufmax);
int flush();
int add(const uint8_t *data, size_t datalen);
size_t get_buflen() const;
private:
evbuffer *evbuffer_;
uint8_t *buf_;
size_t bufmax_;
size_t buflen_;
};
// These functions are provided to reduce epoll_ctl syscall. Avoid
// calling bufferevent_enable/disable() unless it is required by
// sniffing current enabled events.
void bev_enable_unless(bufferevent *bev, int events);
void bev_disable_unless(bufferevent *bev, int events);
} // namespace util
} // namespace nghttp2
#endif // LIBEVENT_UTIL_H
...@@ -67,6 +67,7 @@ ...@@ -67,6 +67,7 @@
#include "app_helper.h" #include "app_helper.h"
#include "HtmlParser.h" #include "HtmlParser.h"
#include "util.h" #include "util.h"
#include "libevent_util.h"
#include "base64.h" #include "base64.h"
#include "http2.h" #include "http2.h"
#include "nghttp2_gzip.h" #include "nghttp2_gzip.h"
......
...@@ -40,6 +40,7 @@ ...@@ -40,6 +40,7 @@
#include "shrpx_spdy_upstream.h" #include "shrpx_spdy_upstream.h"
#endif // HAVE_SPDYLAY #endif // HAVE_SPDYLAY
#include "util.h" #include "util.h"
#include "libevent_util.h"
using namespace nghttp2; using namespace nghttp2;
......
...@@ -44,6 +44,7 @@ ...@@ -44,6 +44,7 @@
#include "shrpx_worker_config.h" #include "shrpx_worker_config.h"
#include "http2.h" #include "http2.h"
#include "util.h" #include "util.h"
#include "libevent_util.h"
#include "base64.h" #include "base64.h"
using namespace nghttp2; using namespace nghttp2;
......
...@@ -38,6 +38,7 @@ ...@@ -38,6 +38,7 @@
#include "shrpx_worker_config.h" #include "shrpx_worker_config.h"
#include "http2.h" #include "http2.h"
#include "util.h" #include "util.h"
#include "libevent_util.h"
#include "base64.h" #include "base64.h"
#include "app_helper.h" #include "app_helper.h"
......
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#include "shrpx_connect_blocker.h" #include "shrpx_connect_blocker.h"
#include "http2.h" #include "http2.h"
#include "util.h" #include "util.h"
#include "libevent_util.h"
using namespace nghttp2; using namespace nghttp2;
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include <algorithm> #include <algorithm>
#include "util.h" #include "util.h"
#include "libevent_util.h"
using namespace nghttp2; using namespace nghttp2;
......
...@@ -40,6 +40,7 @@ ...@@ -40,6 +40,7 @@
#include "shrpx_http2_session.h" #include "shrpx_http2_session.h"
#include "shrpx_connect_blocker.h" #include "shrpx_connect_blocker.h"
#include "util.h" #include "util.h"
#include "libevent_util.h"
using namespace nghttp2; using namespace nghttp2;
......
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#include "shrpx_upstream.h" #include "shrpx_upstream.h"
#include "shrpx_downstream_queue.h" #include "shrpx_downstream_queue.h"
#include "util.h" #include "util.h"
#include "libevent_util.h"
namespace shrpx { namespace shrpx {
......
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
#include "shrpx_worker_config.h" #include "shrpx_worker_config.h"
#include "shrpx_connect_blocker.h" #include "shrpx_connect_blocker.h"
#include "util.h" #include "util.h"
#include "libevent_util.h"
using namespace nghttp2; using namespace nghttp2;
......
...@@ -509,70 +509,6 @@ void write_uri_field(std::ostream& o, ...@@ -509,70 +509,6 @@ void write_uri_field(std::ostream& o,
} }
} }
EvbufferBuffer::EvbufferBuffer()
: evbuffer_(nullptr),
buf_(nullptr),
bufmax_(0),
buflen_(0)
{}
EvbufferBuffer::EvbufferBuffer(evbuffer *evbuffer, uint8_t *buf, size_t bufmax)
: evbuffer_(evbuffer),
buf_(buf),
bufmax_(bufmax),
buflen_(0)
{}
void EvbufferBuffer::reset(evbuffer *evbuffer, uint8_t *buf, size_t bufmax)
{
evbuffer_ = evbuffer;
buf_ = buf;
bufmax_ = bufmax;
buflen_ = 0;
}
int EvbufferBuffer::flush()
{
int rv;
if(buflen_ > 0) {
rv = evbuffer_add(evbuffer_, buf_, buflen_);
if(rv == -1) {
return -1;
}
buflen_ = 0;
}
return 0;
}
int EvbufferBuffer::add(const uint8_t *data, size_t datalen)
{
int rv;
if(buflen_ + datalen > bufmax_) {
if(buflen_ > 0) {
rv = evbuffer_add(evbuffer_, buf_, buflen_);
if(rv == -1) {
return -1;
}
buflen_ = 0;
}
if(datalen > bufmax_) {
rv = evbuffer_add(evbuffer_, data, datalen);
if(rv == -1) {
return -1;
}
return 0;
}
}
memcpy(buf_ + buflen_, data, datalen);
buflen_ += datalen;
return 0;
}
size_t EvbufferBuffer::get_buflen() const
{
return buflen_;
}
bool numeric_host(const char *hostname) bool numeric_host(const char *hostname)
{ {
struct addrinfo hints; struct addrinfo hints;
...@@ -657,24 +593,6 @@ char* get_exec_path(int argc, char **const argv, const char *cwd) ...@@ -657,24 +593,6 @@ char* get_exec_path(int argc, char **const argv, const char *cwd)
return path; return path;
} }
void bev_enable_unless(bufferevent *bev, int events)
{
if((bufferevent_get_enabled(bev) & events) == events) {
return;
}
bufferevent_enable(bev, events);
}
void bev_disable_unless(bufferevent *bev, int events)
{
if((bufferevent_get_enabled(bev) & events) == 0) {
return;
}
bufferevent_disable(bev, events);
}
} // namespace util } // namespace util
} // namespace nghttp2 } // namespace nghttp2
...@@ -37,9 +37,6 @@ ...@@ -37,9 +37,6 @@
#include <sstream> #include <sstream>
#include <memory> #include <memory>
#include <event2/buffer.h>
#include <event2/bufferevent.h>
#include "http-parser/http_parser.h" #include "http-parser/http_parser.h"
namespace nghttp2 { namespace nghttp2 {
...@@ -448,21 +445,6 @@ void write_uri_field(std::ostream& o, ...@@ -448,21 +445,6 @@ void write_uri_field(std::ostream& o,
const char *uri, const http_parser_url &u, const char *uri, const http_parser_url &u,
http_parser_url_fields field); http_parser_url_fields field);
class EvbufferBuffer {
public:
EvbufferBuffer();
EvbufferBuffer(evbuffer *evbuffer, uint8_t *buf, size_t bufmax);
void reset(evbuffer *evbuffer, uint8_t *buf, size_t bufmax);
int flush();
int add(const uint8_t *data, size_t datalen);
size_t get_buflen() const;
private:
evbuffer *evbuffer_;
uint8_t *buf_;
size_t bufmax_;
size_t buflen_;
};
bool numeric_host(const char *hostname); bool numeric_host(const char *hostname);
// Opens |path| with O_APPEND enabled. If file does not exist, it is // Opens |path| with O_APPEND enabled. If file does not exist, it is
...@@ -482,12 +464,6 @@ std::string ascii_dump(const uint8_t *data, size_t len); ...@@ -482,12 +464,6 @@ std::string ascii_dump(const uint8_t *data, size_t len);
// it. // it.
char* get_exec_path(int argc, char **const argv, const char *cwd); char* get_exec_path(int argc, char **const argv, const char *cwd);
// These functions are provided to reduce epoll_ctl syscall. Avoid
// calling bufferevent_enable/disable() unless it is required by
// sniffing current enabled events.
void bev_enable_unless(bufferevent *bev, int events);
void bev_disable_unless(bufferevent *bev, int events);
} // namespace util } // namespace util
} // namespace nghttp2 } // namespace nghttp2
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment