• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2012 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 APP_HELPER_H
26 #define APP_HELPER_H
27 
28 #include "nghttp2_config.h"
29 
30 #include <cinttypes>
31 #include <cstdlib>
32 #ifdef HAVE_SYS_TIME_H
33 #  include <sys/time.h>
34 #endif // HAVE_SYS_TIME_H
35 #include <poll.h>
36 
37 #include <map>
38 #include <chrono>
39 
40 #include <nghttp2/nghttp2.h>
41 
42 namespace nghttp2 {
43 
44 int verbose_on_header_callback(nghttp2_session *session,
45                                const nghttp2_frame *frame, const uint8_t *name,
46                                size_t namelen, const uint8_t *value,
47                                size_t valuelen, uint8_t flags, void *user_data);
48 
49 int verbose_on_frame_recv_callback(nghttp2_session *session,
50                                    const nghttp2_frame *frame, void *user_data);
51 
52 int verbose_on_invalid_frame_recv_callback(nghttp2_session *session,
53                                            const nghttp2_frame *frame,
54                                            int lib_error_code, void *user_data);
55 
56 int verbose_on_frame_send_callback(nghttp2_session *session,
57                                    const nghttp2_frame *frame, void *user_data);
58 
59 int verbose_on_data_chunk_recv_callback(nghttp2_session *session, uint8_t flags,
60                                         int32_t stream_id, const uint8_t *data,
61                                         size_t len, void *user_data);
62 
63 int verbose_error_callback(nghttp2_session *session, int lib_error_code,
64                            const char *msg, size_t len, void *user_data);
65 
66 // Returns difference between |a| and |b| in milliseconds, assuming
67 // |a| is more recent than |b|.
68 template <typename TimePoint>
time_delta(const TimePoint & a,const TimePoint & b)69 std::chrono::milliseconds time_delta(const TimePoint &a, const TimePoint &b) {
70   return std::chrono::duration_cast<std::chrono::milliseconds>(a - b);
71 }
72 
73 // Resets timer
74 void reset_timer();
75 
76 // Returns the duration since timer reset.
77 std::chrono::milliseconds get_timer();
78 
79 // Returns current time point.
80 std::chrono::steady_clock::time_point get_time();
81 
82 void print_timer();
83 
84 // Setting true will print characters with ANSI color escape codes
85 // when printing HTTP2 frames. This function changes a static
86 // variable.
87 void set_color_output(bool f);
88 
89 // Set output file when printing HTTP2 frames. By default, stdout is
90 // used.
91 void set_output(FILE *file);
92 
93 ssize_t deflate_data(uint8_t *out, size_t outlen, const uint8_t *in,
94                      size_t inlen);
95 
96 } // namespace nghttp2
97 
98 #endif // APP_HELPER_H
99