src: Remove ringbuf.h, use buffer.h instead

This commit is contained in:
Tatsuhiro Tsujikawa 2015-02-06 23:40:34 +09:00
parent b4b2ddad3b
commit 795a22a320
13 changed files with 20 additions and 390 deletions

View File

@ -434,11 +434,9 @@ int Http2Handler::write_clear() {
auto loop = sessions_->get_loop(); auto loop = sessions_->get_loop();
for (;;) { for (;;) {
if (wb_.rleft() > 0) { if (wb_.rleft() > 0) {
struct iovec iov[2];
auto iovcnt = wb_.riovec(iov);
ssize_t nwrite; ssize_t nwrite;
while ((nwrite = writev(fd_, iov, iovcnt)) == -1 && errno == EINTR) while ((nwrite = write(fd_, wb_.pos, wb_.rleft())) == -1 &&
errno == EINTR)
; ;
if (nwrite == -1) { if (nwrite == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) { if (errno == EAGAIN || errno == EWOULDBLOCK) {
@ -562,11 +560,7 @@ int Http2Handler::write_tls() {
for (;;) { for (;;) {
if (wb_.rleft() > 0) { if (wb_.rleft() > 0) {
const void *p; auto rv = SSL_write(ssl_, wb_.pos, wb_.rleft());
size_t len;
std::tie(p, len) = wb_.get();
auto rv = SSL_write(ssl_, p, len);
if (rv == 0) { if (rv == 0) {
return -1; return -1;

View File

@ -44,7 +44,7 @@
#include <nghttp2/nghttp2.h> #include <nghttp2/nghttp2.h>
#include "http2.h" #include "http2.h"
#include "ringbuf.h" #include "buffer.h"
namespace nghttp2 { namespace nghttp2 {
@ -143,7 +143,7 @@ private:
ev_io rev_; ev_io rev_;
ev_timer settings_timerev_; ev_timer settings_timerev_;
std::map<int32_t, std::unique_ptr<Stream>> id2stream_; std::map<int32_t, std::unique_ptr<Stream>> id2stream_;
RingBuf<65536> wb_; Buffer<65536> wb_;
std::function<int(Http2Handler &)> read_, write_; std::function<int(Http2Handler &)> read_, write_;
int64_t session_id_; int64_t session_id_;
nghttp2_session *session_; nghttp2_session *session_;

View File

@ -76,8 +76,7 @@ nghttp_SOURCES = ${HELPER_OBJECTS} ${HELPER_HFILES} nghttp.cc nghttp.h \
nghttpd_SOURCES = ${HELPER_OBJECTS} ${HELPER_HFILES} nghttpd.cc \ nghttpd_SOURCES = ${HELPER_OBJECTS} ${HELPER_HFILES} nghttpd.cc \
ssl.cc ssl.h \ ssl.cc ssl.h \
HttpServer.cc HttpServer.h \ HttpServer.cc HttpServer.h
ringbuf.h
bin_PROGRAMS += h2load bin_PROGRAMS += h2load
@ -142,7 +141,6 @@ nghttpx_unittest_SOURCES = shrpx-unittest.cc \
util_test.cc util_test.h \ util_test.cc util_test.h \
nghttp2_gzip_test.c nghttp2_gzip_test.h \ nghttp2_gzip_test.c nghttp2_gzip_test.h \
nghttp2_gzip.c nghttp2_gzip.h \ nghttp2_gzip.c nghttp2_gzip.h \
ringbuf_test.cc ringbuf_test.h \
buffer_test.cc buffer_test.h \ buffer_test.cc buffer_test.h \
memchunk_test.cc memchunk_test.h memchunk_test.cc memchunk_test.h
nghttpx_unittest_CPPFLAGS = ${AM_CPPFLAGS}\ nghttpx_unittest_CPPFLAGS = ${AM_CPPFLAGS}\

View File

@ -45,7 +45,7 @@ void test_buffer_write(void) {
CU_ASSERT(3 == b.rleft()); CU_ASSERT(3 == b.rleft());
CU_ASSERT(13 == b.wleft()); CU_ASSERT(13 == b.wleft());
CU_ASSERT(0 == b.pos - std::begin(b.buf)); CU_ASSERT(b.pos == std::begin(b.buf));
b.drain(3); b.drain(3);
@ -65,14 +65,14 @@ void test_buffer_write(void) {
b.reset(); b.reset();
CU_ASSERT(0 == b.rleft()); CU_ASSERT(0 == b.rleft());
CU_ASSERT(0 == b.wleft()); CU_ASSERT(16 == b.wleft());
CU_ASSERT(0 == b.pos - std::begin(b.buf)); CU_ASSERT(b.pos == std::begin(b.buf));
b.write(5); b.write(5);
CU_ASSERT(5 == b.rleft()); CU_ASSERT(5 == b.rleft());
CU_ASSERT(11 == b.wleft()); CU_ASSERT(11 == b.wleft());
CU_ASSERT(0 == b.pos - std::begin(b.buf)); CU_ASSERT(b.pos == std::begin(b.buf));
} }
} // namespace nghttp2 } // namespace nghttp2

View File

@ -516,11 +516,8 @@ int Client::read_clear() {
int Client::write_clear() { int Client::write_clear() {
for (;;) { for (;;) {
if (wb.rleft() > 0) { if (wb.rleft() > 0) {
struct iovec iov[2];
auto iovcnt = wb.riovec(iov);
ssize_t nwrite; ssize_t nwrite;
while ((nwrite = writev(fd, iov, iovcnt)) == -1 && errno == EINTR) while ((nwrite = write(fd, wb.pos, wb.rleft())) == -1 && errno == EINTR)
; ;
if (nwrite == -1) { if (nwrite == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) { if (errno == EAGAIN || errno == EWOULDBLOCK) {
@ -642,11 +639,7 @@ int Client::write_tls() {
for (;;) { for (;;) {
if (wb.rleft() > 0) { if (wb.rleft() > 0) {
const void *p; auto rv = SSL_write(ssl, wb.pos, wb.rleft());
size_t len;
std::tie(p, len) = wb.get();
auto rv = SSL_write(ssl, p, len);
if (rv == 0) { if (rv == 0) {
return -1; return -1;

View File

@ -45,7 +45,7 @@
#include <openssl/ssl.h> #include <openssl/ssl.h>
#include "http2.h" #include "http2.h"
#include "ringbuf.h" #include "buffer.h"
using namespace nghttp2; using namespace nghttp2;
@ -174,7 +174,7 @@ struct Client {
// The number of requests this client has done so far. // The number of requests this client has done so far.
size_t req_done; size_t req_done;
int fd; int fd;
RingBuf<65536> wb; Buffer<65536> wb;
enum { ERR_CONNECT_FAIL = -100 }; enum { ERR_CONNECT_FAIL = -100 };

View File

@ -652,11 +652,8 @@ int HttpClient::write_clear() {
for (;;) { for (;;) {
if (wb.rleft() > 0) { if (wb.rleft() > 0) {
struct iovec iov[2];
auto iovcnt = wb.riovec(iov);
ssize_t nwrite; ssize_t nwrite;
while ((nwrite = writev(fd, iov, iovcnt)) == -1 && errno == EINTR) while ((nwrite = write(fd, wb.pos, wb.rleft())) == -1 && errno == EINTR)
; ;
if (nwrite == -1) { if (nwrite == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) { if (errno == EAGAIN || errno == EWOULDBLOCK) {
@ -1147,11 +1144,7 @@ int HttpClient::write_tls() {
for (;;) { for (;;) {
if (wb.rleft() > 0) { if (wb.rleft() > 0) {
const void *p; auto rv = SSL_write(ssl, wb.pos, wb.rleft());
size_t len;
std::tie(p, len) = wb.get();
auto rv = SSL_write(ssl, p, len);
if (rv == 0) { if (rv == 0) {
return -1; return -1;

View File

@ -45,7 +45,7 @@
#include "http-parser/http_parser.h" #include "http-parser/http_parser.h"
#include "ringbuf.h" #include "buffer.h"
#include "http2.h" #include "http2.h"
#include "nghttp2_gzip.h" #include "nghttp2_gzip.h"
@ -261,7 +261,7 @@ struct HttpClient {
// true if the response message of HTTP Upgrade request is fully // true if the response message of HTTP Upgrade request is fully
// received. It is not relevant the upgrade succeeds, or not. // received. It is not relevant the upgrade succeeds, or not.
bool upgrade_response_complete; bool upgrade_response_complete;
RingBuf<65536> wb; Buffer<65536> wb;
// SETTINGS payload sent as token68 in HTTP Upgrade // SETTINGS payload sent as token68 in HTTP Upgrade
std::array<uint8_t, 128> settings_payload; std::array<uint8_t, 128> settings_payload;

View File

@ -1,128 +0,0 @@
/*
* 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 RINGBUF_H
#define RINGBUF_H
#include "nghttp2_config.h"
#include <sys/uio.h>
#include <cstring>
#include <algorithm>
namespace nghttp2 {
template <size_t N> struct RingBuf {
RingBuf() : pos(0), len(0) {}
// Returns the number of bytes to read.
size_t rleft() const { return len; }
// Returns the number of bytes this buffer can store.
size_t wleft() const { return N - len; }
// Writes up to min(wleft(), |count|) bytes from buffer pointed by
// |buf|. Returns number of bytes written.
size_t write(const void *buf, size_t count) {
count = std::min(count, wleft());
auto last = (pos + len) % N;
if (count > N - last) {
auto c = N - last;
memcpy(begin + last, buf, c);
memcpy(begin, reinterpret_cast<const uint8_t *>(buf) + c, count - c);
} else {
memcpy(begin + last, buf, count);
}
len += count;
return count;
}
size_t write(size_t count) {
count = std::min(count, wleft());
len += count;
return count;
}
// Drains min(rleft(), |count|) bytes from start of the buffer.
size_t drain(size_t count) {
count = std::min(count, rleft());
pos = (pos + count) % N;
len -= count;
return count;
}
// Returns pointer to the next contiguous readable buffer and its
// length.
std::pair<const void *, size_t> get() const {
if (pos + len > N) {
return {begin + pos, N - pos};
}
return {begin + pos, len};
}
void reset() { pos = len = 0; }
// Fills |iov| for reading. |iov| must contain at least 2 elements.
// Returns the number of filled elements.
int riovec(struct iovec *iov) {
if (len == 0) {
return 0;
}
if (pos + len > N) {
auto c = N - pos;
iov[0].iov_base = begin + pos;
iov[0].iov_len = c;
iov[1].iov_base = begin;
iov[1].iov_len = len - c;
return 2;
}
iov[0].iov_base = begin + pos;
iov[0].iov_len = len;
return 1;
}
// Fills |iov| for writing. |iov| must contain at least 2 elements.
// Returns the number of filled elements.
int wiovec(struct iovec *iov) {
if (len == N) {
return 0;
}
if (pos == 0) {
iov[0].iov_base = begin + pos + len;
iov[0].iov_len = N - pos - len;
return 1;
}
if (pos + len < N) {
auto c = N - pos - len;
iov[0].iov_base = begin + pos + len;
iov[0].iov_len = c;
iov[1].iov_base = begin;
iov[1].iov_len = N - len - c;
return 2;
}
auto last = (pos + len) % N;
iov[0].iov_base = begin + last;
iov[0].iov_len = N - len;
return 1;
}
size_t pos;
size_t len;
uint8_t begin[N];
};
} // namespace nghttp2
#endif // RINGBUF_H

View File

@ -1,183 +0,0 @@
/*
* 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 "ringbuf_test.h"
#include <cstring>
#include <iostream>
#include <tuple>
#include <CUnit/CUnit.h>
#include <nghttp2/nghttp2.h>
#include "ringbuf.h"
namespace nghttp2 {
void test_ringbuf_write(void) {
RingBuf<16> b;
CU_ASSERT(0 == b.rleft());
CU_ASSERT(16 == b.wleft());
b.write("012", 3);
CU_ASSERT(3 == b.rleft());
CU_ASSERT(13 == b.wleft());
CU_ASSERT(0 == b.pos);
CU_ASSERT(3 == b.len);
b.drain(3);
CU_ASSERT(0 == b.rleft());
CU_ASSERT(16 == b.wleft());
CU_ASSERT(3 == b.pos);
CU_ASSERT(0 == b.len);
b.write("0123456789ABCDEF", 16);
CU_ASSERT(16 == b.rleft());
CU_ASSERT(0 == b.wleft());
CU_ASSERT(3 == b.pos);
CU_ASSERT(16 == b.len);
CU_ASSERT(0 == memcmp(b.begin, "DEF0123456789ABC", 16));
const void *p;
size_t len;
std::tie(p, len) = b.get();
CU_ASSERT(13 == len);
CU_ASSERT(0 == memcmp(p, "0123456789ABC", len));
b.drain(14);
CU_ASSERT(2 == b.rleft());
CU_ASSERT(14 == b.wleft());
CU_ASSERT(1 == b.pos);
CU_ASSERT(2 == b.len);
std::tie(p, len) = b.get();
CU_ASSERT(2 == len);
CU_ASSERT(0 == memcmp(p, "EF", len));
}
void test_ringbuf_iovec(void) {
RingBuf<16> b;
struct iovec iov[2];
auto rv = b.riovec(iov);
CU_ASSERT(0 == rv);
rv = b.wiovec(iov);
CU_ASSERT(1 == rv);
CU_ASSERT(b.begin == iov[0].iov_base);
CU_ASSERT(16 == iov[0].iov_len);
// set pos to somewhere middle of the buffer, this will require 2
// iovec for writing.
b.pos = 6;
rv = b.riovec(iov);
CU_ASSERT(0 == rv);
rv = b.wiovec(iov);
CU_ASSERT(2 == rv);
CU_ASSERT(b.begin + b.pos == iov[0].iov_base);
CU_ASSERT(10 == iov[0].iov_len);
CU_ASSERT(b.begin == iov[1].iov_base);
CU_ASSERT(6 == iov[1].iov_len);
// occupy first region of buffer
b.pos = 0;
b.len = 10;
rv = b.riovec(iov);
CU_ASSERT(1 == rv);
CU_ASSERT(b.begin == iov[0].iov_base);
CU_ASSERT(10 == iov[0].iov_len);
rv = b.wiovec(iov);
CU_ASSERT(1 == rv);
CU_ASSERT(b.begin + b.len == iov[0].iov_base);
CU_ASSERT(6 == iov[0].iov_len);
// occupy last region of buffer
b.pos = 6;
b.len = 10;
rv = b.riovec(iov);
CU_ASSERT(1 == rv);
CU_ASSERT(b.begin + b.pos == iov[0].iov_base);
CU_ASSERT(10 == iov[0].iov_len);
rv = b.wiovec(iov);
CU_ASSERT(1 == rv);
CU_ASSERT(b.begin == iov[0].iov_base);
CU_ASSERT(6 == iov[0].iov_len);
// occupy middle of buffer
b.pos = 3;
b.len = 10;
rv = b.riovec(iov);
CU_ASSERT(1 == rv);
CU_ASSERT(b.begin + b.pos == iov[0].iov_base);
CU_ASSERT(10 == iov[0].iov_len);
rv = b.wiovec(iov);
CU_ASSERT(2 == rv);
CU_ASSERT(b.begin + b.pos + b.len == iov[0].iov_base);
CU_ASSERT(3 == iov[0].iov_len);
CU_ASSERT(b.begin == iov[1].iov_base);
CU_ASSERT(3 == iov[1].iov_len);
// crossover
b.pos = 13;
b.len = 10;
rv = b.riovec(iov);
CU_ASSERT(2 == rv);
CU_ASSERT(b.begin + b.pos == iov[0].iov_base);
CU_ASSERT(3 == iov[0].iov_len);
CU_ASSERT(b.begin == iov[1].iov_base);
CU_ASSERT(7 == iov[1].iov_len);
rv = b.wiovec(iov);
CU_ASSERT(1 == rv);
CU_ASSERT(b.begin + 7 == iov[0].iov_base);
CU_ASSERT(6 == iov[0].iov_len);
}
} // namespace nghttp2

View File

@ -1,35 +0,0 @@
/*
* 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 RINGBUF_TEST_H
#define RINGBUF_TEST_H
namespace nghttp2 {
void test_ringbuf_write(void);
void test_ringbuf_iovec(void);
} // namespace nghttp2
#endif // RINGBUF_TEST_H

View File

@ -38,7 +38,6 @@
#include "http2_test.h" #include "http2_test.h"
#include "util_test.h" #include "util_test.h"
#include "nghttp2_gzip_test.h" #include "nghttp2_gzip_test.h"
#include "ringbuf_test.h"
#include "buffer_test.h" #include "buffer_test.h"
#include "memchunk_test.h" #include "memchunk_test.h"
#include "shrpx_config.h" #include "shrpx_config.h"
@ -144,9 +143,7 @@ int main(int argc, char *argv[]) {
!CU_add_test(pSuite, "util_format_duration", !CU_add_test(pSuite, "util_format_duration",
shrpx::test_util_format_duration) || shrpx::test_util_format_duration) ||
!CU_add_test(pSuite, "gzip_inflate", test_nghttp2_gzip_inflate) || !CU_add_test(pSuite, "gzip_inflate", test_nghttp2_gzip_inflate) ||
!CU_add_test(pSuite, "ringbuf_write", nghttp2::test_ringbuf_write) || !CU_add_test(pSuite, "buffer_write", nghttp2::test_buffer_write) ||
!CU_add_test(pSuite, "ringbuf_iovec", nghttp2::test_ringbuf_iovec) ||
!CU_add_test(pSuite, "buffer_write", nghttp2::test_ringbuf_write) ||
!CU_add_test(pSuite, "pool_recycle", nghttp2::test_pool_recycle) || !CU_add_test(pSuite, "pool_recycle", nghttp2::test_pool_recycle) ||
!CU_add_test(pSuite, "memchunk_append", nghttp2::test_memchunks_append) || !CU_add_test(pSuite, "memchunk_append", nghttp2::test_memchunks_append) ||
!CU_add_test(pSuite, "memchunk_drain", nghttp2::test_memchunks_drain) || !CU_add_test(pSuite, "memchunk_drain", nghttp2::test_memchunks_drain) ||

View File

@ -29,6 +29,7 @@
#include <memory> #include <memory>
#include <array> #include <array>
#include <functional>
namespace nghttp2 { namespace nghttp2 {