• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2014 Tatsuhiro Tsujikawa
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 #ifndef LIBEVENT_UTIL_H
26 #define LIBEVENT_UTIL_H
27 
28 #include "nghttp2_config.h"
29 
30 #include <event2/buffer.h>
31 #include <event2/bufferevent.h>
32 
33 namespace nghttp2 {
34 
35 namespace util {
36 
37 class EvbufferBuffer {
38 public:
39   EvbufferBuffer();
40   // If |limit| is not -1, at most min(limit, bufmax) size bytes are
41   // added to evbuffer_.
42   EvbufferBuffer(evbuffer *evbuffer, uint8_t *buf, size_t bufmax,
43                  ssize_t limit = -1);
44   ~EvbufferBuffer();
45   void reset(evbuffer *evbuffer, uint8_t *buf, size_t bufmax,
46              ssize_t limit = -1);
47   int flush();
48   int add(const uint8_t *data, size_t datalen);
49   size_t get_buflen() const;
50   int write_buffer();
51   // Returns the number of written bytes to evbuffer_ so far.  reset()
52   // resets this value to 0.
53   size_t get_writelen() const;
54 
55 private:
56   evbuffer *evbuffer_;
57   evbuffer *bucket_;
58   uint8_t *buf_;
59   size_t bufmax_;
60   size_t buflen_;
61   ssize_t limit_;
62   size_t writelen_;
63 };
64 
65 // These functions are provided to reduce epoll_ctl syscall.  Avoid
66 // calling bufferevent_enable/disable() unless it is required by
67 // sniffing current enabled events.
68 void bev_enable_unless(bufferevent *bev, int events);
69 void bev_disable_unless(bufferevent *bev, int events);
70 
71 } // namespace util
72 
73 } // namespace nghttp2
74 
75 #endif // LIBEVENT_UTIL_H
76