12c593315Sopenharmony_ci/* 22c593315Sopenharmony_ci * nghttp2 - HTTP/2 C Library 32c593315Sopenharmony_ci * 42c593315Sopenharmony_ci * Copyright (c) 2014 Tatsuhiro Tsujikawa 52c593315Sopenharmony_ci * 62c593315Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining 72c593315Sopenharmony_ci * a copy of this software and associated documentation files (the 82c593315Sopenharmony_ci * "Software"), to deal in the Software without restriction, including 92c593315Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish, 102c593315Sopenharmony_ci * distribute, sublicense, and/or sell copies of the Software, and to 112c593315Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to 122c593315Sopenharmony_ci * the following conditions: 132c593315Sopenharmony_ci * 142c593315Sopenharmony_ci * The above copyright notice and this permission notice shall be 152c593315Sopenharmony_ci * included in all copies or substantial portions of the Software. 162c593315Sopenharmony_ci * 172c593315Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 182c593315Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 192c593315Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 202c593315Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 212c593315Sopenharmony_ci * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 222c593315Sopenharmony_ci * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 232c593315Sopenharmony_ci * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 242c593315Sopenharmony_ci */ 252c593315Sopenharmony_ci#include "libevent_util.h" 262c593315Sopenharmony_ci 272c593315Sopenharmony_ci#include <cstring> 282c593315Sopenharmony_ci#include <algorithm> 292c593315Sopenharmony_ci 302c593315Sopenharmony_cinamespace nghttp2 { 312c593315Sopenharmony_ci 322c593315Sopenharmony_cinamespace util { 332c593315Sopenharmony_ci 342c593315Sopenharmony_ciEvbufferBuffer::EvbufferBuffer() 352c593315Sopenharmony_ci : evbuffer_(nullptr), 362c593315Sopenharmony_ci bucket_(nullptr), 372c593315Sopenharmony_ci buf_(nullptr), 382c593315Sopenharmony_ci bufmax_(0), 392c593315Sopenharmony_ci buflen_(0), 402c593315Sopenharmony_ci limit_(0), 412c593315Sopenharmony_ci writelen_(0) {} 422c593315Sopenharmony_ci 432c593315Sopenharmony_ciEvbufferBuffer::EvbufferBuffer(evbuffer *evbuffer, uint8_t *buf, size_t bufmax, 442c593315Sopenharmony_ci ssize_t limit) 452c593315Sopenharmony_ci : evbuffer_(evbuffer), 462c593315Sopenharmony_ci bucket_(limit == -1 ? nullptr : evbuffer_new()), 472c593315Sopenharmony_ci buf_(buf), 482c593315Sopenharmony_ci bufmax_(bufmax), 492c593315Sopenharmony_ci buflen_(0), 502c593315Sopenharmony_ci limit_(limit), 512c593315Sopenharmony_ci writelen_(0) {} 522c593315Sopenharmony_ci 532c593315Sopenharmony_civoid EvbufferBuffer::reset(evbuffer *evbuffer, uint8_t *buf, size_t bufmax, 542c593315Sopenharmony_ci ssize_t limit) { 552c593315Sopenharmony_ci evbuffer_ = evbuffer; 562c593315Sopenharmony_ci buf_ = buf; 572c593315Sopenharmony_ci if (limit != -1 && !bucket_) { 582c593315Sopenharmony_ci bucket_ = evbuffer_new(); 592c593315Sopenharmony_ci } 602c593315Sopenharmony_ci bufmax_ = bufmax; 612c593315Sopenharmony_ci buflen_ = 0; 622c593315Sopenharmony_ci limit_ = limit; 632c593315Sopenharmony_ci writelen_ = 0; 642c593315Sopenharmony_ci} 652c593315Sopenharmony_ci 662c593315Sopenharmony_ciEvbufferBuffer::~EvbufferBuffer() { 672c593315Sopenharmony_ci if (bucket_) { 682c593315Sopenharmony_ci evbuffer_free(bucket_); 692c593315Sopenharmony_ci } 702c593315Sopenharmony_ci} 712c593315Sopenharmony_ci 722c593315Sopenharmony_ciint EvbufferBuffer::write_buffer() { 732c593315Sopenharmony_ci for (auto pos = buf_, end = buf_ + buflen_; pos < end;) { 742c593315Sopenharmony_ci // To avoid merging chunks in evbuffer, we first add to temporal 752c593315Sopenharmony_ci // buffer bucket_ and then move its chain to evbuffer_. 762c593315Sopenharmony_ci auto nwrite = std::min(end - pos, limit_); 772c593315Sopenharmony_ci auto rv = evbuffer_add(bucket_, pos, nwrite); 782c593315Sopenharmony_ci if (rv == -1) { 792c593315Sopenharmony_ci return -1; 802c593315Sopenharmony_ci } 812c593315Sopenharmony_ci rv = evbuffer_add_buffer(evbuffer_, bucket_); 822c593315Sopenharmony_ci if (rv == -1) { 832c593315Sopenharmony_ci return -1; 842c593315Sopenharmony_ci } 852c593315Sopenharmony_ci pos += nwrite; 862c593315Sopenharmony_ci } 872c593315Sopenharmony_ci return 0; 882c593315Sopenharmony_ci} 892c593315Sopenharmony_ci 902c593315Sopenharmony_ciint EvbufferBuffer::flush() { 912c593315Sopenharmony_ci int rv; 922c593315Sopenharmony_ci if (buflen_ > 0) { 932c593315Sopenharmony_ci if (limit_ == -1) { 942c593315Sopenharmony_ci rv = evbuffer_add(evbuffer_, buf_, buflen_); 952c593315Sopenharmony_ci } else { 962c593315Sopenharmony_ci rv = write_buffer(); 972c593315Sopenharmony_ci } 982c593315Sopenharmony_ci if (rv == -1) { 992c593315Sopenharmony_ci return -1; 1002c593315Sopenharmony_ci } 1012c593315Sopenharmony_ci writelen_ += buflen_; 1022c593315Sopenharmony_ci buflen_ = 0; 1032c593315Sopenharmony_ci } 1042c593315Sopenharmony_ci return 0; 1052c593315Sopenharmony_ci} 1062c593315Sopenharmony_ci 1072c593315Sopenharmony_ciint EvbufferBuffer::add(const uint8_t *data, size_t datalen) { 1082c593315Sopenharmony_ci int rv; 1092c593315Sopenharmony_ci if (buflen_ + datalen > bufmax_) { 1102c593315Sopenharmony_ci if (buflen_ > 0) { 1112c593315Sopenharmony_ci if (limit_ == -1) { 1122c593315Sopenharmony_ci rv = evbuffer_add(evbuffer_, buf_, buflen_); 1132c593315Sopenharmony_ci } else { 1142c593315Sopenharmony_ci rv = write_buffer(); 1152c593315Sopenharmony_ci } 1162c593315Sopenharmony_ci if (rv == -1) { 1172c593315Sopenharmony_ci return -1; 1182c593315Sopenharmony_ci } 1192c593315Sopenharmony_ci writelen_ += buflen_; 1202c593315Sopenharmony_ci buflen_ = 0; 1212c593315Sopenharmony_ci } 1222c593315Sopenharmony_ci if (datalen > bufmax_) { 1232c593315Sopenharmony_ci if (limit_ == -1) { 1242c593315Sopenharmony_ci rv = evbuffer_add(evbuffer_, data, datalen); 1252c593315Sopenharmony_ci } else { 1262c593315Sopenharmony_ci rv = write_buffer(); 1272c593315Sopenharmony_ci } 1282c593315Sopenharmony_ci if (rv == -1) { 1292c593315Sopenharmony_ci return -1; 1302c593315Sopenharmony_ci } 1312c593315Sopenharmony_ci writelen_ += buflen_; 1322c593315Sopenharmony_ci return 0; 1332c593315Sopenharmony_ci } 1342c593315Sopenharmony_ci } 1352c593315Sopenharmony_ci memcpy(buf_ + buflen_, data, datalen); 1362c593315Sopenharmony_ci buflen_ += datalen; 1372c593315Sopenharmony_ci return 0; 1382c593315Sopenharmony_ci} 1392c593315Sopenharmony_ci 1402c593315Sopenharmony_cisize_t EvbufferBuffer::get_buflen() const { return buflen_; } 1412c593315Sopenharmony_ci 1422c593315Sopenharmony_cisize_t EvbufferBuffer::get_writelen() const { return writelen_; } 1432c593315Sopenharmony_ci 1442c593315Sopenharmony_civoid bev_enable_unless(bufferevent *bev, int events) { 1452c593315Sopenharmony_ci if ((bufferevent_get_enabled(bev) & events) == events) { 1462c593315Sopenharmony_ci return; 1472c593315Sopenharmony_ci } 1482c593315Sopenharmony_ci 1492c593315Sopenharmony_ci bufferevent_enable(bev, events); 1502c593315Sopenharmony_ci} 1512c593315Sopenharmony_ci 1522c593315Sopenharmony_civoid bev_disable_unless(bufferevent *bev, int events) { 1532c593315Sopenharmony_ci if ((bufferevent_get_enabled(bev) & events) == 0) { 1542c593315Sopenharmony_ci return; 1552c593315Sopenharmony_ci } 1562c593315Sopenharmony_ci 1572c593315Sopenharmony_ci bufferevent_disable(bev, events); 1582c593315Sopenharmony_ci} 1592c593315Sopenharmony_ci 1602c593315Sopenharmony_ci} // namespace util 1612c593315Sopenharmony_ci 1622c593315Sopenharmony_ci} // namespace nghttp2 163