2012-01-24 14:02:24 +01:00
|
|
|
/*
|
2014-03-30 12:09:21 +02:00
|
|
|
* nghttp2 - HTTP/2 C Library
|
2012-01-24 14:02:24 +01:00
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
2013-07-12 17:19:03 +02:00
|
|
|
#include "nghttp2_stream.h"
|
2012-01-24 14:02:24 +01:00
|
|
|
|
2012-02-19 15:42:25 +01:00
|
|
|
#include <assert.h>
|
2014-03-25 18:04:24 +01:00
|
|
|
#include <stdio.h>
|
2012-02-19 15:42:25 +01:00
|
|
|
|
2014-11-11 16:26:23 +01:00
|
|
|
#include "nghttp2_session.h"
|
2014-03-31 15:51:33 +02:00
|
|
|
#include "nghttp2_helper.h"
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
void nghttp2_stream_init(nghttp2_stream *stream, int32_t stream_id,
|
2014-11-27 15:39:04 +01:00
|
|
|
uint8_t flags, nghttp2_stream_state initial_state,
|
|
|
|
int32_t weight, nghttp2_stream_roots *roots,
|
2013-08-08 14:12:49 +02:00
|
|
|
int32_t remote_initial_window_size,
|
|
|
|
int32_t local_initial_window_size,
|
2014-11-27 15:39:04 +01:00
|
|
|
void *stream_user_data) {
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_map_entry_init(&stream->map_entry, stream_id);
|
2012-01-24 14:02:24 +01:00
|
|
|
stream->stream_id = stream_id;
|
2012-01-25 15:46:07 +01:00
|
|
|
stream->flags = flags;
|
2012-01-25 17:04:01 +01:00
|
|
|
stream->state = initial_state;
|
2013-07-12 17:19:03 +02:00
|
|
|
stream->shut_flags = NGHTTP2_SHUT_NONE;
|
2012-02-03 17:37:21 +01:00
|
|
|
stream->stream_user_data = stream_user_data;
|
2014-12-20 11:53:57 +01:00
|
|
|
stream->item = NULL;
|
2013-08-08 14:12:49 +02:00
|
|
|
stream->remote_window_size = remote_initial_window_size;
|
|
|
|
stream->local_window_size = local_initial_window_size;
|
2012-02-25 16:12:32 +01:00
|
|
|
stream->recv_window_size = 0;
|
2014-07-25 14:26:03 +02:00
|
|
|
stream->consumed_size = 0;
|
2013-10-12 10:02:37 +02:00
|
|
|
stream->recv_reduction = 0;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
|
|
|
stream->dep_prev = NULL;
|
|
|
|
stream->dep_next = NULL;
|
|
|
|
stream->sib_prev = NULL;
|
|
|
|
stream->sib_next = NULL;
|
|
|
|
|
2014-11-07 16:36:47 +01:00
|
|
|
stream->closed_prev = NULL;
|
2014-03-30 10:41:54 +02:00
|
|
|
stream->closed_next = NULL;
|
|
|
|
|
2014-12-20 11:53:57 +01:00
|
|
|
stream->dpri = NGHTTP2_STREAM_DPRI_NO_ITEM;
|
2014-03-25 18:04:24 +01:00
|
|
|
stream->num_substreams = 1;
|
2014-04-14 16:53:54 +02:00
|
|
|
stream->weight = weight;
|
|
|
|
stream->sum_dep_weight = 0;
|
2014-04-24 16:37:40 +02:00
|
|
|
stream->sum_norest_weight = 0;
|
2014-04-17 14:15:14 +02:00
|
|
|
|
|
|
|
stream->roots = roots;
|
|
|
|
stream->root_prev = NULL;
|
|
|
|
stream->root_next = NULL;
|
2015-02-19 16:01:15 +01:00
|
|
|
|
|
|
|
stream->http_flags = NGHTTP2_HTTP_FLAG_NONE;
|
|
|
|
stream->content_length = -1;
|
|
|
|
stream->recv_content_length = 0;
|
|
|
|
stream->status_code = -1;
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void nghttp2_stream_free(nghttp2_stream *stream _U_) {
|
2014-12-20 11:53:57 +01:00
|
|
|
/* We don't free stream->item. If it is assigned to aob, then
|
2014-09-17 16:16:00 +02:00
|
|
|
active_outbound_item_reset() will delete it. If it is queued,
|
|
|
|
then it is deleted when pq is deleted in nghttp2_session_del().
|
|
|
|
Otherwise, nghttp2_session_del() will delete it. */
|
2012-02-02 13:51:52 +01:00
|
|
|
}
|
2012-01-28 16:08:51 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void nghttp2_stream_shutdown(nghttp2_stream *stream, nghttp2_shut_flag flag) {
|
2012-01-28 16:08:51 +01:00
|
|
|
stream->shut_flags |= flag;
|
|
|
|
}
|
2012-02-02 13:51:52 +01:00
|
|
|
|
2014-12-20 11:53:57 +01:00
|
|
|
static int stream_push_item(nghttp2_stream *stream, nghttp2_session *session) {
|
2015-04-07 14:25:05 +02:00
|
|
|
/* This is required for Android NDK r10d */
|
|
|
|
int rv = 0;
|
2014-11-11 16:26:23 +01:00
|
|
|
nghttp2_outbound_item *item;
|
2014-03-31 16:01:05 +02:00
|
|
|
|
2014-12-20 11:53:57 +01:00
|
|
|
assert(stream->item);
|
|
|
|
assert(stream->item->queued == 0);
|
2014-03-31 16:01:05 +02:00
|
|
|
|
2014-12-20 11:53:57 +01:00
|
|
|
item = stream->item;
|
2014-11-11 16:26:23 +01:00
|
|
|
|
|
|
|
/* If item is now sent, don't push it to the queue. Otherwise, we
|
|
|
|
may push same item twice. */
|
2014-11-27 15:39:04 +01:00
|
|
|
if (session->aob.item == item) {
|
2014-11-06 15:43:10 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
switch (item->frame.hd.type) {
|
2014-11-11 16:26:23 +01:00
|
|
|
case NGHTTP2_DATA:
|
2015-04-24 15:30:32 +02:00
|
|
|
/* Penalize item by delaying scheduling according to effective
|
|
|
|
weight. This will delay low priority stream, which is good.
|
|
|
|
OTOH, this may incur delay for high priority item. Will
|
|
|
|
see. */
|
2015-06-19 12:36:24 +02:00
|
|
|
item->cycle = session->last_cycle +
|
|
|
|
NGHTTP2_DATA_PAYLOADLEN * NGHTTP2_MAX_WEIGHT /
|
|
|
|
nghttp2_stream_compute_effective_weight(stream);
|
2015-04-24 15:30:32 +02:00
|
|
|
|
2014-11-11 16:26:23 +01:00
|
|
|
rv = nghttp2_pq_push(&session->ob_da_pq, item);
|
2015-04-24 15:30:32 +02:00
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-11-11 16:26:23 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_HEADERS:
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->state == NGHTTP2_STREAM_RESERVED) {
|
2015-04-24 15:30:32 +02:00
|
|
|
nghttp2_outbound_queue_push(&session->ob_syn, item);
|
2014-11-11 16:26:23 +01:00
|
|
|
} else {
|
2015-04-24 15:30:32 +02:00
|
|
|
nghttp2_outbound_queue_push(&session->ob_reg, item);
|
2014-11-11 16:26:23 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* should not reach here */
|
|
|
|
assert(0);
|
|
|
|
}
|
2014-03-31 16:01:05 +02:00
|
|
|
|
2014-11-11 16:26:23 +01:00
|
|
|
item->queued = 1;
|
2014-03-31 16:01:05 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static nghttp2_stream *stream_last_sib(nghttp2_stream *stream) {
|
|
|
|
for (; stream->sib_next; stream = stream->sib_next)
|
|
|
|
;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
static void stream_update_dep_length(nghttp2_stream *stream, ssize_t delta) {
|
2014-03-25 18:04:24 +01:00
|
|
|
stream->num_substreams += delta;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->dep_prev) {
|
2015-06-19 12:36:24 +02:00
|
|
|
stream_update_dep_length(stream->dep_prev, delta);
|
2014-03-25 18:04:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-14 16:53:54 +02:00
|
|
|
int32_t nghttp2_stream_dep_distributed_weight(nghttp2_stream *stream,
|
2014-11-27 15:39:04 +01:00
|
|
|
int32_t weight) {
|
2014-04-14 16:53:54 +02:00
|
|
|
weight = stream->weight * weight / stream->sum_dep_weight;
|
2014-03-31 15:51:33 +02:00
|
|
|
|
2014-04-14 16:53:54 +02:00
|
|
|
return nghttp2_max(1, weight);
|
|
|
|
}
|
2014-03-31 15:51:33 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static void stream_update_dep_set_rest(nghttp2_stream *stream) {
|
|
|
|
if (stream == NULL) {
|
2014-03-25 18:04:24 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-08 15:44:02 +02:00
|
|
|
DEBUGF(fprintf(stderr, "stream: stream=%d is rest\n", stream->stream_id));
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->dpri == NGHTTP2_STREAM_DPRI_REST) {
|
2014-03-25 18:04:24 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->dpri == NGHTTP2_STREAM_DPRI_TOP) {
|
2014-03-25 18:04:24 +01:00
|
|
|
stream->dpri = NGHTTP2_STREAM_DPRI_REST;
|
|
|
|
|
|
|
|
stream_update_dep_set_rest(stream->sib_next);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream_update_dep_set_rest(stream->sib_next);
|
|
|
|
stream_update_dep_set_rest(stream->dep_next);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Performs dfs starting |stream|, search stream which can become
|
2015-06-19 12:36:24 +02:00
|
|
|
* NGHTTP2_STREAM_DPRI_TOP and set its dpri. This function also
|
|
|
|
* updates sum_norest_weight if stream->dpri ==
|
|
|
|
* NGHTTP2_STREAM_DPRI_NO_ITEM. This function returns nonzero if
|
|
|
|
* stream's subtree contains at least one NGHTTP2_STRAEM_DPRI_TOP
|
|
|
|
* stream.
|
2014-10-08 15:44:02 +02:00
|
|
|
*/
|
2015-06-19 12:36:24 +02:00
|
|
|
static int stream_update_dep_set_top(nghttp2_stream *stream) {
|
2014-10-08 15:44:02 +02:00
|
|
|
nghttp2_stream *si;
|
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
if (!stream) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->dpri == NGHTTP2_STREAM_DPRI_TOP) {
|
2015-06-19 12:36:24 +02:00
|
|
|
return 1;
|
2014-10-08 15:44:02 +02:00
|
|
|
}
|
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
stream->sum_norest_weight = 0;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->dpri == NGHTTP2_STREAM_DPRI_REST) {
|
|
|
|
DEBUGF(
|
2014-12-20 11:53:57 +01:00
|
|
|
fprintf(stderr, "stream: stream=%d item is top\n", stream->stream_id));
|
2014-10-08 15:44:02 +02:00
|
|
|
|
|
|
|
stream->dpri = NGHTTP2_STREAM_DPRI_TOP;
|
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
return 1;
|
2014-10-08 15:44:02 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (si = stream->dep_next; si; si = si->sib_next) {
|
2015-06-19 12:36:24 +02:00
|
|
|
if (stream_update_dep_set_top(si)) {
|
|
|
|
stream->sum_norest_weight += si->weight;
|
|
|
|
}
|
2014-10-08 15:44:02 +02:00
|
|
|
}
|
2015-06-19 12:36:24 +02:00
|
|
|
|
|
|
|
return stream->sum_norest_weight > 0;
|
2014-10-08 15:44:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Performs dfs starting |stream|, and dueue stream whose dpri is
|
|
|
|
* NGHTTP2_STREAM_DPRI_TOP and has not been queued yet.
|
2014-03-25 18:04:24 +01:00
|
|
|
*
|
2014-04-14 16:53:54 +02:00
|
|
|
* This function returns 0 if it succeeds, or one of the following
|
|
|
|
* negative error codes:
|
2014-03-25 18:04:24 +01:00
|
|
|
*
|
|
|
|
* NGHTTP2_ERR_NOMEM
|
|
|
|
* Out of memory.
|
|
|
|
*/
|
2014-11-11 16:26:23 +01:00
|
|
|
static int stream_update_dep_queue_top(nghttp2_stream *stream,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session *session) {
|
2014-04-14 16:53:54 +02:00
|
|
|
int rv;
|
2014-04-24 16:37:40 +02:00
|
|
|
nghttp2_stream *si;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->dpri == NGHTTP2_STREAM_DPRI_REST) {
|
2014-04-14 16:53:54 +02:00
|
|
|
return 0;
|
2014-03-25 18:04:24 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->dpri == NGHTTP2_STREAM_DPRI_TOP) {
|
2014-12-20 11:53:57 +01:00
|
|
|
if (!stream->item->queued) {
|
2014-11-27 15:39:04 +01:00
|
|
|
DEBUGF(fprintf(stderr, "stream: stream=%d enqueue\n", stream->stream_id));
|
2014-12-20 11:53:57 +01:00
|
|
|
rv = stream_push_item(stream, session);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-03-25 18:04:24 +01:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-24 16:37:40 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
if (stream->sum_norest_weight == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (si = stream->dep_next; si; si = si->sib_next) {
|
2014-11-11 16:26:23 +01:00
|
|
|
rv = stream_update_dep_queue_top(si, session);
|
2014-03-31 15:51:33 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-03-31 15:51:33 +02:00
|
|
|
return rv;
|
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
}
|
|
|
|
|
2014-04-24 16:37:40 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-04-24 16:37:40 +02:00
|
|
|
/*
|
2015-06-19 12:36:24 +02:00
|
|
|
* Updates stream->sum_norest_weight recursively towards root.
|
|
|
|
* |delta| must not be 0. We have to gather effective sum of weight
|
|
|
|
* of descendants. |delta| is added to stream->sum_norest_weight. If
|
|
|
|
* stream->sum_norest_weight becomes 0, we have to update parent
|
|
|
|
* stream, decreasing its sum_norest_weight by stream->weight. If
|
|
|
|
* stream->sum_norest_weight becomes from 0 to positive, then we have
|
|
|
|
* to update parent stream, increasing its sum_norest_weight by
|
|
|
|
* stream->weight. Otherwise, we stop recursive call.
|
2014-04-24 16:37:40 +02:00
|
|
|
*/
|
2015-06-19 12:36:24 +02:00
|
|
|
static void stream_update_dep_sum_norest_weight(nghttp2_stream *stream,
|
|
|
|
int32_t delta) {
|
|
|
|
int32_t old;
|
2014-04-24 16:37:40 +02:00
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
if (!stream) {
|
|
|
|
return;
|
2014-04-24 16:37:40 +02:00
|
|
|
}
|
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
assert(delta != 0);
|
|
|
|
assert(stream->sum_norest_weight + delta >= 0);
|
|
|
|
|
|
|
|
old = stream->sum_norest_weight;
|
|
|
|
stream->sum_norest_weight += delta;
|
|
|
|
|
|
|
|
if (old == 0) {
|
|
|
|
assert(delta > 0);
|
|
|
|
stream_update_dep_sum_norest_weight(stream->dep_prev, stream->weight);
|
|
|
|
return;
|
2014-03-25 18:04:24 +01:00
|
|
|
}
|
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
assert(old > 0);
|
2014-04-24 16:37:40 +02:00
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
if (stream->sum_norest_weight == 0) {
|
|
|
|
stream_update_dep_sum_norest_weight(stream->dep_prev, -stream->weight);
|
2014-03-31 15:51:33 +02:00
|
|
|
}
|
2015-06-19 12:36:24 +02:00
|
|
|
}
|
2014-03-31 15:51:33 +02:00
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
/*
|
|
|
|
* Returns stream whose dpri is NGHTTP2_STREAM_DPRI_NO_ITEM along the
|
|
|
|
* path following stream->dep_prev (stream's ancestors, including
|
|
|
|
* itself). In other words, find stream which blocks the descendant
|
|
|
|
* streams. If there is no such stream, returns NULL.
|
|
|
|
*/
|
|
|
|
static nghttp2_stream *stream_get_dep_blocking(nghttp2_stream *stream) {
|
|
|
|
for (; stream; stream = stream->dep_prev) {
|
|
|
|
if (stream->dpri != NGHTTP2_STREAM_DPRI_NO_ITEM) {
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
2014-03-25 18:04:24 +01:00
|
|
|
}
|
|
|
|
|
2014-12-20 11:53:57 +01:00
|
|
|
static int stream_update_dep_on_attach_item(nghttp2_stream *stream,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session *session) {
|
2015-06-19 12:36:24 +02:00
|
|
|
nghttp2_stream *blocking_stream;
|
|
|
|
int rv;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
|
|
|
stream->dpri = NGHTTP2_STREAM_DPRI_REST;
|
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
blocking_stream = stream_get_dep_blocking(stream->dep_prev);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
/* If we found REST or TOP in ascendants, we don't have to update
|
|
|
|
any metadata. */
|
|
|
|
if (blocking_stream) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
stream->dpri = NGHTTP2_STREAM_DPRI_TOP;
|
|
|
|
if (stream->sum_norest_weight == 0) {
|
|
|
|
stream_update_dep_sum_norest_weight(stream->dep_prev, stream->weight);
|
|
|
|
} else {
|
|
|
|
stream_update_dep_set_rest(stream->dep_next);
|
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
if (!stream->item->queued) {
|
|
|
|
DEBUGF(fprintf(stderr, "stream: stream=%d enqueue\n", stream->stream_id));
|
|
|
|
rv = stream_push_item(stream, session);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
2014-04-24 16:37:40 +02:00
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
return 0;
|
2014-03-25 18:04:24 +01:00
|
|
|
}
|
|
|
|
|
2014-12-20 11:53:57 +01:00
|
|
|
static int stream_update_dep_on_detach_item(nghttp2_stream *stream,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session *session) {
|
2015-06-19 12:36:24 +02:00
|
|
|
if (stream->dpri == NGHTTP2_STREAM_DPRI_REST) {
|
|
|
|
stream->dpri = NGHTTP2_STREAM_DPRI_NO_ITEM;
|
|
|
|
return 0;
|
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
assert(stream->dpri == NGHTTP2_STREAM_DPRI_TOP);
|
2014-03-31 15:51:33 +02:00
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
stream->dpri = NGHTTP2_STREAM_DPRI_NO_ITEM;
|
2014-03-31 15:51:33 +02:00
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
if (stream_update_dep_set_top(stream) == 0) {
|
|
|
|
stream_update_dep_sum_norest_weight(stream->dep_prev, -stream->weight);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-04-24 16:37:40 +02:00
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
return stream_update_dep_queue_top(stream->dep_next, session);
|
2014-03-25 18:04:24 +01:00
|
|
|
}
|
|
|
|
|
2014-12-20 11:53:57 +01:00
|
|
|
int nghttp2_stream_attach_item(nghttp2_stream *stream,
|
|
|
|
nghttp2_outbound_item *item,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session *session) {
|
2014-04-02 15:55:49 +02:00
|
|
|
assert((stream->flags & NGHTTP2_STREAM_FLAG_DEFERRED_ALL) == 0);
|
2014-12-20 11:53:57 +01:00
|
|
|
assert(stream->item == NULL);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-12-20 11:53:57 +01:00
|
|
|
DEBUGF(fprintf(stderr, "stream: stream=%d attach item=%p\n",
|
|
|
|
stream->stream_id, item));
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-12-20 11:53:57 +01:00
|
|
|
stream->item = item;
|
2014-04-03 08:52:21 +02:00
|
|
|
|
2014-12-20 11:53:57 +01:00
|
|
|
return stream_update_dep_on_attach_item(stream, session);
|
2014-03-25 18:04:24 +01:00
|
|
|
}
|
|
|
|
|
2014-12-20 11:53:57 +01:00
|
|
|
int nghttp2_stream_detach_item(nghttp2_stream *stream,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session *session) {
|
2014-12-20 11:53:57 +01:00
|
|
|
DEBUGF(fprintf(stderr, "stream: stream=%d detach item=%p\n",
|
|
|
|
stream->stream_id, stream->item));
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-12-20 11:53:57 +01:00
|
|
|
stream->item = NULL;
|
2014-04-02 15:55:49 +02:00
|
|
|
stream->flags &= ~NGHTTP2_STREAM_FLAG_DEFERRED_ALL;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-12-20 11:53:57 +01:00
|
|
|
return stream_update_dep_on_detach_item(stream, session);
|
2014-03-25 18:04:24 +01:00
|
|
|
}
|
|
|
|
|
2014-12-20 11:53:57 +01:00
|
|
|
int nghttp2_stream_defer_item(nghttp2_stream *stream, uint8_t flags,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session *session) {
|
2014-12-20 11:53:57 +01:00
|
|
|
assert(stream->item);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-12-20 11:53:57 +01:00
|
|
|
DEBUGF(fprintf(stderr, "stream: stream=%d defer item=%p cause=%02x\n",
|
|
|
|
stream->stream_id, stream->item, flags));
|
2014-04-03 08:48:19 +02:00
|
|
|
|
2014-04-02 15:55:49 +02:00
|
|
|
stream->flags |= flags;
|
2014-04-03 08:48:19 +02:00
|
|
|
|
2014-12-20 11:53:57 +01:00
|
|
|
return stream_update_dep_on_detach_item(stream, session);
|
2012-02-19 15:42:25 +01:00
|
|
|
}
|
|
|
|
|
2014-12-20 11:53:57 +01:00
|
|
|
int nghttp2_stream_resume_deferred_item(nghttp2_stream *stream, uint8_t flags,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session *session) {
|
2014-12-20 11:53:57 +01:00
|
|
|
assert(stream->item);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-12-20 11:53:57 +01:00
|
|
|
DEBUGF(fprintf(stderr, "stream: stream=%d resume item=%p flags=%02x\n",
|
|
|
|
stream->stream_id, stream->item, flags));
|
2014-04-03 08:52:21 +02:00
|
|
|
|
2014-09-26 14:32:17 +02:00
|
|
|
stream->flags &= ~flags;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->flags & NGHTTP2_STREAM_FLAG_DEFERRED_ALL) {
|
2014-09-26 14:32:17 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-12-20 11:53:57 +01:00
|
|
|
return stream_update_dep_on_attach_item(stream, session);
|
2014-04-02 15:55:49 +02:00
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-12-20 11:53:57 +01:00
|
|
|
int nghttp2_stream_check_deferred_item(nghttp2_stream *stream) {
|
|
|
|
return stream->item && (stream->flags & NGHTTP2_STREAM_FLAG_DEFERRED_ALL);
|
2014-04-02 15:55:49 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int nghttp2_stream_check_deferred_by_flow_control(nghttp2_stream *stream) {
|
2014-12-20 11:53:57 +01:00
|
|
|
return stream->item &&
|
2014-11-27 15:39:04 +01:00
|
|
|
(stream->flags & NGHTTP2_STREAM_FLAG_DEFERRED_FLOW_CONTROL);
|
2012-02-19 15:42:25 +01:00
|
|
|
}
|
2012-03-09 16:10:11 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int update_initial_window_size(int32_t *window_size_ptr,
|
|
|
|
int32_t new_initial_window_size,
|
|
|
|
int32_t old_initial_window_size) {
|
2013-08-08 17:58:52 +02:00
|
|
|
int64_t new_window_size = (int64_t)(*window_size_ptr) +
|
2014-11-27 15:39:04 +01:00
|
|
|
new_initial_window_size - old_initial_window_size;
|
|
|
|
if (INT32_MIN > new_window_size ||
|
|
|
|
new_window_size > NGHTTP2_MAX_WINDOW_SIZE) {
|
2013-08-08 14:12:49 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2014-06-11 16:37:16 +02:00
|
|
|
*window_size_ptr = (int32_t)new_window_size;
|
2013-08-08 14:12:49 +02:00
|
|
|
return 0;
|
2012-03-09 16:10:11 +01:00
|
|
|
}
|
2013-07-24 18:49:05 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int nghttp2_stream_update_remote_initial_window_size(
|
|
|
|
nghttp2_stream *stream, int32_t new_initial_window_size,
|
|
|
|
int32_t old_initial_window_size) {
|
2013-08-08 17:58:52 +02:00
|
|
|
return update_initial_window_size(&stream->remote_window_size,
|
|
|
|
new_initial_window_size,
|
|
|
|
old_initial_window_size);
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int nghttp2_stream_update_local_initial_window_size(
|
|
|
|
nghttp2_stream *stream, int32_t new_initial_window_size,
|
|
|
|
int32_t old_initial_window_size) {
|
2013-08-08 17:58:52 +02:00
|
|
|
return update_initial_window_size(&stream->local_window_size,
|
|
|
|
new_initial_window_size,
|
|
|
|
old_initial_window_size);
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void nghttp2_stream_promise_fulfilled(nghttp2_stream *stream) {
|
2013-07-24 18:49:05 +02:00
|
|
|
stream->state = NGHTTP2_STREAM_OPENED;
|
2015-02-09 13:45:55 +01:00
|
|
|
stream->flags &= ~NGHTTP2_STREAM_FLAG_PUSH;
|
2013-07-24 18:49:05 +02:00
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_stream *nghttp2_stream_get_dep_root(nghttp2_stream *stream) {
|
2015-06-19 12:36:24 +02:00
|
|
|
for (; stream->dep_prev; stream = stream->dep_prev)
|
|
|
|
;
|
2014-03-25 18:04:24 +01:00
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nghttp2_stream_dep_subtree_find(nghttp2_stream *stream,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_stream *target) {
|
|
|
|
if (stream == NULL) {
|
2014-03-25 18:04:24 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream == target) {
|
2014-03-25 18:04:24 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_stream_dep_subtree_find(stream->sib_next, target)) {
|
2014-03-25 18:04:24 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nghttp2_stream_dep_subtree_find(stream->dep_next, target);
|
|
|
|
}
|
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
int32_t nghttp2_stream_compute_effective_weight(nghttp2_stream *stream) {
|
|
|
|
int32_t weight;
|
|
|
|
|
|
|
|
if (!stream->dep_prev) {
|
|
|
|
return stream->weight;
|
|
|
|
}
|
|
|
|
|
|
|
|
weight = nghttp2_stream_compute_effective_weight(stream->dep_prev) *
|
|
|
|
stream->weight / stream->dep_prev->sum_norest_weight;
|
|
|
|
|
|
|
|
return nghttp2_max(1, weight);
|
|
|
|
}
|
|
|
|
|
2014-03-25 18:04:24 +01:00
|
|
|
void nghttp2_stream_dep_insert(nghttp2_stream *dep_stream,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_stream *stream) {
|
2014-03-25 18:04:24 +01:00
|
|
|
nghttp2_stream *si;
|
2015-06-19 12:36:24 +02:00
|
|
|
nghttp2_stream *blocking_stream;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
assert(stream->dpri == NGHTTP2_STREAM_DPRI_NO_ITEM);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
|
|
|
DEBUGF(fprintf(stderr,
|
|
|
|
"stream: dep_insert dep_stream(%p)=%d, stream(%p)=%d\n",
|
2014-11-27 15:39:04 +01:00
|
|
|
dep_stream, dep_stream->stream_id, stream, stream->stream_id));
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-04-14 16:53:54 +02:00
|
|
|
stream->sum_dep_weight = dep_stream->sum_dep_weight;
|
|
|
|
dep_stream->sum_dep_weight = stream->weight;
|
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
blocking_stream = stream_get_dep_blocking(dep_stream);
|
|
|
|
|
|
|
|
stream->sum_norest_weight = 0;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (dep_stream->dep_next) {
|
2015-06-19 12:36:24 +02:00
|
|
|
assert(dep_stream->num_substreams >= 1);
|
|
|
|
/* num_substreams includes node itself */
|
|
|
|
stream->num_substreams = dep_stream->num_substreams;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (si = dep_stream->dep_next; si; si = si->sib_next) {
|
2015-06-19 12:36:24 +02:00
|
|
|
si->dep_prev = stream;
|
|
|
|
if (!blocking_stream && (si->dpri == NGHTTP2_STREAM_DPRI_TOP ||
|
|
|
|
(si->dpri == NGHTTP2_STREAM_DPRI_NO_ITEM &&
|
|
|
|
si->sum_norest_weight))) {
|
|
|
|
stream->sum_norest_weight += si->weight;
|
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
stream->dep_next = dep_stream->dep_next;
|
|
|
|
}
|
|
|
|
|
|
|
|
dep_stream->dep_next = stream;
|
|
|
|
stream->dep_prev = dep_stream;
|
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
if (stream->sum_norest_weight) {
|
|
|
|
dep_stream->sum_norest_weight = stream->weight;
|
|
|
|
}
|
2014-04-24 16:37:40 +02:00
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
stream_update_dep_length(dep_stream, 1);
|
2014-04-17 14:15:14 +02:00
|
|
|
|
|
|
|
++stream->roots->num_streams;
|
2014-03-25 18:04:24 +01:00
|
|
|
}
|
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
static void set_dep_prev(nghttp2_stream *stream, nghttp2_stream *dep) {
|
|
|
|
for (; stream; stream = stream->sib_next) {
|
|
|
|
stream->dep_prev = dep;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static void link_dep(nghttp2_stream *dep_stream, nghttp2_stream *stream) {
|
2014-10-15 18:12:59 +02:00
|
|
|
dep_stream->dep_next = stream;
|
2015-06-19 12:36:24 +02:00
|
|
|
if (stream) {
|
|
|
|
stream->dep_prev = dep_stream;
|
|
|
|
}
|
2014-10-15 18:12:59 +02:00
|
|
|
}
|
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
static void link_sib(nghttp2_stream *a, nghttp2_stream *b) {
|
|
|
|
a->sib_next = b;
|
|
|
|
if (b) {
|
|
|
|
b->sib_prev = a;
|
|
|
|
}
|
2014-10-15 18:12:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void insert_link_dep(nghttp2_stream *dep_stream,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_stream *stream) {
|
2014-10-15 18:12:59 +02:00
|
|
|
nghttp2_stream *sib_next;
|
|
|
|
|
|
|
|
assert(stream->sib_prev == NULL);
|
|
|
|
|
|
|
|
sib_next = dep_stream->dep_next;
|
|
|
|
|
|
|
|
link_sib(stream, sib_next);
|
|
|
|
|
|
|
|
link_dep(dep_stream, stream);
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static void unlink_sib(nghttp2_stream *stream) {
|
2014-10-16 14:59:52 +02:00
|
|
|
nghttp2_stream *prev, *next, *dep_next;
|
|
|
|
|
|
|
|
prev = stream->sib_prev;
|
|
|
|
dep_next = stream->dep_next;
|
|
|
|
|
|
|
|
assert(prev);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (dep_next) {
|
2014-10-16 14:59:52 +02:00
|
|
|
/*
|
|
|
|
* prev--stream(--sib_next--...)
|
|
|
|
* |
|
|
|
|
* dep_next
|
|
|
|
*/
|
|
|
|
|
|
|
|
link_sib(prev, dep_next);
|
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
set_dep_prev(dep_next, stream->dep_prev);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->sib_next) {
|
2014-10-16 14:59:52 +02:00
|
|
|
link_sib(stream_last_sib(dep_next), stream->sib_next);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* prev--stream(--sib_next--...)
|
|
|
|
*/
|
|
|
|
next = stream->sib_next;
|
|
|
|
|
|
|
|
prev->sib_next = next;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (next) {
|
2014-10-16 14:59:52 +02:00
|
|
|
next->sib_prev = prev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static void unlink_dep(nghttp2_stream *stream) {
|
2014-10-16 14:59:52 +02:00
|
|
|
nghttp2_stream *prev, *next, *dep_next;
|
|
|
|
|
|
|
|
prev = stream->dep_prev;
|
|
|
|
dep_next = stream->dep_next;
|
|
|
|
|
|
|
|
assert(prev);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (dep_next) {
|
2014-10-16 14:59:52 +02:00
|
|
|
/*
|
|
|
|
* prev
|
|
|
|
* |
|
|
|
|
* stream(--sib_next--...)
|
|
|
|
* |
|
|
|
|
* dep_next
|
|
|
|
*/
|
|
|
|
link_dep(prev, dep_next);
|
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
set_dep_prev(dep_next, stream->dep_prev);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->sib_next) {
|
2014-10-16 14:59:52 +02:00
|
|
|
link_sib(stream_last_sib(dep_next), stream->sib_next);
|
|
|
|
}
|
2015-06-19 12:36:24 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
} else if (stream->sib_next) {
|
2014-10-16 14:59:52 +02:00
|
|
|
/*
|
|
|
|
* prev
|
|
|
|
* |
|
|
|
|
* stream--sib_next
|
|
|
|
*/
|
|
|
|
next = stream->sib_next;
|
|
|
|
|
|
|
|
next->sib_prev = NULL;
|
|
|
|
|
|
|
|
link_dep(prev, next);
|
|
|
|
} else {
|
|
|
|
prev->dep_next = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-25 18:04:24 +01:00
|
|
|
void nghttp2_stream_dep_add(nghttp2_stream *dep_stream,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_stream *stream) {
|
2015-06-19 12:36:24 +02:00
|
|
|
assert(stream->dpri == NGHTTP2_STREAM_DPRI_NO_ITEM);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
DEBUGF(fprintf(stderr, "stream: dep_add dep_stream(%p)=%d, stream(%p)=%d\n",
|
|
|
|
dep_stream, dep_stream->stream_id, stream, stream->stream_id));
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
stream_update_dep_length(dep_stream, 1);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-04-14 16:53:54 +02:00
|
|
|
dep_stream->sum_dep_weight += stream->weight;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (dep_stream->dep_next == NULL) {
|
2014-10-16 14:59:52 +02:00
|
|
|
link_dep(dep_stream, stream);
|
2014-04-14 16:53:54 +02:00
|
|
|
} else {
|
2014-10-15 18:12:59 +02:00
|
|
|
insert_link_dep(dep_stream, stream);
|
2014-03-25 18:04:24 +01:00
|
|
|
}
|
|
|
|
|
2014-04-17 14:15:14 +02:00
|
|
|
++stream->roots->num_streams;
|
2014-03-25 18:04:24 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void nghttp2_stream_dep_remove(nghttp2_stream *stream) {
|
2015-06-19 12:36:24 +02:00
|
|
|
nghttp2_stream *next, *dep_prev, *si, *blocking_stream;
|
|
|
|
int32_t sum_dep_weight_delta, sum_norest_weight_delta;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
assert(stream->dpri == NGHTTP2_STREAM_DPRI_NO_ITEM);
|
2014-04-24 16:37:40 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
DEBUGF(fprintf(stderr, "stream: dep_remove stream(%p)=%d\n", stream,
|
|
|
|
stream->stream_id));
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
blocking_stream = stream_get_dep_blocking(stream->dep_prev);
|
|
|
|
|
2014-04-14 16:53:54 +02:00
|
|
|
/* Distribute weight of |stream| to direct descendants */
|
|
|
|
sum_dep_weight_delta = -stream->weight;
|
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
sum_norest_weight_delta = 0;
|
|
|
|
|
|
|
|
/* blocking_stream == NULL means that ascendants are all
|
|
|
|
NGHTTP2_STREAM_DPRI_NO_ITEM */
|
|
|
|
if (!blocking_stream && stream->sum_norest_weight) {
|
|
|
|
sum_norest_weight_delta -= stream->weight;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (si = stream->dep_next; si; si = si->sib_next) {
|
2014-04-14 16:53:54 +02:00
|
|
|
si->weight = nghttp2_stream_dep_distributed_weight(stream, si->weight);
|
|
|
|
|
|
|
|
sum_dep_weight_delta += si->weight;
|
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
if (!blocking_stream &&
|
|
|
|
(si->dpri == NGHTTP2_STREAM_DPRI_TOP ||
|
|
|
|
(si->dpri == NGHTTP2_STREAM_DPRI_NO_ITEM && si->sum_norest_weight))) {
|
|
|
|
sum_norest_weight_delta += si->weight;
|
|
|
|
}
|
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
dep_prev = stream->dep_prev;
|
2014-04-14 16:53:54 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (dep_prev) {
|
2015-06-19 12:36:24 +02:00
|
|
|
stream_update_dep_length(dep_prev, -1);
|
2014-04-14 16:53:54 +02:00
|
|
|
|
|
|
|
dep_prev->sum_dep_weight += sum_dep_weight_delta;
|
2015-06-19 12:36:24 +02:00
|
|
|
dep_prev->sum_norest_weight += sum_norest_weight_delta;
|
2014-03-25 18:04:24 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->sib_prev) {
|
2014-10-16 14:59:52 +02:00
|
|
|
unlink_sib(stream);
|
2014-11-27 15:39:04 +01:00
|
|
|
} else if (stream->dep_prev) {
|
2014-10-16 14:59:52 +02:00
|
|
|
unlink_dep(stream);
|
2014-03-25 18:04:24 +01:00
|
|
|
} else {
|
2014-04-17 14:15:14 +02:00
|
|
|
nghttp2_stream_roots_remove(stream->roots, stream);
|
|
|
|
|
2014-03-25 18:04:24 +01:00
|
|
|
/* stream is a root of tree. Removing stream makes its
|
|
|
|
descendants a root of its own subtree. */
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (si = stream->dep_next; si;) {
|
2014-03-25 18:04:24 +01:00
|
|
|
next = si->sib_next;
|
|
|
|
|
|
|
|
si->dep_prev = NULL;
|
|
|
|
si->sib_prev = NULL;
|
|
|
|
si->sib_next = NULL;
|
|
|
|
|
2014-04-17 14:15:14 +02:00
|
|
|
nghttp2_stream_roots_add(si->roots, si);
|
|
|
|
|
2014-03-25 18:04:24 +01:00
|
|
|
si = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stream->num_substreams = 1;
|
2014-04-14 16:53:54 +02:00
|
|
|
stream->sum_dep_weight = 0;
|
2015-06-19 12:36:24 +02:00
|
|
|
stream->sum_norest_weight = 0;
|
2014-04-14 16:53:54 +02:00
|
|
|
|
2014-03-25 18:04:24 +01:00
|
|
|
stream->dep_prev = NULL;
|
|
|
|
stream->dep_next = NULL;
|
|
|
|
stream->sib_prev = NULL;
|
|
|
|
stream->sib_next = NULL;
|
2014-04-17 14:15:14 +02:00
|
|
|
|
|
|
|
--stream->roots->num_streams;
|
2014-03-25 18:04:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int nghttp2_stream_dep_insert_subtree(nghttp2_stream *dep_stream,
|
|
|
|
nghttp2_stream *stream,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session *session) {
|
2014-03-25 18:04:24 +01:00
|
|
|
nghttp2_stream *last_sib;
|
|
|
|
nghttp2_stream *dep_next;
|
2015-06-19 12:36:24 +02:00
|
|
|
nghttp2_stream *blocking_stream;
|
|
|
|
nghttp2_stream *si;
|
2014-03-25 18:04:24 +01:00
|
|
|
size_t delta_substreams;
|
|
|
|
|
|
|
|
DEBUGF(fprintf(stderr, "stream: dep_insert_subtree dep_stream(%p)=%d "
|
2014-11-27 15:39:04 +01:00
|
|
|
"stream(%p)=%d\n",
|
|
|
|
dep_stream, dep_stream->stream_id, stream, stream->stream_id));
|
2014-03-25 18:04:24 +01:00
|
|
|
|
|
|
|
delta_substreams = stream->num_substreams;
|
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
blocking_stream = stream_get_dep_blocking(dep_stream);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (dep_stream->dep_next) {
|
2014-04-14 16:53:54 +02:00
|
|
|
/* dep_stream->num_substreams includes dep_stream itself */
|
|
|
|
stream->num_substreams += dep_stream->num_substreams - 1;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-04-14 16:53:54 +02:00
|
|
|
stream->sum_dep_weight += dep_stream->sum_dep_weight;
|
|
|
|
dep_stream->sum_dep_weight = stream->weight;
|
|
|
|
|
|
|
|
dep_next = dep_stream->dep_next;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
if (!blocking_stream && dep_stream->sum_norest_weight) {
|
|
|
|
stream_update_dep_set_rest(dep_next);
|
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-10-16 14:59:52 +02:00
|
|
|
link_dep(dep_stream, stream);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->dep_next) {
|
2014-03-25 18:04:24 +01:00
|
|
|
last_sib = stream_last_sib(stream->dep_next);
|
|
|
|
|
2014-10-16 14:59:52 +02:00
|
|
|
link_sib(last_sib, dep_next);
|
2014-03-25 18:04:24 +01:00
|
|
|
} else {
|
2014-10-16 14:59:52 +02:00
|
|
|
link_dep(stream, dep_next);
|
2014-03-25 18:04:24 +01:00
|
|
|
}
|
2015-06-19 12:36:24 +02:00
|
|
|
|
|
|
|
for (si = dep_next; si; si = si->sib_next) {
|
|
|
|
si->dep_prev = stream;
|
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
} else {
|
2014-10-16 14:59:52 +02:00
|
|
|
link_dep(dep_stream, stream);
|
2014-04-14 16:53:54 +02:00
|
|
|
|
|
|
|
assert(dep_stream->sum_dep_weight == 0);
|
|
|
|
dep_stream->sum_dep_weight = stream->weight;
|
2014-03-25 18:04:24 +01:00
|
|
|
}
|
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
stream_update_dep_length(dep_stream, delta_substreams);
|
|
|
|
|
|
|
|
if (blocking_stream) {
|
|
|
|
stream_update_dep_set_rest(stream);
|
2014-03-31 15:51:33 +02:00
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stream_update_dep_set_top(stream) == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-03-31 15:51:33 +02:00
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
dep_stream->sum_norest_weight = stream->weight;
|
|
|
|
stream_update_dep_sum_norest_weight(dep_stream->dep_prev, dep_stream->weight);
|
2014-04-24 16:37:40 +02:00
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
return stream_update_dep_queue_top(stream, session);
|
2014-03-25 18:04:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int nghttp2_stream_dep_add_subtree(nghttp2_stream *dep_stream,
|
|
|
|
nghttp2_stream *stream,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session *session) {
|
2015-06-19 12:36:24 +02:00
|
|
|
nghttp2_stream *blocking_stream;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
|
|
|
DEBUGF(fprintf(stderr, "stream: dep_add_subtree dep_stream(%p)=%d "
|
2014-11-27 15:39:04 +01:00
|
|
|
"stream(%p)=%d\n",
|
|
|
|
dep_stream, dep_stream->stream_id, stream, stream->stream_id));
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (dep_stream->dep_next) {
|
2014-04-14 16:53:54 +02:00
|
|
|
dep_stream->sum_dep_weight += stream->weight;
|
|
|
|
|
2014-10-15 18:12:59 +02:00
|
|
|
insert_link_dep(dep_stream, stream);
|
2014-03-25 18:04:24 +01:00
|
|
|
} else {
|
2014-10-16 14:59:52 +02:00
|
|
|
link_dep(dep_stream, stream);
|
2014-04-14 16:53:54 +02:00
|
|
|
|
|
|
|
assert(dep_stream->sum_dep_weight == 0);
|
|
|
|
dep_stream->sum_dep_weight = stream->weight;
|
2014-03-25 18:04:24 +01:00
|
|
|
}
|
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
stream_update_dep_length(dep_stream, stream->num_substreams);
|
|
|
|
|
|
|
|
blocking_stream = stream_get_dep_blocking(dep_stream);
|
|
|
|
|
|
|
|
if (blocking_stream) {
|
|
|
|
/* We cannot make any assumption for stream if its dpri is not
|
|
|
|
NGHTTP2_DPRI_TOP. Just dfs under stream here. */
|
|
|
|
stream_update_dep_set_rest(stream);
|
2014-03-31 15:51:33 +02:00
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2014-03-31 15:51:33 +02:00
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
if (stream->dpri == NGHTTP2_STREAM_DPRI_TOP) {
|
|
|
|
stream_update_dep_sum_norest_weight(dep_stream, stream->weight);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stream_update_dep_set_top(stream) == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-04-24 16:37:40 +02:00
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
/* Newly added subtree contributes to dep_stream's
|
|
|
|
sum_norest_weight */
|
|
|
|
stream_update_dep_sum_norest_weight(dep_stream, stream->weight);
|
|
|
|
|
|
|
|
return stream_update_dep_queue_top(stream, session);
|
2014-03-25 18:04:24 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void nghttp2_stream_dep_remove_subtree(nghttp2_stream *stream) {
|
2015-06-19 12:36:24 +02:00
|
|
|
nghttp2_stream *next, *dep_prev, *blocking_stream;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
DEBUGF(fprintf(stderr, "stream: dep_remove_subtree stream(%p)=%d\n", stream,
|
|
|
|
stream->stream_id));
|
2014-03-31 15:51:33 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->sib_prev) {
|
2015-06-19 12:36:24 +02:00
|
|
|
link_sib(stream->sib_prev, stream->sib_next);
|
|
|
|
dep_prev = stream->dep_prev;
|
2014-11-27 15:39:04 +01:00
|
|
|
} else if (stream->dep_prev) {
|
2014-04-14 16:53:54 +02:00
|
|
|
dep_prev = stream->dep_prev;
|
2014-03-25 18:04:24 +01:00
|
|
|
next = stream->sib_next;
|
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
link_dep(dep_prev, next);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (next) {
|
2014-03-25 18:04:24 +01:00
|
|
|
next->sib_prev = NULL;
|
|
|
|
}
|
2014-04-14 16:53:54 +02:00
|
|
|
} else {
|
2014-04-17 14:15:14 +02:00
|
|
|
nghttp2_stream_roots_remove(stream->roots, stream);
|
|
|
|
|
2014-04-14 16:53:54 +02:00
|
|
|
dep_prev = NULL;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (dep_prev) {
|
2014-04-14 16:53:54 +02:00
|
|
|
dep_prev->sum_dep_weight -= stream->weight;
|
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
stream_update_dep_length(dep_prev, -stream->num_substreams);
|
|
|
|
|
|
|
|
blocking_stream = stream_get_dep_blocking(dep_prev);
|
2014-04-14 16:53:54 +02:00
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
if (!blocking_stream && (stream->dpri == NGHTTP2_STREAM_DPRI_TOP ||
|
|
|
|
(stream->dpri == NGHTTP2_STREAM_DPRI_NO_ITEM &&
|
|
|
|
stream->sum_norest_weight))) {
|
|
|
|
stream_update_dep_sum_norest_weight(dep_prev, -stream->weight);
|
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
stream->sib_prev = NULL;
|
|
|
|
stream->sib_next = NULL;
|
|
|
|
stream->dep_prev = NULL;
|
|
|
|
}
|
|
|
|
|
2014-11-11 16:26:23 +01:00
|
|
|
int nghttp2_stream_dep_make_root(nghttp2_stream *stream,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session *session) {
|
|
|
|
DEBUGF(fprintf(stderr, "stream: dep_make_root stream(%p)=%d\n", stream,
|
|
|
|
stream->stream_id));
|
2014-03-31 15:51:33 +02:00
|
|
|
|
2014-04-17 14:15:14 +02:00
|
|
|
nghttp2_stream_roots_add(stream->roots, stream);
|
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
if (stream_update_dep_set_top(stream) == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-04-24 16:37:40 +02:00
|
|
|
|
2014-11-11 16:26:23 +01:00
|
|
|
return stream_update_dep_queue_top(stream, session);
|
2014-03-25 18:04:24 +01:00
|
|
|
}
|
2014-04-17 14:15:14 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int
|
|
|
|
nghttp2_stream_dep_all_your_stream_are_belong_to_us(nghttp2_stream *stream,
|
|
|
|
nghttp2_session *session) {
|
2014-04-17 14:15:14 +02:00
|
|
|
nghttp2_stream *first, *si;
|
|
|
|
|
|
|
|
DEBUGF(fprintf(stderr, "stream: ALL YOUR STREAM ARE BELONG TO US "
|
2014-11-27 15:39:04 +01:00
|
|
|
"stream(%p)=%d\n",
|
2014-04-17 14:15:14 +02:00
|
|
|
stream, stream->stream_id));
|
|
|
|
|
|
|
|
first = stream->roots->head;
|
|
|
|
|
|
|
|
/* stream must not be include in stream->roots->head list */
|
|
|
|
assert(first != stream);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (first) {
|
2014-04-17 14:15:14 +02:00
|
|
|
nghttp2_stream *prev;
|
|
|
|
|
|
|
|
prev = first;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
DEBUGF(fprintf(stderr, "stream: root stream(%p)=%d\n", first,
|
|
|
|
first->stream_id));
|
2014-04-17 14:15:14 +02:00
|
|
|
|
|
|
|
stream->sum_dep_weight += first->weight;
|
|
|
|
stream->num_substreams += first->num_substreams;
|
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
if (stream->dpri != NGHTTP2_STREAM_DPRI_NO_ITEM &&
|
|
|
|
(first->dpri == NGHTTP2_STREAM_DPRI_TOP ||
|
|
|
|
(first->dpri == NGHTTP2_STREAM_DPRI_NO_ITEM &&
|
|
|
|
first->sum_norest_weight))) {
|
|
|
|
stream_update_dep_set_rest(first);
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (si = first->root_next; si; si = si->root_next) {
|
2014-04-17 14:15:14 +02:00
|
|
|
|
|
|
|
assert(si != stream);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
DEBUGF(
|
|
|
|
fprintf(stderr, "stream: root stream(%p)=%d\n", si, si->stream_id));
|
2014-04-17 14:15:14 +02:00
|
|
|
|
|
|
|
stream->sum_dep_weight += si->weight;
|
|
|
|
stream->num_substreams += si->num_substreams;
|
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
if (stream->dpri != NGHTTP2_STREAM_DPRI_NO_ITEM &&
|
|
|
|
(si->dpri == NGHTTP2_STREAM_DPRI_TOP ||
|
|
|
|
(si->dpri == NGHTTP2_STREAM_DPRI_NO_ITEM &&
|
|
|
|
si->sum_norest_weight))) {
|
|
|
|
stream_update_dep_set_rest(si);
|
|
|
|
}
|
|
|
|
|
2014-10-16 14:59:52 +02:00
|
|
|
link_sib(prev, si);
|
2015-06-19 12:36:24 +02:00
|
|
|
si->dep_prev = stream;
|
2014-04-17 14:15:14 +02:00
|
|
|
|
|
|
|
prev = si;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->dep_next) {
|
2014-10-15 18:12:59 +02:00
|
|
|
nghttp2_stream *sib_next;
|
2014-04-17 14:15:14 +02:00
|
|
|
|
2014-10-15 18:12:59 +02:00
|
|
|
sib_next = stream->dep_next;
|
|
|
|
|
2015-06-19 12:36:24 +02:00
|
|
|
first->dep_prev = stream;
|
2014-10-15 18:12:59 +02:00
|
|
|
link_sib(first, sib_next);
|
|
|
|
link_dep(stream, prev);
|
2014-04-17 14:15:14 +02:00
|
|
|
} else {
|
2014-10-16 14:59:52 +02:00
|
|
|
link_dep(stream, first);
|
2014-04-17 14:15:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nghttp2_stream_roots_remove_all(stream->roots);
|
|
|
|
|
2014-11-11 16:26:23 +01:00
|
|
|
return nghttp2_stream_dep_make_root(stream, session);
|
2014-04-17 14:15:14 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int nghttp2_stream_in_dep_tree(nghttp2_stream *stream) {
|
|
|
|
return stream->dep_prev || stream->dep_next || stream->sib_prev ||
|
|
|
|
stream->sib_next || stream->root_next || stream->root_prev ||
|
|
|
|
stream->roots->head == stream;
|
2014-04-17 14:15:14 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void nghttp2_stream_roots_init(nghttp2_stream_roots *roots) {
|
2014-04-17 14:15:14 +02:00
|
|
|
roots->head = NULL;
|
|
|
|
roots->num_streams = 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void nghttp2_stream_roots_free(nghttp2_stream_roots *roots _U_) {}
|
2014-04-17 14:15:14 +02:00
|
|
|
|
|
|
|
void nghttp2_stream_roots_add(nghttp2_stream_roots *roots,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_stream *stream) {
|
|
|
|
if (roots->head) {
|
2014-04-17 14:15:14 +02:00
|
|
|
stream->root_next = roots->head;
|
|
|
|
roots->head->root_prev = stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
roots->head = stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nghttp2_stream_roots_remove(nghttp2_stream_roots *roots,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_stream *stream) {
|
2014-04-17 14:15:14 +02:00
|
|
|
nghttp2_stream *root_prev, *root_next;
|
|
|
|
|
|
|
|
root_prev = stream->root_prev;
|
|
|
|
root_next = stream->root_next;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (root_prev) {
|
2014-04-17 14:15:14 +02:00
|
|
|
root_prev->root_next = root_next;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (root_next) {
|
2014-04-17 14:15:14 +02:00
|
|
|
root_next->root_prev = root_prev;
|
|
|
|
}
|
|
|
|
} else {
|
2014-11-27 15:39:04 +01:00
|
|
|
if (root_next) {
|
2014-04-17 14:15:14 +02:00
|
|
|
root_next->root_prev = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
roots->head = root_next;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream->root_prev = NULL;
|
|
|
|
stream->root_next = NULL;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void nghttp2_stream_roots_remove_all(nghttp2_stream_roots *roots) {
|
2014-04-17 14:15:14 +02:00
|
|
|
nghttp2_stream *si, *next;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (si = roots->head; si;) {
|
2014-04-17 14:15:14 +02:00
|
|
|
next = si->root_next;
|
|
|
|
|
|
|
|
si->root_prev = NULL;
|
|
|
|
si->root_next = NULL;
|
|
|
|
|
|
|
|
si = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
roots->head = NULL;
|
|
|
|
}
|