Imported Upstream version 1.0.0
[platform/upstream/nghttp2.git] / lib / nghttp2_outbound_item.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 NGHTTP2_OUTBOUND_ITEM_H
26 #define NGHTTP2_OUTBOUND_ITEM_H
27
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif /* HAVE_CONFIG_H */
31
32 #include <nghttp2/nghttp2.h>
33 #include "nghttp2_frame.h"
34 #include "nghttp2_mem.h"
35
36 /* struct used for HEADERS and PUSH_PROMISE frame */
37 typedef struct {
38   nghttp2_data_provider data_prd;
39   void *stream_user_data;
40   /* error code when request HEADERS is canceled by RST_STREAM while
41      it is in queue. */
42   uint32_t error_code;
43   /* nonzero if request HEADERS is canceled.  The error code is stored
44      in |error_code|. */
45   uint8_t canceled;
46   /* nonzero if this item should be attached to stream object to make
47      it under priority control */
48   uint8_t attach_stream;
49 } nghttp2_headers_aux_data;
50
51 /* struct used for DATA frame */
52 typedef struct {
53   /**
54    * The data to be sent for this DATA frame.
55    */
56   nghttp2_data_provider data_prd;
57   /**
58    * The flags of DATA frame.  We use separate flags here and
59    * nghttp2_data frame.  The latter contains flags actually sent to
60    * peer.  This |flags| may contain NGHTTP2_FLAG_END_STREAM and only
61    * when |eof| becomes nonzero, flags in nghttp2_data has
62    * NGHTTP2_FLAG_END_STREAM set.
63    */
64   uint8_t flags;
65   /**
66    * The flag to indicate whether EOF was reached or not. Initially
67    * |eof| is 0. It becomes 1 after all data were read.
68    */
69   uint8_t eof;
70   /**
71    * The flag to indicate that NGHTTP2_DATA_FLAG_NO_COPY is used.
72    */
73   uint8_t no_copy;
74 } nghttp2_data_aux_data;
75
76 typedef enum {
77   NGHTTP2_GOAWAY_AUX_NONE = 0x0,
78   /* indicates that session should be terminated after the
79      transmission of this frame. */
80   NGHTTP2_GOAWAY_AUX_TERM_ON_SEND = 0x1,
81   /* indicates that this GOAWAY is just a notification for graceful
82      shutdown.  No nghttp2_session.goaway_flags should be updated on
83      the reaction to this frame. */
84   NGHTTP2_GOAWAY_AUX_SHUTDOWN_NOTICE = 0x2
85 } nghttp2_goaway_aux_flag;
86
87 /* struct used for GOAWAY frame */
88 typedef struct {
89   /* bitwise-OR of one or more of nghttp2_goaway_aux_flag. */
90   uint8_t flags;
91 } nghttp2_goaway_aux_data;
92
93 /* Additional data which cannot be stored in nghttp2_frame struct */
94 typedef union {
95   nghttp2_data_aux_data data;
96   nghttp2_headers_aux_data headers;
97   nghttp2_goaway_aux_data goaway;
98 } nghttp2_aux_data;
99
100 struct nghttp2_outbound_item;
101 typedef struct nghttp2_outbound_item nghttp2_outbound_item;
102
103 struct nghttp2_outbound_item {
104   nghttp2_frame frame;
105   nghttp2_aux_data aux_data;
106   /* The priority used in priority comparion.  Smaller is served
107      ealier.  For PING, SETTINGS and non-DATA frames (excluding
108      response HEADERS frame) have dedicated cycle value defined above.
109      For DATA frame, cycle is computed by taking into account of
110      effective weight and frame payload length previously sent, so
111      that the amount of transmission is distributed across streams
112      proportional to effective weight (inside a tree). */
113   uint64_t cycle;
114   nghttp2_outbound_item *qnext;
115   /* nonzero if this object is queued. */
116   uint8_t queued;
117 };
118
119 /*
120  * Initializes |item|.  No memory allocation is done in this function.
121  * Don't call nghttp2_outbound_item_free() until frame member is
122  * initialized.
123  */
124 void nghttp2_outbound_item_init(nghttp2_outbound_item *item);
125
126 /*
127  * Deallocates resource for |item|. If |item| is NULL, this function
128  * does nothing.
129  */
130 void nghttp2_outbound_item_free(nghttp2_outbound_item *item, nghttp2_mem *mem);
131
132 /*
133  * queue for nghttp2_outbound_item.
134  */
135 typedef struct {
136   nghttp2_outbound_item *head, *tail;
137   /* number of items in this queue. */
138   size_t n;
139 } nghttp2_outbound_queue;
140
141 void nghttp2_outbound_queue_init(nghttp2_outbound_queue *q);
142
143 /* Pushes |item| into |q| */
144 void nghttp2_outbound_queue_push(nghttp2_outbound_queue *q,
145                                  nghttp2_outbound_item *item);
146
147 /* Pops |item| at the top from |q|.  If |q| is empty, nothing
148    happens. */
149 void nghttp2_outbound_queue_pop(nghttp2_outbound_queue *q);
150
151 /* Returns the top item. */
152 #define nghttp2_outbound_queue_top(Q) ((Q)->head)
153
154 /* Returns the size of the queue */
155 #define nghttp2_outbound_queue_size(Q) ((Q)->n)
156
157 #endif /* NGHTTP2_OUTBOUND_ITEM_H */