Imported Upstream version 1.0.0
[platform/upstream/nghttp2.git] / src / app_helper.h
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 // Returns difference between |a| and |b| in milliseconds, assuming
64 // |a| is more recent than |b|.
65 template <typename TimePoint>
66 std::chrono::milliseconds time_delta(const TimePoint &a, const TimePoint &b) {
67   return std::chrono::duration_cast<std::chrono::milliseconds>(a - b);
68 }
69
70 // Resets timer
71 void reset_timer();
72
73 // Returns the duration since timer reset.
74 std::chrono::milliseconds get_timer();
75
76 // Returns current time point.
77 std::chrono::steady_clock::time_point get_time();
78
79 void print_timer();
80
81 // Setting true will print characters with ANSI color escape codes
82 // when printing HTTP2 frames. This function changes a static
83 // variable.
84 void set_color_output(bool f);
85
86 // Set output file when printing HTTP2 frames. By default, stdout is
87 // used.
88 void set_output(FILE *file);
89
90 ssize_t deflate_data(uint8_t *out, size_t outlen, const uint8_t *in,
91                      size_t inlen);
92
93 } // namespace nghttp2
94
95 #endif // APP_HELPER_H